gpt4 book ai didi

flutter - 警告对话框中垂直对齐的 FlatButtons?

转载 作者:IT王子 更新时间:2023-10-29 07:11:12 24 4
gpt4 key购买 nike

我想在 Flutter 中显示一个带有 3 个按钮的 AlertDialog,但垂直对齐,因为按钮的文本占用了太多空间。到目前为止,我只让它们水平显示。知道如何解决吗?这篇文章 ( How to make an AlertDialog in Flutter? ) 中的解决方案对我不起作用,仍然水平显示。

  static Future<void> showLogoutAllDevicesOrOnlyThisDialog(
BuildContext context) {
var b1 = FlatButton(
textColor: Colors.red,
child: Text('Only on this device'),
onPressed: () {
Navigator.of(context).pop();
RxBus.post(HideSpinnerEvent());
},
);
var b2 = FlatButton(
textColor: Colors.red,
child: Text('On all devices'),
onPressed: () {
Navigator.of(context).pop();
RxBus.post(HideSpinnerEvent());
},
);

var b3 = FlatButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
);

return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(''),
content: Text(
"Möchtest du dich auf allen Geräten abmelden oder nur auf diesem Gerät?"),
actions: <Widget>[
b1, b2, b3
],

);
},
);
}
}

enter image description here

最佳答案

您可以创建自己的自定义对话框和您想要的任何内容,如下所示:

void _showDialog() {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(2),
),
elevation: 0,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 20,
vertical: 10,
),
child: IntrinsicWidth(
child: IntrinsicHeight(
child: Column(
children: <Widget>[
SizedBox(
height: 10,
),
Text(
"Custom Alert Dialog",
style: TextStyle(
fontWeight: FontWeight.w700,
fontFamily: "Roboto",
fontSize: 18,
),
),
SizedBox(
height: 20,
),
Text(
"This is a message inside your custom Alert Dialog!\nFeel free to change it to fit your needs.",
style: TextStyle(
fontFamily: "Roboto",
fontWeight: FontWeight.w400,
fontSize: 16,
),
),
SizedBox(
height: 30,
),
Column(
children: <Widget>[
Align(
alignment: Alignment.bottomRight,
child: FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("OK"),
),
),
Align(
alignment: Alignment.bottomRight,
child: FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Not Sure"),
),
),
Align(
alignment: Alignment.bottomRight,
child: FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Cancel"),
),
),
],
)
],
),
),
),
),
);
},
);

你的最终结果将是这个:

Column

但是如果你正在寻找一个行设计,你需要这样的东西:

void _showDialog() {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(2),
),
elevation: 0,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 20,
vertical: 10,
),
child: IntrinsicWidth(
child: IntrinsicHeight(
child: Column(
children: <Widget>[
SizedBox(
height: 10,
),
Text(
"Custom Alert Dialog",
style: TextStyle(
fontWeight: FontWeight.w700,
fontFamily: "Roboto",
fontSize: 18,
),
),
SizedBox(
height: 20,
),
Text(
"This is a message inside your custom Alert Dialog!\nFeel free to change it to fit your needs.",
style: TextStyle(
fontFamily: "Roboto",
fontWeight: FontWeight.w400,
fontSize: 16,
),
),
SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("OK"),
),
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Not Sure"),
),
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Cancel"),
),
],
)
],
),
),
),
),
);
},
);

最终结果:

Row

关于flutter - 警告对话框中垂直对齐的 FlatButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56716314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com