简体   繁体   中英

Flutter: Refresh parent widget on Navigation.pop

I have a parent widget "BookmarkedShows" and child widget "ListOfShows". From child widget, when user taps on list item, it opens details page. When the user removes the show from bookmark from details page, on pressing back button, the show is not removed from the listing page. ie the parent is not refreshed. I'm using BlocBuilder.

There are some options mentioned in other question to add.then() to Navigator.push() method. However Navigator.push() happens in children component. How would I force refresh parent BlocBuilder during Navigation.pop()?

Parent "BookmarkedShows":

class BookmarkedShows extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => BookmarkShowsBloc()..add(LoadBookmarkedShows()),
      child: BlocBuilder<BookmarkShowsBloc, BookmarkedShowsState>(
          builder: (BuildContext context, BookmarkedShowsState state) {
              return ShowList("Bookmarked shows", state.shows)
          }),
    );
  }
}

Child "ListOfShows":

class ListOfShows extends StatelessWidget {
  final String listName;
  final List<Show> shows;

  const ListOfShows(this.listName, this.shows);

  @override
  Widget build(BuildContext context) {
    return Wrap(children: shows.map((show) => showItem(show, context)).toList());
  }

  InkWell showItem(Show show, BuildContext context) {
    return InkWell(
        onTap: () async {
          await Navigator.of(context).push(MaterialPageRoute(
              builder: (context) => showDetails(show)));
        },
        child: Container(
              CachedNetworkImage(
                  imageUrl: show.portraitPoster
              ),
        ));
  }
}

The question stated is a bit unclear, but I'm going to answer it the best I can. If you want your widget to be able to update you need to make it Stateful .

Make your BookmarkedShows Widget Stateful:

class BookmarkedShows extends StatefulWidget {
  BookmarkedShows ({Key key}) : super(key: key); //Can also work without this line
  @override
  StatefulBookmarkedShows createState() => StatefulBookmarkedShows();
}
class StatefulBookmarkedShows extends State<BookmarkedShows> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => BookmarkShowsBloc()..add(LoadBookmarkedShows()),
      child: BlocBuilder<BookmarkShowsBloc, BookmarkedShowsState>(
          builder: (BuildContext context, BookmarkedShowsState state) {
              return ShowList("Bookmarked shows", state.shows)
          }),
    );
  }
}

On returning back to parent you could implement something like in this Flutter docs example which might help to update the parent when navigating back. The async method awaits a response back from the child(Navigator). When returning back to the Stateful parent you can call this like in the above mentioned async method:

LoadBookmarkedShows();
setState(() { });

I hope it works. Goodluck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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