4.flutter_demo之底部栏

4.flutter_demo之底部栏

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title:'Flutter Demo',
        theme: ThemeData.light(),
        home: LXTabBar(),
    );
  }
}

class LXTabBar extends StatefulWidget {
  @override
  _LXTabBarState createState() => _LXTabBarState();
}

class _LXTabBarState extends State<LXTabBar> {

  final _tabBarColor = Colors.blue;
  int _currentIndex = 0;
  List<Widget> list = List();

  @override
  void initState() {
    list..add(HomePage())..add(ChatPage())..add(MinePage());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: list[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
              icon:Icon(Icons.home, color: _tabBarColor),
              title: Text('首页', style: TextStyle(color: _tabBarColor))
          ),
          BottomNavigationBarItem(
              icon:Icon(Icons.chat, color: _tabBarColor),
              title: Text('消息', style: TextStyle(color: _tabBarColor))
          ),
          BottomNavigationBarItem(icon:Icon(Icons.person, color: _tabBarColor),
              title: Text('我的', style: TextStyle(color: _tabBarColor))
          )
        ],
        currentIndex: _currentIndex,
        onTap: (int index){
          setState(() {
            _currentIndex = index;
          });
        },
      )
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('首页'),),
      body: Center(child: Text('homePage'),)
    );
  }
}

class ChatPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('会话'),),
        body: Center(child: Text('chatPage'),)
    );
  }
}

class MinePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('我的'),),
        body: Center(child: Text('minePage'),)
    );
  }
}