【问题标题】:Trigger FirstPage's setState method after popping Second page弹出第二页后触发FirstPage的setState方法
【发布时间】:2018-09-01 02:28:38
【问题描述】:

我的应用中有两个页面,分别是 FirstPage 和 SecondPage。

基本上,FirstPage 小部件显示一个带有项目列表的 ListView,而 SecondPage 小部件是我可以在列表中添加/删除项目的地方。

我可以通过以下方式导航到第二页:

Navigator.of(context).pushNamed("/SecondPage");

我也可以使用以下方法返回到首页:

Navigator.of(context).pop();

我的问题是我不知道如何在弹出 SecondPage 后触发 FirstPage 小部件的 setState 方法,以便更新 FirstPage 的 ListView。

我会很感激任何提示。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    每当我们push,我们都会得到一个future,我们可以使用这个future来触发setState

    Future pushNamed = Navigator.of(context).pushNamed("/SecondPage");
    pushNamed.then((_)=> setState(() {}));
    

    请参考here 将数据从 secondScreen 发送到 firstScreen。

    【讨论】:

      【解决方案2】:

      使用此代码将值从子视图发送到父视图

       Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Center(
            child: new FlatButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => SecondView(callBackValue)),
                  );
                },
                child: new Text('Open Child Widget'))),
      );
      }
      
       //Your Callback Values will be received here
      void callBackValue(double value) {
      print('Value $value');
      }
      
      // Your SecondPage
      class SecondView extends StatefulWidget{
          final void Function(double data) callback;
          SecondView(this.callback);
      
          @override
          _SecondView createState() => new _SecondView();
        }
      
        class _SecondView extends State<SecondView>{
          @override
          Widget build(BuildContext context) {
            return new Scaffold(
              appBar: new AppBar(
                title: new Text('Child Widget'),
              ),
              body: new Center(
                child: new FlatButton(onPressed: (){
                  widget.callback(10.0);
                }, child: new Text('Send Value to parent')),
              ),
            );
          }
        }
      

      【讨论】:

        【解决方案3】:

        你想要达到的效果类似于 Android 中的 startActivityForResult() 方法。

        这可以通过类似这样的颤振来实现:-

        在你的第一个活动中

          Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView());
        
        // now it will wait for the secondView to return some data
        //and the below code will be executed whenever the second view pops.
          setState((){});
        

        希望这对您有所帮助。

        别忘了在导航的方法声明中写上async

          void _navigateToSecondActivity async {
               Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView());
               setState((){});
          }
        

        当您需要导航到第二个活动时,只需调用此函数即可。

        或在您的onTap()

        onTap: () async{
          Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView());
               setState((){});
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 2018-12-23
          • 1970-01-01
          相关资源
          最近更新 更多