其他分享
首页 > 其他分享> > 【Flutter】入门14-对话框

【Flutter】入门14-对话框

作者:互联网

SimpleDialog

 String _choice = '请选择';

  Future _openSimpleDialog() async {
    final option = await showDialog(
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: Text('请选择'),
            children: <Widget>[
              SimpleDialogOption(
                child: Text('item1'),
                onPressed: () {
                  //关闭对话框并放回 "item1"
                  Navigator.pop(context, "item1");
                },
              ),
              SimpleDialogOption(
                child: Text('item2'),
                onPressed: () {
                  Navigator.pop(context, 'item2');
                },
              ),
              FlatButton(
                  onPressed: () {
                    Navigator.pop(context, '请选择');
                  },
                  child: Text('取消'))
            ],
          );
        });

    setState(() {
      _choice = option;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Gecer')),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              //使用InkWell 是为了给子元素添加点击事件
              InkWell(
                onTap: _openSimpleDialog,
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Icon(
                        Icons.favorite,
                        color: Colors.redAccent,
                      ),
                      //DateFormat 引用于intl
                      Text(_choice)
                    ]),
              )
            ]));
  }

AlertDialog 

Future _openAlertDialog() async {
    final action = await showDialog(
      context: context,
      barrierDismissible: false, //当为flase时代表不能通过点击空白处关闭对话框
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('提示'),
          content: Text('To be?'),
          actions: <Widget>[
            FlatButton(
              child: Text('Cancel'),
              onPressed: () {
                Navigator.pop(context, false);
              },
            ),
            FlatButton(
              child: Text('Ok'),
              onPressed: () {
                Navigator.pop(context, true);
              },
            ),
          ],
        );
      },
    );
    setState(() {
      _choice = action.toString();
    });
  }

BottomSheet(通过key)

final _bottomSheetScaffoldKey = GlobalKey<ScaffoldState>();
  _openBottomSheet() {
    _bottomSheetScaffoldKey.currentState
        .showBottomSheet((BuildContext context) {
      return BottomAppBar(
        child: Container(
          height: 90.0,
          width: double.infinity,
          padding: EdgeInsets.all(16.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(Icons.favorite,color:Colors.redAccent),
             Text('surprise')
            ],
          ),
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: _bottomSheetScaffoldKey,
        appBar: AppBar(title: Text('Gecer')),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              //使用InkWell 是为了给子元素添加点击事件
              InkWell(
                onTap: _openBottomSheet,
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Icon(
                        Icons.favorite,
                        color: Colors.redAccent,
                      ),
                      Text('打开')
                    ]),
              )
            ]));
  }

BottomSheet(不通过key)

String _choice = '打开';
  Future _openModalBottomSheet() async {
    final option = await showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            height: 200.0,
            child: Column(
              children: <Widget>[
                ListTile(
                  title: Text('Item1'),
                  onTap: () {
                    Navigator.pop(context, 'Item1');
                  },
                ),
                ListTile(
                  title: Text('Item2'),
                  onTap: () {
                    Navigator.pop(context, 'Item2');
                  },
                ),
                ListTile(
                  title: Text('Item3'),
                  onTap: () {
                    Navigator.pop(context, 'Item3');
                  },
                ),
              ],
            ),
          );
        });
    setState(() {
      _choice = option;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Gecer')),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              //使用InkWell 是为了给子元素添加点击事件
              InkWell(
                onTap: _openModalBottomSheet,
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Icon(
                        Icons.favorite,
                        color: Colors.redAccent,
                      ),
                      Text(_choice)
                    ]),
              )
            ]));
  }

SnackBar

//调用
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Gecer')),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SnackBarButton()
            ]));
  }


//定义
class SnackBarButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FlatButton(
      child: Text('Open SnackBar'),
      onPressed: () {
        Scaffold.of(context).showSnackBar(
          SnackBar(
            content: Text('Processing...'),
            action: SnackBarAction(
              label: 'OK',
              onPressed: () {},
            ),
          )
        );
      },
    );
  }
}

 

夜雨Gecer 发布了63 篇原创文章 · 获赞 4 · 访问量 8008 私信 关注

标签:14,对话框,Text,title,BuildContext,children,context,child,Flutter
来源: https://blog.csdn.net/weixin_39370093/article/details/104191815