wavrik
wavrik

Reputation: 451

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.

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?

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.

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:

  @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); execute a clone event from the object that was passed by a parameter, and this is the funtion:

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? This is my function to clone:

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?

And when i see the parameter object was modified and i don`t know why because i made a clone from this object.

Upvotes: 0

Views: 1050

Answers (1)

Moaid ALRazhy
Moaid ALRazhy

Reputation: 1744

if you mean resetting second page use WillPopScope to reset second page before poping

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 Navigator.of(context).pushNamed('/Pages');
// some logic will execute when popping second page

Upvotes: 1

Related Questions