SimppleDialog和AlertDialog

SimpleDialog和AlertDialog本身都是一个Widget,使用时需要通过showDialog方法来展示

// 展示SimpleDialog
showDialog( //展示Dialog的方法
  context: context,
  builder: (context) {
    return SimpleDialog(
      title: Text('评价一下'), //标题
      titlePadding: EdgeInsets.all(20), //标题的padding值
      children: <Widget>[ //弹框中的选项
        SimpleDialogOption( //每一个选项都是一个SimpleDialogOption Widget
          onPressed: (){
            print('给个好评');
            Navigator.pop(context);
          },
          child: Text('给好评'), //选项提示文案
        ),
        SimpleDialogOption(
          onPressed: (){
            print('残忍拒绝');
            Navigator.pop(context);
          },
          child: Text('残忍拒绝'),
        ),
        SimpleDialogOption(
          onPressed: (){
            print('我有意见');
            Navigator.pop(context);
          },
          child: Text('我有意见'),
        ),
      ],
      contentPadding: EdgeInsets.all(0),
    );
  },
);
 
//展示AlertDialog
showDialog(
  context: context,
  builder: (context) {
    return AlertDialog(
      title: Text('提示'), //标题
      titlePadding: EdgeInsets.all(20), //标题的padding值
      content: Text('是否想放弃学习Flutter'), //弹框展示主要内容
      contentPadding: EdgeInsets.only(left: 20, right: 20), //内容的padding值
      actions: <Widget>[ //操作按钮数组
        FlatButton(
          onPressed: () {
            print("取消");
            Navigator.pop(context);
          },
          child: Text('取消'),
        ),
        FlatButton(
          onPressed: () {
            print('确定');
            Navigator.pop(context);
          },
          child: Text('确定'),
        ),
      ],
    );
  },
);
原文地址:https://www.cnblogs.com/timba1322/p/12487010.html