Losing widget state when switching pages in a Flutter PageView

前端 未结 3 2056
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 21:07

I have a series of stateful widgets in a PageView managed by a PageController. I am using pageController.jumpToPage(index) to switch pages. When switching pages

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 21:29

    keepPage: true is the default behavior; it means that the PageController will remember what page it's on if it is destroyed and recreated. This isn't what you want.

    Instead, pass a page-specific PageStorageKey to the constructor of the pages. This helps Flutter give a unique storage bucket to your page. Then in your State that you want to have restored to its previous condition, you can use PageStorage.of(context) to get the storage bucket, which you can read values from in initState and write values to when they change. You can see an example in ExpansionTile.

提交回复
热议问题