dart - 如何在 Flutter 中设置 AlertDialog Actions 的样式

我使用这个方法来显示一个 AlertDialog:

_onSubmit(message) {
    if (message.isNotEmpty) {
      showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Center(child: Text('Alert')),
            content: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children : <Widget>[
                Expanded(
                  child: Text(
                    message,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.red,

                    ),
                  ),
                )
              ],
            ),
            actions: <Widget>[
              FlatButton(
                  child: Text('Cancel'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  }),
              FlatButton(
                  child: Text('Ok'),
                  onPressed: () {
                    _inputTextController.clear();
                    Navigator.of(context).pop();
                  })
            ],
          );
        },
      );
    }
  }

一切正常,但按钮右对齐,如下图所示:

我想设计一些按钮的样式,例如一个开始另一个结束。 我在文档中进行了搜索,但只找到了如何使它们成为“堆叠全角按钮”。 任何想法如何设置按钮的样式?

最佳答案

2022/10/22 更新

Flutter 2.5介绍了actionsAlignment属性:

AlertDialog(
  title: ..
  actions: ..
  actionsAlignment: MainAxisAlignment.end
),

自定义小部件

编辑小部件本身:在 AlertDialog 下有一个 ButtonBar widget您可以在其中使用 alignment: MainAxisAlignment.spaceBetween 正确对齐按钮。见 this answer有关自定义 AlertDialog 小部件的示例。

自己的按钮行

您还可以删除 actions 下的按钮,并使用 RaisedButtons 添加自己的自定义 Row在其中,不知何故是这样的:

Row (
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: <Widget>[
        RaisedButton(), // button 1
        RaisedButton(), // button 2
    ]
)

在您的情况下,您可以添加 Column在 content 中的 Row 周围,并在其中添加您现有的 Row 和您从上述示例创建的修改后的行。

https://stackoverflow.com/questions/51235014/

相关文章:

android - 如何在 Flutter 中禁用 FlatButton 的飞溅突出显示?

dart - 如何格式化内插字符串

android - 在 Flutter 项目的 android studio 上初始化 Gradle

android - 在 Flutter 上捕获 Android 后退按钮事件

json - 在 Dart 中解析具有嵌套对象数组的 JSON?

flutter - Flutter 中的 MyApp 类错误未定义方法 'setState'

dart - Flutter 从 Firebase 存储加载图像

ios - 文本字段在 iOS 模拟器上不显示键盘

swift - 如何在 Flutter 中启用对现有项目的 Swift 支持

dart - flutter 实现粘性标题和对齐项目效果