flutter - 如何在 Flutter 中将 AlertDialog FlatButton 居中

标签 flutter flutter-alertdialog

如何在不创建自定义 AlertDialog 的情况下设置 FlatButton 的样式使其居中对齐?

AlertDialog(
      title: Text(
        'Alert Dialog'.toUpperCase(),
        textAlign: TextAlign.center,
        style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28.0),
      ),
      content: SingleChildScrollView(
        child: ListBody(
          children: <Widget>[
            Text('Scrollable AlertDialog' * 100),
          ],
        ),
      ),
      actions: <Widget>[
        FlatButton(
          child: Text(
            'Accept'.toUpperCase(),
          ),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );

This is how it looks like

我尝试删除“actions”并在“content”的一行内添加 FlatButton,但滚动停止工作并且内容溢出。

content: Column(
        children: <Widget>[
          SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Text('Scrollable AlertDialog.' * 50),
              ],
            ),
          ),
          Row(
            children: <Widget>[
              FlatButton(
                child: Text(
                  'Accept'.toUpperCase(),
                  style: TextStyle(
                      color: Theme.of(context).primaryColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 16.0
                  ),
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
        ],
      ),

最佳答案

Google 的 Material 设计指南不希望您将其放在中心位置,这就是为什么 Flutter 团队将 ButtonBar 用于 actions 而您无法控制 ButtonBaractions 属性中的对齐方式,所以如果您真的想让单个按钮居中,有 2 个简单的解决方法:

  1. 修改源代码,在dialog.dartAlertDialogbuild方法中,你会看到一个ButtonBar,widget,给它加上alignment

    ButtonBar(
      alignment: MainAxisAlignment.center, // add this line 
      // ...
    ) 
    
  2. 您可以使用像 SizedBox 这样的虚拟小部件并为其提供一些宽度。

    actions: [
      SizedBox(width: space),
      FlatButton(
        onPressed: () {},
        child: Text('Button'),
      ),
      SizedBox(width: space),
    ]
    

截图:

enter image description here

关于flutter - 如何在 Flutter 中将 AlertDialog FlatButton 居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60791192/

相关文章:

Flutter & AlertDialog : How to stretch a container so that it has the same width with the AlertDialog box?

flutter - 如何垂直扩展Container中的TextField以覆盖Flutter中的所有可用空间

Flutter:通过按下后退按钮解除键盘时,不关注文本字段(多行)

typescript - 来自 TypeScript 的匿名类型对象的 Dart 等价物

function - 如何从 Flutter 中的方法获取 AlertDialog 回调?

android - 如何在 FlatButton 单击时关闭 AlertDialog?

flutter - Flutter&AlertDialog:加载后我的应用程序不显示警报对话框

android - 如何更新build.gradle

azure - 使用 Microsoft Azure 进行身份验证时如何使用 native 客户端重定向 URI?

flutter - 如何在 Flutter 中显示 2 个连续的对话框