Flutter学习第二课-基础组件 BottomNavigationBar

/*
BottomNavigationBar({
Key key,
@required this.items,
this.onTap,点击响应事件回调
this.currentIndex = 0,选中索引
this.elevation = 8.0,
BottomNavigationBarType type, 导航样式类型 fixed(等分选项): shifting(选中的选项加大占比);
Color fixedColor,BottomNavigationBarType.fixed 生效 并于 selectedItemColor 冲突 选项选中时图标和文本的颜色
this.backgroundColor,背景颜色
this.iconSize = 24.0,图标大小
Color selectedItemColor,选项选中时图标和文本的颜色
this.unselectedItemColor,选项未选中时图标和文本的颜色
this.selectedIconTheme = const IconThemeData(),
this.unselectedIconTheme = const IconThemeData(),
this.selectedFontSize = 14.0,选项选中时文本大小
this.unselectedFontSize = 12.0,选项未选中时文本大小
this.selectedLabelStyle,选项选中时文本样式
this.unselectedLabelStyle, 选项未选中时文本样式
this.showSelectedLabels = true, 选中的选项文本开启显示,flase 则不显示文本
bool showUnselectedLabels,//默认为flase 开启后未选中的选项文本开启显示
})
const BottomNavigationBarItem({
    @required this.icon,导航选项图标
    this.title, 导航选项文本
    Widget activeIcon, 当此底部导航项选中时替代的显示图标
    this.backgroundColor,  此底部导航项选中时替换的背景颜色
  })

*/
import 'package:flutter/material.dart';

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

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

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

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

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
  TextStyle(fontSize: 24, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Home',
      style: optionStyle,
    ),
    Text(
      'Index 1: Business',
      style: optionStyle,
    ),
    Text(
      'Index 2: School',
      style: optionStyle,
    ),
    Text(
      'Index 3: Hospital',
      style: optionStyle,
    ),
  ];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        unselectedItemColor: Colors.grey,
        onTap: _onItemTapped,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
//            backgroundColor: Colors.yellow,
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
//            backgroundColor: Colors.blue,
            icon: Icon(Icons.business),
            title: Text('Business'),
          ),
          BottomNavigationBarItem(
//            backgroundColor: Colors.purple,
            icon: Icon(Icons.school),
            title: Text('School'),
          ),
          BottomNavigationBarItem(
//            backgroundColor: Colors.red,
            activeIcon: Icon(Icons.local_hospital,color: Colors.red,),
            icon: Icon(Icons.local_hospital),
            title: Text('Hospital'),
          ),
        ],
      ),
    );
  }
}

常用方法已经添加了注释,可复制代码直接运行看效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值