首页 > 解决方案 > 我正在使用 BottomNavigationBar 导航到 diff SliverList。我观察到它不会保留到我滚动的最后一行。我怎样才能解决这个问题?

问题描述

class _HomeState extends State<Home> {
  int _currentIndex = 0;
  final List<Widget> _children = [Profile(), ServiceRequestList()];
  void _onTap(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: _children[_currentIndex],
        bottomNavigationBar: BottomNavigationBar(
          onTap: _onTap,
          currentIndex: _currentIndex,
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.account_circle),
              title: Text('Profile'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.assignment),
              title: Text('Service'),
            ),
          ],
        ));
  }
}

我正在使用 BottomNavigationBar 导航到 diff SliverList。我观察到它不会保留到我滚动的最后一行。如何解决这个问题?

标签: dartflutter

解决方案


PageStorageKey 就是您要找的东西!

在 Profile 页面和 ServiceRequestList 页面的 SliverList 小部件中,设置一个唯一的页面存储键,如下所示:

SliverList(
  key: PageStorageKey("somethingUnique"),
)

PageStorageKey 适用于任何具有滚动区域的小部件。


推荐阅读