问题描述
我想要做的是创建一个自定义对话框,其中包含许多颤动按钮, 当用户按下按钮时,我的目标是关闭自定义对话框并知道按下了哪个按钮(以便通过使用提供程序刷新我的主页)
我定义了带有 2 个按钮的自定义对话框(例如)。我如何才能实现我的目标?
代码如下:
CustomDialog.dart
import 'package:Flutter/material.dart';
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>[
RaisedButton(
onpressed: (){},child: Text("Button 1"),RaisedButton(
onpressed: (){},child: Text("Button 2"),);
}
@override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),elevation: 0.0,backgroundColor: Colors.transparent,child: dialogContent(context),);
}
}
在 main.dart 中我称之为:
Container(
child: Center(
child: RaisedButton(
onpressed: (){
showDialog(context: context,builder: (BuildContext context){
return CustomDialog(
);
}
);
},child: Text("Custom Dialog"),
解决方法
从对话框中可以将值返回到打开它的位置
首先,你必须等待打开对话框时的值
return Container(
child: Center(
child: RaisedButton(
onPressed: () async {
var pressedButtonNumber = await showDialog<int>(
context: context,builder: (BuildContext context) {
return CustomDialog();
});
print(pressedButtonNumber);
},child: Text("Custom Dialog"),),);
}
然后你必须在关闭对话框时返回值
RaisedButton(
onPressed: () {
Navigator.of(context).pop(1);
},child: Text("Button 1"),
,
我是这样解决的:
showDialog(context: context,builder: (BuildContext context){
return CustomDialog(
);
}
).then((value) {
});
在自定义对话框中:
Navigator.pop(context,//** RETURNED VALUE**//);