简体   繁体   English

如何在页面上制作 Navigator.of(context).pop 并在 Flutter 上重置它

[英]How make a Navigator.of(context).pop on a page and reset it on Flutter

I have a product page that has some information inside it.我有一个产品页面,里面有一些信息。 This page has some widgets inside it.这个页面里面有一些小部件。

The problem is when I change this information and press the back button on top of the page that will execute a Navigator.of(context).pop.问题是当我更改此信息并按下将执行 Navigator.of(context).pop 的页面顶部的后退按钮时。

When I click again to show the page, the product has the same information that I put from before.当我再次单击以显示该页面时,该产品具有我之前输入的相同信息。

How can I reset the page when I execute Navigator.of(context).pop?执行 Navigator.of(context).pop 时如何重置页面?

My problem is resolve if i use:如果我使用,我的问题就解决了:

Navigator.of(context).pushReplacementNamed('/Pages', arguments: 0);

But I don`t want that, because every time I do a pushReplacement they load again a page already loaded before.但我不希望这样,因为每次我执行 pushReplacement 时,他们都会再次加载之前已经加载的页面。

This is the sequence of events to show the problem.这是显示问题的事件序列。

First i click on image of the product in a grid and this is what i execute:首先,我单击网格中的产品图像,这就是我执行的操作:

Get.toNamed('/Food',   arguments: new RouteArgument(
                        heroTag: this.widget.heroTag,
                        param:  widget.foodList.elementAt(index),),);

When i change the page i execute this function:当我更改页面时,我执行此 function:

  @override
  Widget build(BuildContext context) {
    _con.iniPage(this.routeArgument);
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(0.0), // here the desired height
        child: AppBar(
          automaticallyImplyLeading: false,
          brightness: Brightness.dark,
          backgroundColor: Colors.transparent,
        ),
      ),

This function _con.iniPage(this.routeArgument);这个 function _con.iniPage(this.routeArgument); execute a clone event from the object that was passed by a parameter, and this is the funtion:从 object 执行一个由参数传递的克隆事件,这是函数:

iniPage(RouteArgument routeArgument) {
    this.routeArgument = new RouteArgument(
                            heroTag: routeArgument.heroTag,
                            param: routeArgument.param,);
    Food foodTemp = routeArgument.param;
    this.food = routeArgument.param.copyAll(food: foodTemp);
    calculateTotal();
}

See that I`m cloning the object?看到我正在克隆 object 吗? This is my function to clone:这是我要克隆的 function:

Food copyAll({Food food}) {
    return Food(
      id: id ?? food.id,
      name: name ?? food.name,
      price: price ?? food.price,
      discountPrice: discountPrice ?? food.discountPrice,
      image: image ?? food.image,
      description: description ?? food.description,
      ingredients: ingredients ?? food.ingredients,
      weight: weight ?? food.weight,
      unit: unit ?? food.unit,
      packageItemsCount: packageItemsCount ?? food.packageItemsCount,
      featured: featured ?? food.featured,
      deliverable: deliverable ?? food.deliverable,
      restaurant: restaurant ?? food.restaurant,
      category: category ?? food.category,
      extraGroupsFoods: extraGroupsFoods ?? food.extraGroupsFoods,
      extraGroupsRules: extraGroupsRules ?? food.extraGroupsRules,
      foodReviews: foodReviews ?? food.foodReviews,
      nutritions: nutritions ?? food.nutritions
    );
  }

After cloning that is the first thing i do, my page make a loop in this widget:克隆之后,这是我做的第一件事,我的页面在这个小部件中循环:

                    _con.food.extraGroupsFoods != null &&
                            _con.food.extraGroupsFoods.length > 0
                        ? Column(
                            children: [
                              for (var extraGroupFood in _con.food.extraGroupsFoods)
                                _con.getExtraSelectType(
                                extraGroupFood,
                                _con.food.extraGroupsRules),
                              ExtraObsWidget(_con.getObsText),
                            ],
                          )
                        : Container(),

The problem is here, I simple loop on the cloned object but if I change the information in this specific widget, press back to pop this screen and return to this screen, the state of my information do not reset why?问题就在这里,我在克隆的object上简单循环,但是如果我更改了这个特定小部件中的信息,按返回弹出这个屏幕并返回这个屏幕,我的信息的state没有重置为什么?

And when i see the parameter object was modified and i don`t know why because i made a clone from this object.当我看到参数 object 被修改时,我不知道为什么,因为我从这个 object 进行了克隆。

if you mean resetting second page use WillPopScope to reset second page before poping如果您的意思是重置第二页,请使用 WillPopScope 在弹出之前重置第二页

WillPopScope(
    onWillPop: () async {
    // your reset logic here
   return Future.value(true);
},
    child: new Scaffold(

if you mean resetting first page use await with push to reset first page after coming back如果您的意思是重置第一页,请使用 await 并在返回后通过 push 重置第一页

await Navigator.of(context).pushNamed('/Pages');
// some logic will execute when popping second page

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

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