Flutter - ListView inside on a TabBarView loses its scroll position

前端 未结 7 1529
误落风尘
误落风尘 2021-01-30 11:07

I have a very simple Flutter app with a TabBarView with two views (Tab 1 and Tab 2), one of them (Tab 1) has

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 11:16

    Output:


    Code:

    @override
    Widget build(BuildContext context) {
      return DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text("PageStorageKey"),
            bottom: TabBar(
              tabs: [
                Tab(icon: Icon(Icons.looks_one), text: "List1"),
                Tab(icon: Icon(Icons.looks_two), text: "List2"),
              ],
            ),
          ),
          body: TabBarView(
            children: [
              _buildList(key: "key1", string: "List1: "),
              _buildList(key: "key2", string: "List2: "),
            ],
          ),
        ),
      );
    }
    
    Widget _buildList({String key, String string}) {
      return ListView.builder(
        key: PageStorageKey(key),
        itemBuilder: (_, i) => ListTile(title: Text("${string} ${i}")),
      );
    }
    

提交回复
热议问题