我在Flutter中有一个带有按钮的StatefulWidget,它使用Navigator.push()将我导航到另一个StatefulWidget。在第二个小部件上,我正在更改全局状态(一些用户首选项)。当我从第二个小部件回到第一个时,使用Navigator.pop(),第一个小部件处于旧状态,但我想强制它重新加载。知道怎么做吗?我有一个想法,但看起来很难看:

弹出以删除第二个小部件(当前小部件) 再次弹出以删除第一个小部件(前一个) 推送第一个小部件(它应该强制重绘)


当前回答

简短的回答:

在第一页使用这个:

Navigator.pushNamed(context, '/page2').then((_) => setState(() {}));

在第二页

Navigator.pop(context);

有两个东西,传递数据

第一页至第二页 在第一页使用这个 //从第一个发送“Foo 导航器。push(context, MaterialPageRoute(builder: (_) => Page2("Foo"))); 在第二页使用这个。 类Page2扩展StatelessWidget 字符串; 所以Page2 (this.string);//在第二帧接收“Foo” ... }


第二页至第一页 在第二页使用这个 //从第二发送“Bar” 导航器。流行(上下文,“酒吧”); 在第一页使用这个,它是相同的,但有一点修改。 //第1位接收“Bar” 字符串received = await Navigator。push(context, MaterialPageRoute(builder: (_) => Page2("Foo")));

其他回答

我的解决方案是在SecondPage上添加一个函数参数,然后接收从FirstPage执行的重新加载函数,然后在Navigator.pop(context)行之前执行该函数。

珍宝

refresh() {
setState(() {
//all the reload processes
});
}

然后翻到下一页……

Navigator.push(context, new MaterialPageRoute(builder: (context) => new SecondPage(refresh)),);

SecondPage

final Function refresh;
SecondPage(this.refresh); //constructor

然后在导航器弹出行之前,

widget.refresh(); // just refresh() if its statelesswidget
Navigator.pop(context);

所有需要从前一页重新加载的内容都应该在弹出窗口后更新。

您可以在弹出上下文时传递回一个动态结果,然后在值为true时调用setState((){}),否则就保持状态不变。

我粘贴了一些代码片段供您参考。

handleClear() async {
    try {
      var delete = await deleteLoanWarning(
        context,
        'Clear Notifications?',
        'Are you sure you want to clear notifications. This action cannot be undone',
      );
      if (delete.toString() == 'true') {
        //call setState here to rebuild your state.

      }
    } catch (error) {
      print('error clearing notifications' + error.toString());
             }
  }



Future<bool> deleteLoanWarning(BuildContext context, String title, String msg) async {

  return await showDialog<bool>(
        context: context,
        child: new AlertDialog(
          title: new Text(
            title,
            style: new TextStyle(fontWeight: fontWeight, color: CustomColors.continueButton),
            textAlign: TextAlign.center,
          ),
          content: new Text(
            msg,
            textAlign: TextAlign.justify,
          ),
          actions: <Widget>[
            new Container(
              decoration: boxDecoration(),
              child: new MaterialButton(
                child: new Text('NO',),
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
              ),
            ),
            new Container(
              decoration: boxDecoration(),
              child: new MaterialButton(
                child: new Text('YES', ),
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
              ),
            ),
          ],
        ),
      ) ??
      false;
}

问候, Mahi

只需添加.then((value) {setState(() {});在导航器。按page1(),如下所示:

Navigator.push(context,MaterialPageRoute(builder: (context) => Page2())).then((value) { setState(() {});

现在当你使用Navigator.pop(context)从page2你的page1重建自己

这工作真的很好,我从一个医生那里得到的在颤振页面,颤振医生

我定义了从第一个页面控制导航的方法。

_navigateAndDisplaySelection(BuildContext context) async {
    final result = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => AddDirectionPage()),
    );

    //below you can get your result and update the view with setState
    //changing the value if you want, i just wanted know if i have to  
    //update, and if is true, reload state

    if (result) {
      setState(() {});
    }
  }

所以,我调用它在一个动作方法从一个墨水瓶,但也可以调用从一个按钮:

onTap: () {
   _navigateAndDisplaySelection(context);
},

最后在第二页,返回一些东西(我返回了一个bool,你可以返回任何你想要的):

onTap: () {
  Navigator.pop(context, true);
}

简短的回答:

在第一页使用这个:

Navigator.pushNamed(context, '/page2').then((_) => setState(() {}));

在第二页

Navigator.pop(context);

有两个东西,传递数据

第一页至第二页 在第一页使用这个 //从第一个发送“Foo 导航器。push(context, MaterialPageRoute(builder: (_) => Page2("Foo"))); 在第二页使用这个。 类Page2扩展StatelessWidget 字符串; 所以Page2 (this.string);//在第二帧接收“Foo” ... }


第二页至第一页 在第二页使用这个 //从第二发送“Bar” 导航器。流行(上下文,“酒吧”); 在第一页使用这个,它是相同的,但有一点修改。 //第1位接收“Bar” 字符串received = await Navigator。push(context, MaterialPageRoute(builder: (_) => Page2("Foo")));