gpt4 book ai didi

flutter - 如何在 flutter 中创建自定义对话框

转载 作者:行者123 更新时间:2023-12-04 09:30:56 24 4
gpt4 key购买 nike

我想创建一个自定义对话框,如下所示。我能够创建一个带有两个按钮(正按钮和负按钮)的普通对话框。但是我搜索了很多关于创建如下所示的自定义对话框但徒劳无功。
enter image description here

showAlertDialog(BuildContext context) {

// set up the buttons
Widget cancelButton = FlatButton(
child: Text("Cancel"),
onPressed: () {},
);
Widget continueButton = FlatButton(
child: Text("Continue"),
onPressed: () {},
);

// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Action"),
content: Text("Would you like to continue learning how to use Flutter alerts?"),
actions: [
cancelButton,
continueButton,
],
);

// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
现在我想将这些按钮和图像作为对话框的子项,以及底部的图标按钮“X”来关闭对话框。任何帮助表示赞赏。我是一个完整的 flutter 初学者。

最佳答案

为此,我们创建了一个自定义对话框
1.自定义对话框内容类

class CustomDialog extends StatelessWidget {

dialogContent(BuildContext context) {
return Container(
decoration: new BoxDecoration(
color: Colors.white,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 10.0,
offset: const Offset(0.0, 10.0),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min, // To make the card compact
children: <Widget>[
Image.asset('assets/images/image.jpg', height: 100),
Text(
"Text 1",
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.w700,
),
),
SizedBox(height: 16.0),
Text(
"Text 1",
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.w700,
),
),
SizedBox(height: 24.0),
Align(
alignment: Alignment.bottomCenter,
child: Icon(Icons.cancel),
),
],
),
);
}

@override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 0.0,
backgroundColor: Colors.transparent,
child: dialogContent(context),
);
}
}
2.点击调用自定义对话框:
RaisedButton(
color: Colors.redAccent,
textColor: Colors.white,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return CustomDialog();
});
;
},
child: Text("PressMe"),
),

关于flutter - 如何在 flutter 中创建自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62848764/

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