Flutter中 AlertDialog、SimpleDialog、showModalBottomSheet及第三方组件fluttertoast的使用

Uma ·
更新时间:2024-11-14
· 786 次阅读

main.dart 

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        //标题栏
        appBar: AppBar(
          title: Text("Dialog Demo"),
        ),
        //内容区域
        body: DatePickerPubPage(),
      ),
      //主题
      theme: ThemeData(primarySwatch: Colors.red),
    );
  }
}
class DatePickerPubPage extends StatefulWidget {
  @override
  _DatePickerPubPageState createState() => _DatePickerPubPageState();
}
class _DatePickerPubPageState extends State {
  void _alertDialog() async {
    var resault = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("提示信息"),
            content: Text("确定要删除吗?"),
            actions: [
              FlatButton(
                child: Text("取消"),
                onPressed: () {
                  Navigator.pop(context, 'Cancel');
                  print("取消");
                },
              ),
              FlatButton(
                child: Text("确定"),
                onPressed: () {
                  Navigator.pop(context, 'Ok');
                  print("确定");
                },
              ),
            ],
          );
        });
    print(resault);
  }
  void _alertSimpleDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("选择选修课程"),
            children: [
              SimpleDialogOption(
                child: Text("商务礼仪"),
                onPressed: () {
                  Navigator.pop(context);
                  print("商务礼仪");
                },
              ),
              //加条线
              Divider(),
              SimpleDialogOption(
                child: Text("政治经济"),
                onPressed: () {
                  Navigator.pop(context);
                  print("政治经济");
                },
              ),
              //加条线
              Divider(),
              SimpleDialogOption(
                child: Text("市场营销"),
                onPressed: () {
                  Navigator.pop(context);
                  print("市场营销");
                },
              ),
              Divider(),
            ],
          );
        });
  }
  void _alertBottomDialog() async {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 220,
            child: Column(
              children: [
                ListTile(
                  title: Text("大话设计模式"),
                  onTap: () {
                    Navigator.pop(context);
                    print("大话设计模式");
                  },
                ),
                Divider(),
                ListTile(
                  title: Text("Java编程思想"),
                  onTap: () {
                    Navigator.pop(context);
                    print("Java编程思想");
                  },
                ),
                Divider(),
                ListTile(
                  title: Text("Android开发艺术探索"),
                  onTap: () {
                    Navigator.pop(context);
                    print("Android开发艺术探索");
                  },
                ),
                Divider(),
              ],
            ),
          );
        });
  }
  void _alertToast() async {
    Fluttertoast.showToast(
        msg: "我是一个toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIos: 1,
        backgroundColor: Colors.grey,
        textColor: Colors.white,
        fontSize: 14.0);
  }
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RaisedButton(
            child: Text("AlertDialog"),
            onPressed: _alertDialog,
          ),
          SizedBox(height: 20),
          RaisedButton(
            child: Text("SimpleDialog"),
            onPressed: _alertSimpleDialog,
          ),
          SizedBox(height: 20),
          RaisedButton(
            child: Text("ActionSheet底部弹出框"),
            onPressed: _alertBottomDialog,
          ),
          SizedBox(height: 20),
          RaisedButton(
            child: Text("toast-fluttertoast第三方库"),
            onPressed: _alertToast,
          ),
          SizedBox(height: 20),
        ],
      ),
    );
  }
}

pubspec.yaml

dependencies:
  fluttertoast: ^3.1.1

运行效果:


作者:蓝枫amy



alertdialog flutter

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章