码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-04-15 14:30:07      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:color   less   fixed   contex   override   err   final   ati   title   

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底部导航栏切换后,状态丢失。底部

标签:color   less   fixed   contex   override   err   final   ati   title   

原文地址:https://www.cnblogs.com/xiongwei/p/10710319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!