我确实有一个可以在达到一定数量的点时创建AlertDialog的应用程序。在此AlertDialog中,我添加了一个应该重置点的按钮。但是我不知道在AlertDialog被解雇后如何运行函数,如果有人可以帮助我解决这个问题,那将是很棒的。
使用此代码,我称为AlertDialog:
Future<Null> gewinner(int gewinner_team, List<String> spieler){
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return GewinnerDialog(gewinner_team,spieler);
}
);
}
这是我创建AlertDialog用户界面的代码:
import 'package:flutter/material.dart';
import 'spieler_definieren.dart';
class GewinnerDialog extends StatefulWidget{
int gewinner_team;
List<String> spieler;
GewinnerDialog(this.gewinner_team, this.spieler);
@override
State<StatefulWidget> createState() => new _Gewinner(gewinner_team, spieler);
}
class _Gewinner extends State<GewinnerDialog>{
int gewinner_team;
List<String> spieler;
_Gewinner(this.gewinner_team, this.spieler);
@override
Widget build(BuildContext context) {
return new AlertDialog(
content: new SingleChildScrollView(
child: new Text("Winner")
),
actions: <Widget>[
//this FlatButton should reset all the points, that are saved into a List<String>
// in another class
new FlatButton(
onPressed: (){
Navigator.pop(context);
},
child: new Row(
children: <Widget>[
Icon(Icons.replay),
new Text("New Game")
],
)
),
new FlatButton(
onPressed: (){
Navigator.pushReplacement(context, new MaterialPageRoute(builder: (BuildContext context) => new SpielerDefinieren()));
},
child: new Row(
children: <Widget>[
Icon(Icons.person_add),
new Text("New Player")
],
)
)
],
);
}
}
如果有人可以帮助我解决XD问题,那就太好了
答案 0 :(得分:5)
showDialog()可以等待回调,而Navigator.pop可以将值传回。所以代替:
Future<Null> gewinner(int gewinner_team, List<String> spieler){
return showDialog(
....
);
}
您可以使用:
Future<Null> gewinner(int gewinner_team, List<String> spieler) async {
String returnVal = await showDialog(
....
);
}
,然后在对话框构建器/屏幕中,只需弹出一个返回值即可:
Navigator.pop(context, 'success');
,然后使用returnVal
完成您想要的操作。
if (returnVal == 'success') {
...
}
如果关闭对话框,则returnVal
将为空。
答案 1 :(得分:4)
这是从“警报对话框”获取状态的简单示例
RaisedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Are you sure?'),
content: Text('Do you want to remove item?'),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.of(context).pop('Success'),
child: Text('NO')),
FlatButton(
onPressed: () => Navigator.of(context).pop('Failure'),
child: Text('YES'))
],
)).then((value) =>
print('Result: ' + value.toString()));
},
child: Text('Show Alert Dialog'),
),