Flutter 基础控件 BottomNavigationBar

Flutter 基础控件 BottomNavigationBar

创建子页面

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

/// 搜索页面
class SearchPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("SEARCH"),),
      body: Center(
        child: Text("SEARCH"),
      ),
    );
  }
}

/// 邮箱页面
class EmailPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("EMAIL"),),
      body: Center(
        child: Text("EMAIL"),
      ),
    );
  }
}

创建一个状态可变的控件

/// 底部导航栏
class BottomNavigationDemo extends StatefulWidget {
  @override
  _BottomNavigationDemoState createState() {
    return _BottomNavigationDemoState();
  }
}
class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
  @override
  Widget build(BuildContext context) {
    return null;
    }
}

创建底部导航栏

     // 底部导航栏
      bottomNavigationBar: BottomNavigationBar(items: [
        BottomNavigationBarItem(
            icon: Icon(
              Icons.home,
              color: Colors.grey,
            ),
            title: Text("HOME", style: TextStyle(color: Colors.grey),),
            activeIcon: Icon(
              Icons.home,
              color: Colors.blue,
            ),
        ),
        BottomNavigationBarItem(
            icon: Icon(
              Icons.search,
              color: Colors.grey,
            ),
            title: Text("SEARCH", style: TextStyle(color: Colors.grey)),
            activeIcon: Icon(
              Icons.search,
              color: Colors.blue,
            ),
        ),
        BottomNavigationBarItem(
            icon: Icon(
              Icons.email,
              color: Colors.grey,
            ),
            title: Text("EMAIL", style: TextStyle(color: Colors.grey)),
            activeIcon: Icon(
              Icons.email,
              color: Colors.blue,
            )
        ),
      ],

设置底部导航栏状态,及相关属性

class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
  int currentPosition = 0;
  List<Widget> pageList = [];

  @override
  void initState() {
  	// 初始化添加页面
    pageList..add(HomePage())..add(SearchPage())..add(EmailPage());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: pageList[currentPosition],
      // 底部导航栏
      bottomNavigationBar: BottomNavigationBar(items: ...,
        fixedColor: Colors.blue,
        type: BottomNavigationBarType.fixed,
        // 当前位置
        currentIndex: currentPosition, 
        // item 点击事件
        onTap: (index) {
        	// 改变位置
          setState(() {
            currentPosition = index;
          });
        },
      ),
    );
  }
}

效果

在这里插入图片描述

自定义底部导航栏

/// 自定义底部导航栏
class CustomBottomNavigationDemo extends StatefulWidget {
  @override
  _CustomBottomNavigationDemoState createState() {
    return _CustomBottomNavigationDemoState();
  }
}

class _CustomBottomNavigationDemoState
    extends State<CustomBottomNavigationDemo> {
  int currentPosition = 0;
  List<Color> colorsList = [];
  List<Widget> widgetList = [];

  @override
  void initState() {
    super.initState();
    widgetList..add(NewPage("首页"))..add(NewPage("消息"));
    colorsList..add(Colors.blue)..add(Colors.grey);
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: widgetList[currentPosition],
      bottomNavigationBar: BottomAppBar(
        child: Row(
          // 在主轴方向占有空间的值,默认是max。
          mainAxisSize: MainAxisSize.max,
          // 主轴方向上的对齐方式,会对child的位置起作用,默认是start。
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            RaisedButton(
              color: Colors.white,
              elevation: 0,
              onPressed: () {
                setState(() {
                  currentPosition = 0;
                  colorsList[0] = Colors.blue;
                  colorsList[1] = Colors.grey;
                });
              },
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Icon(Icons.home, color: colorsList[0], size: 30,),
                  Text("HOME", style: TextStyle(color: colorsList[0]),)
                ],
              ),
            ),
            RaisedButton(
              color: Colors.white,
              elevation: 0,
              onPressed: () {
                setState(() {
                  currentPosition = 1;
                  colorsList[0] = Colors.grey;
                  colorsList[1] = Colors.blue;
                });
              },
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Icon(Icons.message, color: colorsList[1], size: 30,),
                  Text("Message", style: TextStyle(color: colorsList[1]),)
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class NewPage extends StatelessWidget {
  final String content;

  NewPage(this.content);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(content),),
      body: Center(
        child: Text(content, style: TextStyle(color: Colors.red),),
      ),
    );
  }
}

不规则底部导航栏

// 创建一个浮动按钮并显示它的位置
floatingActionButton: FloatingActionButton(onPressed: (){},
elevation: 0,
tooltip: "长按我就提示",
child: Icon(Icons.add,color:Colors.white),),
// 设置FloatingButton的显示
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

效果
在这里插入图片描述

遇到的问题

  • 页面切换回来后,发现页面被重置了
    body: pageList[currentPosition],
    // 修改为       
    body: IndexedStack(index: currentPosition,children: pageList,),
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值