Flutter - BottomNavigationBar底部导航栏切换后,状态丢失。底部

Posted 熊伟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter - BottomNavigationBar底部导航栏切换后,状态丢失。底部相关的知识,希望对你有一定的参考价值。

import package:flutter/material.dart;

import ./pages/home_page.dart;
import ./pages/book_page.dart;
import package:bottom_nav_bar_test/pages/movie_page.dart;
import package:bottom_nav_bar_test/pages/music_page.dart;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: Bottom Navigation,
      home: Main(),
      theme: ThemeData(primaryColor: Colors.orange),
    );
  }
}

class Main extends StatefulWidget {
  @override
  _MainState createState() => _MainState();
}

class _MainState extends State<Main> {
  int _currentIndex = 0;
  final List<Widget> _children = [Home(), Book(), Music(), Movie()];

  final List<BottomNavigationBarItem> _list = <BottomNavigationBarItem>[
    BottomNavigationBarItem(
        icon: Icon(Icons.home),
        title: Text(Home),
        //backgroundColor: Colors.orange
        ),
    BottomNavigationBarItem(
        icon: Icon(Icons.book),
        title: Text(Book),
        //backgroundColor: Colors.orange
        ),
    BottomNavigationBarItem(
        icon: Icon(Icons.music_video),
        title: Text(Music),
        //backgroundColor: Colors.orange
        ),
    BottomNavigationBarItem(
        icon: Icon(Icons.movie),
        title: Text(Movie),
        //backgroundColor: Colors.orange
        ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(Bottom Navigation),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: onTabTapped,
        currentIndex: _currentIndex,
        items: _list,
      ),
      //body: _children[_currentIndex],
      body: IndexedStack(
        index: _currentIndex,
        children: _children,
      ),
    );
  }

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }
}

 

以上是关于Flutter - BottomNavigationBar底部导航栏切换后,状态丢失。底部的主要内容,如果未能解决你的问题,请参考以下文章

Xcode 13 和 Flutter 2.5.1 - 致命错误:找不到“Flutter/Flutter.h”文件 #import <Flutter/Flutter.h>

Flutter 致命错误:找不到“Flutter/Flutter.h”文件

[Flutter] flutter项目一直卡在 Running Gradle task 'assembleDebug'...

flutter 日志输出,Flutter打印日志,flutter log,flutter 真机日志

Flutter开发 Flutter 包和插件 ( Flutter 包和插件简介 | 创建 Flutter 插件 | 创建 Dart 包 )

flutter与原生混编(iOS)