Flutter:BottomNavigationBar在选项卡更改时重建页面

Flutter:BottomNavigationBar在选项卡更改时重建页面,第1张

Flutter:BottomNavigationBar在选项卡更改时重建页面

以前的答案都没有为我解决。

切换选项卡时使页面保持活动状态的解决方案是将页面包装在 IndexedStack中

class Tabbar extends StatefulWidget {  Tabbar({this.screens});  static const Tag = "Tabbar";  final List<Widget> screens;  @override  State<StatefulWidget> createState() {  return _TabbarState();  }}class _TabbarState extends State<Tabbar> {  int _currentIndex = 0;  Widget currentScreen;  @override  Widget build(BuildContext context) {    var _l10n = PackedLocalizations.of(context);    return Scaffold(  body: IndexedStack(    index: _currentIndex,    children: widget.screens,  ),  bottomNavigationBar: BottomNavigationBar(    fixedColor: Colors.black,    type: BottomNavigationBarType.fixed,    onTap: onTabTapped,    currentIndex: _currentIndex,    items: [      BottomNavigationBarItem(        icon: new Icon(Icons.format_list_bulleted),        title: new Text(_l10n.tripsTitle),      ),      BottomNavigationBarItem(        icon: new Icon(Icons.settings),        title: new Text(_l10n.settingsTitle),      )    ],  ),);  }  void onTabTapped(int index) {    setState(() {      _currentIndex = index;    });  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5051797.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存