1

I have a menu page which has tabA and tabB. The appBar has a search button.

menu.dart

   @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Item List'),
        actions: [
          _currentIndex == 0
              ? Row(
                  children: [
                    IconButton(
                        onPressed: () {
                            Navigator.pushNamed(context, SearchScreen.ROUTE,
                                arguments: list);                        
                        },
                        icon: Icon(
                          Icons.search,
                          color: Colors.white,
                        )),
                    IconButton(
                      icon: Icon(
                        Icons.tune,
                        color: Colors.white,
                      ),
                      onPressed: () {
                            ...
                            }
                          }
                        });
                      },
                    )
                  ],
                )
              : Container()
        ],
        bottom: TabBar(
          unselectedLabelColor: Colors.white,
          labelColor: Colors.white,
          isScrollable: false,
          tabs: <Widget>[
            Tab(
              text: "Tab A",
            ),
            Tab(
              text: "Tab B",
            ),
          ],
          controller: _tabController,
          indicatorColor: Colors.white,
          indicatorSize: TabBarIndicatorSize.tab,
        ),
        bottomOpacity: 1,
      ),
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          ShowTabA(),
          ShowTabB()
        ],
      ),
    );
  }
}

In tabA, it has a listView. When I tap on one of the listview item, it will navigate to EditPage. When return from EditPage, I will refresh the data in tabA. All the list item will be updated.

tabA.page

initState(){
    _bloc.getItemList();
}

Navigator.pushNamed(EditPage.ROUTE,context).then((onValue){
 _bloc.getItemList();
});

My problem here:

When I click the search icon in menu page , it will navigate to SearchScreen page.Once user click the list item in SearchScreen, it will navigate to EditPage.

When back from EditPage, I want the list in TabA page refreshed but it can't because I navigate using pushReplacementNamed in menu page . How to achieve that ?

SearchScreen.dart

Navigator.pushReplacementNamed(context);
8
  • are you using a provider in your app to manage the state? I guess _bloc is the object of that provider. If my guess is right then you can use StreamProvider or ChangeNotifierProvider to update your list automatically as soon as your list value changes. Commented Jan 7, 2021 at 18:00
  • @CodeRunner But the problem now is _bloc.getItemList(); not getting called once it back from EditPage. Commented Jan 7, 2021 at 18:05
  • Navigator.pushNamed(EditPage.ROUTE,context).then((onValue){ _bloc.getItemList(); }); this part won't be called when you come back from that page because you are calling that while you are going to edit page not coming back from edit page. you can call getItems() while you hit save or update button on edit page or what you can do that is you can call getItems() inside of dispose method of that page so it will call the method every time you come back from editPage. Commented Jan 7, 2021 at 18:09
  • where should I put the dispose method? In tab A? Commented Jan 7, 2021 at 18:12
  • nope in your edit page Commented Jan 7, 2021 at 18:13

1 Answer 1

1

Updated answer on Modified Question:

Since you are using pushReplacement your await won't work. You can use the following approach to make it work:

First you have to define your routes:

routes: <String, WidgetBuilder> {
   '/': (BuildContext context) => // Your MainPage,
   '/menu': (BuildContext context) => MenuPage(),
   '/search': (BuildContext context) => SearchPage(),
   '/edit': (BuildContext context) => EditPage(),
}

Navigation from Menu to Search:

void goToSearch() async {

   await Navigator.pushNamed(context, "/search", arguments: list);
   _bloc.getItemList().then((onValue){
      setState(() {
         list = onValue;
      });
   });

}

Use pushNamed instead of pushReplacementNamed when navigating from Search to Edit:

Navigator.of(context).pushNamed("/edit");

Important step When you have to go back to Menu from Edit, you've to use popUntil method:

Navigator.popUntil(context, ModalRoute.withName('/menu'));
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I think my question is unclear and misleading.I have updated it.
Please help T.T
@Tony Happy to know that it helped. Happy Coding!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.