Dart 如何设置颤振中AlertDialog动作的样式
我使用此方法显示AlertDialog:Dart 如何设置颤振中AlertDialog动作的样式,dart,flutter,android-alertdialog,Dart,Flutter,Android Alertdialog,我使用此方法显示AlertDialog: _onSubmit(message) { if (message.isNotEmpty) { showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return AlertDialog( title: Center(c
_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();
})
],
);
},
);
}
}
\u提交(消息){
if(message.isNotEmpty){
显示对话框(
上下文:上下文,
禁止:错误,
生成器:(BuildContext上下文){
返回警报对话框(
标题:中心(子项:文本(“警报”),
内容:行(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
扩大(
子:文本(
消息
textAlign:textAlign.center,
样式:TextStyle(
颜色:颜色,红色,
),
),
)
],
),
行动:[
扁平按钮(
子项:文本('Cancel'),
已按下:(){
Navigator.of(context.pop();
}),
扁平按钮(
子项:文本('Ok'),
已按下:(){
_inputExtController.clear();
Navigator.of(context.pop();
})
],
);
},
);
}
}
一切正常,但按钮在右侧对齐,如下图所示:
我想设计一些按钮的样式,例如一个在开始,另一个在结束。
我在文档中搜索,但只找到了如何使它们成为“堆叠的全宽按钮”。
有没有关于如何设置按钮样式的想法?自定义小部件 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: () {})) ])) ]); 编辑小部件本身:在
AlertDialog
下有一个对话框,您可以使用alignment:MainAxisAlignment.spaceBetween来正确对齐按钮。有关自定义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: () {}))
]))
]);
您还可以删除操作
下的按钮,并添加自己的自定义行
,其中包含以下内容:
Row (
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
RaisedButton(), // button 1
RaisedButton(), // button 2
]
)
行(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
RaisedButton(),//按钮1
RaisedButton(),//按钮2
]
)
在您的情况下,您可以在内容中的行周围添加一个,并在其中添加您现有的行和根据上述示例创建的修改行。不要在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,
),
),
),
FlatButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
}),
FlatButton(
child: Text('Ok'),
onPressed: () {
_inputTextController.clear();
Navigator.of(context).pop();
})
],
),
);
},
);
}
}
\u提交(消息){
if(message.isNotEmpty){
显示对话框(
上下文:上下文,
禁止:错误,
生成器:(BuildContext上下文){
返回警报对话框(
标题:中心(子项:文本(“警报”),
内容:行(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
扩大(
子:文本(
消息
textAlign:textAlign.center,
样式:TextStyle(
颜色:颜色,红色,
),
),
),
扁平按钮(
子项:文本('Cancel'),
已按下:(){
Navigator.of(context.pop();
}),
扁平按钮(
子项:文本('Ok'),
已按下:(){
_inputExtController.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();
})
])
],
),
);
});
showDialog(
上下文:上下文,
禁止:错误,
生成器:(BuildContext上下文){
返回警报对话框(
标题:中心(子项:文本(“警报”),
内容:专栏(
mainAxisSize:mainAxisSize.min,
儿童:[
容器(
子:文本(
“信息”,
textAlign:textAlign.center,
样式:TextStyle(
颜色:颜色,红色,
),
),
),
划船(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
扁平按钮(
子项:文本('Yes'),
已按下:(){
Navigator.of(context.pop();
}),
扁平按钮(
child:Text('No'),
已按下:(){
Navigator.of(context.pop();
})
])
],
),
);
});
或者您可以使用该库。它易于定制和使用。它的默认样式包括圆角,您可以添加任意数量的按钮
警报样式:
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(
主题:主题数据(
buttonBarTheme:buttonBarTheme数据(
对齐:MainAxisAlignment.center,
)),
...
我使用此方法来对齐AlertDialog操作中的按钮
工作原理:
SizedBox使用alertdialog的上下文(在语句MediaQuery.of(context).size.width中)获取alertdialog的全宽。然后,使用一行,将其子行之间的空间放置在主轴上(mainAxisAlignment=>行的x轴)
AlertDialo