Flutter 中底部导航栏 bottomNavigationB

2020-07-28  本文已影响0人  喜剧收尾_XWX
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: "底部标签栏",
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _selectIndex = 1;

  //导航栏中数据

  final _widgetOotions = [
    Text('index 0 : 信息'),
    Text('index 1 : 通讯录'),
    Text('index 2 : 发现'),
  ];

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('底部导航栏'),
      ),
      body: Center(
        child: _widgetOotions.elementAt(_selectIndex),
      ),
      //底部按钮
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.chat), title: Text('信息')),
          BottomNavigationBarItem(
              icon: Icon(Icons.contacts), title: Text('通讯录')),
          BottomNavigationBarItem(
              icon: Icon(Icons.access_alarm), title: Text('发现')),
        ],

        currentIndex: _selectIndex, //当前选中的索引
        fixedColor: Colors.lightBlue, //选中项的颜色
        onTap: _onItemTapped, //选择按下处理
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectIndex = index;
    });
  }
}

展示图片
上一篇下一篇

猜你喜欢

热点阅读