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

How to style AlertDialog Actions in Flutter

我用这个方法来显示一个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();
                  })
            ],
          );
        },
      );
    }
  }

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

我想设计一些按钮的样式,例如一个开始另一个结束。 我在文档中搜索但只找到了如何制作它们 "Stacked full-width buttons"。 有关于如何设置按钮样式的想法吗?

自定义小部件

编辑小部件本身:在 AlertDialog 下有一个 ButtonBar widget where you can use alignment: MainAxisAlignment.spaceBetween to align the buttons correctly. See 用于自定义 AlertDialog 小部件的示例。

自己的按钮行

您也可以删除 actions 下的按钮并添加自己的自定义 RowRaisedButtons,就像这样:

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

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

不要在 AlertDialog 的 actions 中添加按钮。如您所见。

_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,

                    ),
                  ),
                ),

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

将按钮移动到内容是一个很好的解决方案。

showDialog(
              context: context,
              barrierDismissible: false,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Center(child: Text('Alert')),
                  content: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Container(
                        child: Text(
                          "message",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            color: Colors.red,
                          ),
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          FlatButton(
                              child: Text('Yes'),
                              onPressed: () {
                                Navigator.of(context).pop();
                              }),
                          FlatButton(
                              child: Text('No'),
                              onPressed: () {
                                Navigator.of(context).pop();
                              })
                        ])
                    ],
                  ),
                );
              });

或者您可以为此使用 RFlutter Alert 库。它易于定制且易于使用。它的默认样式包括圆角,您可以根据需要添加任意数量的按钮。

警报样式:

var alertStyle = AlertStyle(
    animationType: AnimationType.fromTop,
    isCloseButton: false,
    isOverlayTapDismiss: false,
    descStyle: TextStyle(fontWeight: FontWeight.bold),
    animationDuration: Duration(milliseconds: 400),
    alertBorder: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(0.0),
    side: BorderSide(
        color: Colors.grey,
    ),
    ),
    titleStyle: TextStyle(
    color: Colors.red,
    ),
);

并将您的 AlertStyle 对象分配给 Alert 的样式字段。

Alert(
    context: context,
    style: alertStyle,
    type: AlertType.info,
    title: "RFLUTTER ALERT",
    desc: "Flutter is more awesome with RFlutter Alert.",
    buttons: [
    DialogButton(
        child: Text(
        "COOL",
        style: TextStyle(color: Colors.white, fontSize: 20),
        ),
        onPressed: () => Navigator.pop(context),
        color: Color.fromRGBO(0, 179, 134, 1.0),
        radius: BorderRadius.circular(0.0),
    ),
    ],
).show();

*我是 RFlutter Alert 的开发者之一。

更换主题是个不错的选择。

MaterialApp(
  theme: ThemeData(
      buttonBarTheme: ButtonBarThemeData(
    alignment: MainAxisAlignment.center,
  )),
  ...

我使用此方法在 AlertDialog 的操作中对齐按钮。

这是如何运作的::

SizedBox 使用其上下文(在语句 MediaQuery.of(context 中)占据警报对话框的整个宽度。 size.width。之后,使用一行,将 space 放置在主轴的子项之间(mainAxisAlignment => 一行的 x-axis)。

AlertDialog 样式:

AlertDialog(
        title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('Tilte'),
              CloseButton(
                  color: Color(0xFFD5D3D3),
                  onPressed: () {
                    Navigator.of(context).pop();
                  })
            ]),
        content: SingleChildScrollView(child: Text("Boby")),
        actions: [
          SizedBox(
              width: MediaQuery.of(context).size.width,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ButtonTheme(
                        minWidth: 25.0,
                        height: 25.0,
                        child: OutlineButton(
                            borderSide: BorderSide(color: Colors.blueAccent),
                            child: new Text("Save"),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(20)),
                            onPressed: () {})),
                    SizedBox(width: 8.0),
                    ButtonTheme(
                        minWidth: 25.0,
                        height: 25.0,
                        child: OutlineButton(
                            borderSide: BorderSide(color: Colors.black26),
                            textColor: Colors.blue,
                            child: new Text("Close"),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(20)),
                            onPressed: () {}))
                  ]))
        ]);