是否可以使用navigator.pop ...刷新或重新加载页面,例如第一页(nav.push => 2)回到第二页,然后从第二页(nav.pop,value)退回到第一页? [英] is it possible to refresh or reload page with navigator.pop... like 1st (nav.push=>2) page to 2nd page and back from 2nd (nav.pop, value) to 1st page?

查看:497
本文介绍了是否可以使用navigator.pop ...刷新或重新加载页面,例如第一页(nav.push => 2)回到第二页,然后从第二页(nav.pop,value)退回到第一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用navigator.pop将值从第二页传递到第一页,并以initstate或其他任何替代方法用新值刷新或重新加载第一页吗?

I want to pass values from 2nd page to 1st page with navigator.pop and refresh or reload my 1st page with new values in initstate or any other work around?

我能够在第1页中获得这些值,但无法在initstate中刷新或重新加载具有新值的页面,在此我想在API中传递新数据n获得响应...

I am able to get these values in 1st page but not able to refresh or reload the page with new values in initstate where i want to pass new data in API n get response...

有什么帮助吗?

1. I am redirecting to list page from this...

  getBrand(BuildContext context) async {
    tempBrand = await Navigator.push(context,
        MaterialPageRoute(builder: (context) => BrandList(widget.subCatID)));
    selectedBrand = tempBrand.name;
    selectedBrandID = tempBrand.id;
  }

now here i am passing those values in navigator.pop...

2. getting value here and passing back to 1st page...

Container(
          padding: EdgeInsets.all(10),
          child: ListView.separated(
              separatorBuilder: (context, index) =>
                  Divider(color: Color(0xffcccccc)),
              itemCount: prodBrands.length,
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  child: InkWell(
                    onTap: () {
                      tempProdBrand = ProdBrandListModel.Message(
                        id: prodBrands[index].id,
                        name: prodBrands[index].name,
                      );

                      Navigator.pop(context, tempProdBrand);

                    },
                    child: Container(
                      child: Text(prodBrands[index].name),
                      padding: EdgeInsets.all(10.0),
                    ),
                  ),
                );
              }),
        )

3. Want to use new values here...

submitProduct() {
    api
        .newbiz(
      selectedBrandID,
    )
        .then((response) {
      setState(() {
        productID = response.message;
      });
    });
    setState(() {});
  }

当我弹出时,只想从initstate或任何其他方式刷新页面

Simply want to refresh my page from initstate or any other way when i pop back to 1st page with my new values passed in pop function.

推荐答案

从第1页导航到第2页

通常使用Navigator.push()
例如:

usually you use Navigator.push() for example:

// Page1
    Navigator.push(context, MaterialPageRoute(builder: (context) => Page2()));

当您在Page2中时,您需要做一些工作,例如以共享首选项保存一些数据并返回到Page1并刷新Page1以从共享的首选项中获取新数据,您可以执行以下操作:

and when you are in Page2 you do some work for example saving some data in shared preference and go back to Page1 and refresh Page1 to get the new data from shared preferences, what you can do is to pop to Page1 using

//Page2
    Navigator.pop(context);

这不足以刷新Page1
,因此您需要附加一个。然后在Page1导航器中作为回调从您从page2弹出时将被调用,然后.then应该具有设置状态以触发重建,只需在set状态内调用某种东西以导致重建
,如下所示:

this is not gonna be enough to refresh your Page1 so you need to attach a .then in the Page1 Navigator as a callback it will be called when you pop from page2 and that .then should have a set state to trigger a rebuild just call a something inside the set state to cause a rebuild like this:

//Page1
    Navigator.push(context, MaterialPageRoute(builder: (context) => Page2())).then((value) {
                  setState(() {
                    // refresh state of Page1
                  });
                });

这篇关于是否可以使用navigator.pop ...刷新或重新加载页面,例如第一页(nav.push => 2)回到第二页,然后从第二页(nav.pop,value)退回到第一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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