Flutter-showDialog被关闭后运行功能 [英] Flutter - Run function after showDialog is dismissed

查看:465
本文介绍了Flutter-showDialog被关闭后运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有一个可以在达到一定数量的点时创建AlertDialog的应用程序。在此AlertDialog中,我添加了一个按钮,该按钮应重置点。但是我不知道在AlertDialog被解雇后如何运行一个函数,如果有人可以帮助我解决这个问题将是很棒的。



使用以下代码,我称为AlertDialog:

  Future< Null> ; gewinner(int gewinner_team,List< String> spieler){
return showDialog(
context:context,
barrierDismissible:false,
builder:(BuildContext context){
返回GewinnerDialog(gewinner_team,spieler);
}
);
}

这是我的代码,用于创建AlertDialog的用户界面:

  import'package:flutter / material.dart'; 
导入‘spieler_definieren.dart’;

类GewinnerDialog扩展了StatefulWidget {
int gewinner_team;
List< String>尖锐的

GewinnerDialog(this.gewinner_team,this.spieler);

@override
State< StatefulWidget> createState()=>新的_Gewinner(gewinner_team,spieler);
}

类_Gewinner扩展了State< GewinnerDialog> {
int gewinner_team;
List< String>尖锐的

_Gewinner(this.gewinner_team,this.spieler);


@override
小部件构建(BuildContext上下文){
return new AlertDialog(
content:new SingleChildScrollView(
child:new Text (获胜者)
),
操作:< Widget> [
//此FlatButton应该重置所有保存到列表中的点< String>
/ /在另一类中
new FlatButton(
onPressed:(){
Navigator.pop(context);
},
子级:new Row(
孩子:< Widget> [
Icon(Icons.replay),
新Text( New Game)

],

) ,
new FlatButton(
onPressed:(){
Navigator.pushReplacement(context,new MaterialPageRoute(builder:(BuildContext context)=> new SpielerDefinieren()));
},
子级:新行(
c hildren:< Widget> [
Icon(Icons.person_add),
new Text( New Player)
],


],
);
}
}

如果有人可以帮助我,那就太好了问题XD

解决方案

showDialog()可以等待回调,而Navigator.pop可以将值传回。因此,而不是:

  Future< Null> gewinner(int gewinner_team,List< String> spieler){
return showDialog(
....
);
}

您可以使用:

  Future< Null> gewinner(int gewinner_team,List< String> spieler)异步{
String returnVal = await showDialog(
....
);
}

,然后在对话框生成器/屏幕中,简单地弹出一个返回值:

  Navigator.pop(context,'success'); 

然后使用 returnVal 希望。

  if(returnVal =='success'){
...
}

如果取消了对话框,则 returnVal 将为空。 / p>

I do have an app that creates a AlertDialog when a certain amount of points are reached. In this AlertDialog I added a button that should reset the points. But I do not know how to run a function after the AlertDialog is dismissed.It would be great if somebody could help me with this problem.

With this code I call the AlertDialog:

Future<Null> gewinner(int gewinner_team, List<String> spieler){
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return GewinnerDialog(gewinner_team,spieler);
        }
    );
  }

And this is my code that creates the UI of the 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")
              ],
            )
        )
      ],
    );
  }
}

It would be great if someone could help me with this problem XD

解决方案

showDialog() can await a callback, and Navigator.pop can pass a value back. so instead of:

Future<Null> gewinner(int gewinner_team, List<String> spieler){
   return showDialog(
       ....
   );
}

you can use:

Future<Null> gewinner(int gewinner_team, List<String> spieler) async {
   String returnVal = await showDialog(
       ....
   );
}

and then in the dialog builder/screen you simply pop with a return value:

Navigator.pop(context, 'success');

and then do with the returnVal what you wish.

if (returnVal == 'success') {
  ...
}

if the dialog is dismissed then returnVal will be null.

这篇关于Flutter-showDialog被关闭后运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆