简体   繁体   English

是否可以使用 navigator.pop 刷新或重新加载页面...例如第 1(nav.push=>2)页到第 2 页,然后从第 2(nav.pop,值)返回到第 1 页?

[英]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?

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?我想使用 navigator.pop 将值从第二页传递到第一页,并使用initstate中的新值或任何其他解决方法刷新或重新加载我的第一页?

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...我能够在第一页中获取这些值,但无法在initstate中使用新值刷新或重新加载页面,我想在 API 中传递新数据 n 获取响应...

any help?有帮助吗?

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现在我在navigator.pop中传递这些值

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),
                    ),
                  ),
                );
              }),
        )

Want to use new values here...想在这里使用新值...

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

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.当我使用 pop 函数中传递的新值弹出回到第一页时,只想从 initstate 或任何其他方式刷新我的页面。

to navigate from Page1 to Page2从 Page1 导航到 Page2

usually you use Navigator.push() for example:通常你使用 Navigator.push() 例如:

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

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 中时,你会做一些工作,例如在共享首选项中保存一些数据,然后返回到 Page1 并刷新 Page1 以从共享首选项中获取新数据,你可以做的是使用弹出到 Page1

//Page2
    Navigator.pop(context);

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,因此您需要在 Page1 导航器中附加 a.then 作为回调,当您从 page2 弹出时将调用它。then 应该有一个设置状态来触发重建,只需调用 a set state 中的某些东西导致像这样的重建:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM