flutter使用BottomNavigationBar 切换页面时重新加载的解决方案

直接上代码吧

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HomeState();
  }
}

class HomeState extends State<HomePage> {
  List<Widget> pages = List();
  var _currentIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    pages
      ..add(MusicPage())
      ..add(RadioPage())
      ..add(VideoPage())
      ..add(UserPage());
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        iconSize: 24,
        currentIndex: _currentIndex,
        fixedColor: Config.titleBarColor,
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.queue_music),
              title: Text(
                IntlUtil.getString(context, Ids.bottom_bar_music),
                style: TextStyle(fontSize: 14),
              )),
          BottomNavigationBarItem(
              icon: Icon(Icons.radio),
              title: Text(
                IntlUtil.getString(context, Ids.bottom_bar_radio),
                style: TextStyle(fontSize: 14),
              )),
          BottomNavigationBarItem(
              icon: Icon(Icons.ondemand_video),
              title: Text(
                IntlUtil.getString(context, Ids.bottom_bar_video),
                style: TextStyle(fontSize: 14),
              )),
          BottomNavigationBarItem(
              icon: Icon(Icons.supervised_user_circle),
              title: Text(
                IntlUtil.getString(context, Ids.bottom_bar_user),
                style: TextStyle(fontSize: 14),
              )),
        ],
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容