【Flutter学习】基本组件之BottomNavigationBar底部导航栏

一,概述

  BottomNavigationBar即是底部导航栏控件,显示在页面底部的设计控件,用于在试图切换,底部导航栏包含多个标签、图标或者两者搭配的形式,简而言之提供了顶级视图之间的快速导航。

二,Bar关键元素

  • BottomNavigationBar  
    • BottomNavigationBar 是属于 Scaffold 中的一个位于底部的控件。通常和 BottomNavigationBarItem 配合使用。
    • BottomNavigationBar构造方法
      复制代码
       BottomNavigationBar({
          Key key,
          @required this.items,  
          this.onTap,
          this.currentIndex = 0,
          BottomNavigationBarType type,
          this.fixedColor,
          this.iconSize = 24.0,
        })
      复制代码
    • BottomNavigationBar 参数含义



  • BottomNavigationBarItem
    • 底部导航栏要显示的Item,有图标和标题组成

    • BottomNavigationBarItem构造方法
        const BottomNavigationBarItem({
          @required this.icon,
          this.title,
          Widget activeIcon,
          this.backgroundColor,
        })
    • BottomNavigationBarItem 参数含义

三,实现过程

  • 1.构建底部标签
    复制代码
     // 导航图标
       final List<BottomNavigationBarItem> bottomNavItems = [
         new BottomNavigationBarItem(
           backgroundColor: Colors.blue,
           icon: Icon(Icons.home),
           title: new Text("首页")
         ),
    
         new BottomNavigationBarItem(
           backgroundColor: Colors.green,
           icon: Icon(Icons.message),
           title: new Text('消息')
         ),
    
         new BottomNavigationBarItem(
           backgroundColor: Colors.amber,
           icon: Icon(Icons.shopping_cart),
           title: new Text("购物车")
         ),
    
         new BottomNavigationBarItem(
           backgroundColor: Colors.red,
           icon: Icon(Icons.person),
           title: Text('个人中心')
         )
       ];
    复制代码
  • 2.构建导航显示的页面
     //视图view
        final pageViews = [
            new HomePage(),
            new MsgPage(),
            new CartPage(),
            new PersonPage()
        ];
  • 2.创建底部导航栏
    复制代码
          /** 如果点击的导航页不是当前项,切换*/
        void _changePage(int index) {
         if(index != currentIndex){
           setState(() {
             currentIndex = index;
           });
         }
        }
     
        @override
        Widget build(BuildContext context) {
        // TODO: implement build
        return new DefaultTabController(
          length: myTabs.length,
          child:  new Scaffold(
            appBar: new AppBar(
              title: new Text('顶部标签栏'),
              bottom: new TabBar(
                indicatorColor: Colors.blue,
                tabs: myTabs,
                isScrollable: true,
              ),
            ),
           
            bottomNavigationBar: new BottomNavigationBar(
              items: bottomNavItems,
              currentIndex: currentIndex,
              type: BottomNavigationBarType.fixed,
              onTap: (index) {
                 _changePage(index);
              },
            ), 
            body: pageViews[currentIndex],
          ),
      );
    }
     
    复制代码
  • 3.完成
    复制代码
    import 'package:flutter/material.dart';
    import './HomePage.dart';
    import './CartPage.dart';
    import './MsgPage.dart';
    import './PersonPage.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return new MaterialApp(
          title: '页面布局',
          theme:new ThemeData(
            primarySwatch: Colors.red
          ),
          home: new App(),
        );
      }
    }
    
    class App extends StatefulWidget {
    
      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return new AppState();
      }
    }
    
    class AppState extends State<App> {
    
        // 导航图标
       final List<BottomNavigationBarItem> bottomNavItems = [
         new BottomNavigationBarItem(
           backgroundColor: Colors.blue,
           icon: Icon(Icons.home),
           title: new Text("首页")
         ),
    
         new BottomNavigationBarItem(
           backgroundColor: Colors.green,
           icon: Icon(Icons.message),
           title: new Text('消息')
         ),
    
         new BottomNavigationBarItem(
           backgroundColor: Colors.amber,
           icon: Icon(Icons.shopping_cart),
           title: new Text("购物车")
         ),
    
         new BottomNavigationBarItem(
           backgroundColor: Colors.red,
           icon: Icon(Icons.person),
           title: Text('个人中心')
         )
       ];
    
       int currentIndex;
    
       //视图view
        final pageViews = [
            new HomePage(),
            new MsgPage(),
            new CartPage(),
            new PersonPage()
        ];
    
        @override
        void initState() {
          super.initState();
          currentIndex = 0;
        }
    
          /** 如果点击的导航页不是当前项,切换*/
        void _changePage(int index) {
         if(index != currentIndex){
           setState(() {
             currentIndex = index;
           });
         }
        }
     
        @override
        Widget build(BuildContext context) {
        // TODO: implement build
        return  new Scaffold(
            appBar: new AppBar(
              title: new Text('顶部标签栏'),
            ),
           
            bottomNavigationBar: new BottomNavigationBar(
              items: bottomNavItems,
              currentIndex: currentIndex,
              type: BottomNavigationBarType.fixed,
              onTap: (index) {
                 _changePage(index);
              },
            ), 
            body: pageViews[currentIndex],
        );
     }
    }
    复制代码

     

posted on   梁飞宇  阅读(1814)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示