簡體   English   中英

如何在 flutter 中保持列表視圖滾動 position

[英]How to keep list view scrolling position in flutter

在我的待辦事項應用程序中,我有兩個待辦事項列表底部導航欄項和待辦事項已完成列表..

我想回到我上次到達的列表視圖的 position,

簡而言之,我想繼續滾動 position 以便我可以從該 position 滾動,而不是從頭開始滾動,同時返回該導航欄項目。

這是我的代碼

讓我知道我應該為此學習什么主題...


class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int selectedindex=0;
  final tabs=[
    TodoList(),
    TodoCompletedList(),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Todo App'),),
        body:tabs[selectedindex],
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: Colors.white,
          unselectedItemColor: Colors.grey,
          selectedItemColor: Colors.blue,
          currentIndex: selectedindex,
          onTap: (index){
            selectedindex=index;
            setState(() {
            });
          },
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.fact_check_outlined),
                label: 'Todos'),
            BottomNavigationBarItem(
                label: 'Completed',
                icon: Icon(Icons.done))
          ],
        ),


    );
  }

}

class TodoList extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: 40,
        itemBuilder: (context,index){
          return ListTile(
            leading: CircleAvatar(),
            title: Text('Item No.'+(index+1).toString()),
          );
        });
  }
}


class TodoCompletedList extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: 10,
        itemBuilder: (context,index){
          return ListTile(
            leading: CircleAvatar(),
            title: Text('Item No.'+(index+1).toString()),
          );
        });
  }
}

我從堆棧溢出舊問題和答案中得到了答案

只需添加關鍵頁面存儲....就完成了

ListView.builder(
      key: PageStorageKey<String>('todolist'),
        itemCount: 40,
        itemBuilder: (context,index){
          return ListTile(
            leading: CircleAvatar(),
            title: Text('Item No.'+(index+1).toString()),
          );
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM