Flutter: BottomNavigationBar rebuilds Page on change of tab

后端 未结 5 2000
小蘑菇
小蘑菇 2020-12-13 03:50

I have a problem with my BottomNavigationBar in Flutter. I want to keep my page alive if I change the tabs.

here my implementation

BottomNavigation

5条回答
  •  温柔的废话
    2020-12-13 04:16

    If, you just need to remember the scroll position inside a list, the best option is to simply use a PageStoreKey object for the key property:

      @override
      Widget build(BuildContext context) {
        return Container(
          child: ListView.builder(
            key: PageStorageKey('some-list-key'),
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: items.length,
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                onTap: () => _onElementTapped(index),
                child: makeCard(items[index])
              );
            },
          ),
        );
      }
    

    According to https://docs.flutter.io/flutter/widgets/PageStorageKey-class.html, this should work on ANY scrollable widget.

提交回复
热议问题