【问题标题】:BottomNavigationBar without loading all pages at onceBottomNavigationBar 无需一次加载所有页面
【发布时间】:2021-06-22 20:58:29
【问题描述】:

我有一个带有 BottomNavigationBar 和 IndexedStack 的屏幕,它根据底部栏索引交换子视图。

一切都很好,除了所有四个页面是同时创建的,加载的数据非常繁重,应用程序会变慢几秒钟。

是否有允许我仅在点击相应选项卡时加载每个页面的设置?

【问题讨论】:

  • 你好,这个问题你解决了吗?
  • 很遗憾没有

标签: flutter tabs bottomnavigationview


【解决方案1】:

我建议删除IndexedStack 并制作小部件列表并根据索引呈现数据。

List<Widget> _pages = [Home(), Business(),School()];

在底部导航容器小部件上创建 Provider/Bloc 实例

return MultiProvider(
   providers [
     Provider<Something>(create: (_) => Something()),
   ],
   child :Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
    body: _pages[_selectedIndex],
    bottomNavigationBar: BottomNavigationBar(
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: 'Home',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.business),
        label: 'Business',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.school),
        label: 'School',
      ),
    ],
    currentIndex: _selectedIndex,
    selectedItemColor: Colors.amber[800],
    onTap: _onItemTapped,
  )
)

在每个页面绑定provider的consumer,调用你provider中的api

【讨论】:

  • 嗨,我试过了,但这会使每个屏幕在每次在底部导航中被选中时都重新加载,这不是我想要的......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多