繁体   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?

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

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

有帮助吗?

我正在从这个重定向到列表页面...

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

现在我在navigator.pop中传递这些值

在这里获取价值并传回第一页。

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

想在这里使用新值...

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

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

从 Page1 导航到 Page2

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

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

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

//Page2
    Navigator.pop(context);

这不足以刷新您的 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