我使用这个方法来显示一个 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/