一,概述

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

二,Bar关键元素

  • BottomNavigationBar  

    • BottomNavigationBar 是属于 Scaffold 中的一个位于底部的控件。通常和 BottomNavigationBarItem 配合使用。
    • BottomNavigationBar构造方法
       BottomNavigationBar({
      Key key,
      @required this.items,
      this.onTap,
      this.currentIndex = ,
      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 = ;
    } /** 如果点击的导航页不是当前项,切换*/
    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],
    );
    }
    }

【Flutter学习】基本组件之BottomNavigationBar底部导航栏的更多相关文章

  1. Android学习总结——输入法将BottomNavigationBar(底部导航栏)顶上去的问题

    在应用清单中给当前<Activity>设置: android:windowSoftInputMode="adjustPan" 关于android:windowSoftI ...

  2. Flutter - BottomNavigationBar底部导航栏切换后,状态丢失

    如果你用过BottomNavigationBar.TabBar.还有Drawer,你就会发现,在切换页面之后,原来的页面状态就会丢失. 要是上一页有一个数据列表,很多数据,你滚动到了下头,切换页面后, ...

  3. 01 uni-app框架学习:项目创建及底部导航栏tabBar配置

    1.创建一个项目类型选择uniapp 2. pages里新建3个页面如下 3.在pages.json中配置底部导航tabBar 效果展示:

  4. Flutter - BottomNavigationBar底部导航栏切换后,状态丢失。底部

    import 'package:flutter/material.dart'; import './pages/home_page.dart'; import './pages/book_page.d ...

  5. 20个Flutter实例视频教程-第02节: 底部导航栏制作-2

    视频地址: https://www.bilibili.com/video/av39709290?p=2 博客地址: https://jspang.com/post/flutterDemo.html#t ...

  6. 底部导航栏使用BottomNavigationBar

    1.github地址 https://github.com/zhouxu88/BottomNavigationBar 2.基本使用 2,1添加依赖 implementation 'com.ashokv ...

  7. Flutter——BottomNavigationBar组件(底部导航栏组件)

    BottomNavigationBar常用的属性: 属性名 说明 items List<BottomNavigationBarItem> 底部导航条按钮集合 iconSize icon c ...

  8. Flutter 底部导航栏bottomNavigationBar

    实现一个底部导航栏,包含3到4个功能标签,点击对应的导航标签可以切换到对应的页面内容,并且页面抬头显示的内容也会跟着改变. 实际上由于手机屏幕大小的限制,底部导航栏的功能标签一般在3到5个左右,如果太 ...

  9. Flutter移动电商实战 --(4)打通底部导航栏

    关于界面切换以及底栏的实现可参考之前写的一篇文章:Flutter实 ViewPager.bottomNavigationBar界面切换 1.新建4个基本dart文件 在pages目录下,我们新建下面四 ...

随机推荐

  1. Git 中的一些其他常用命令

    1.查看提交的历史版本(git log) 我们可以使用 git log 命令来查看提交的历史版本. 默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面.每个版本都有 ...

  2. centos6.8下安装nginx

    我用的阿里云上的镜像,make的时候总是出错,后来说是gcc安装不完整,要重新用下面命令安装 下: yum install gcc gcc-c++ gcc-g77 接下来下载nginx用到的一些库 w ...

  3. 集训队8月9日(组合计数+容斥原理+Mobius函数)

    刷题数:4 今天看了组合计数+容斥原理+Mobius函数,算法竞赛进阶指南169~179页 组合计数 https://www.cnblogs.com/2462478392Lee/p/11328938. ...

  4. [CSP-S模拟测试]:虎(DFS+贪心)

    题目传送门(内部题15) 输入格式 第一行一个整数$n$,代表点数接下来$n-1$行,每行三个数$x,y,z$,代表点$i$与$x$之间有一条边,若$y$为$0$代表初始为白色,否则为黑色,若$z$为 ...

  5. Extjs的一些基础使用!

    一.获取元素(Getting Elements) 1. Ext.get() var el = Ext.getCmp('id');//获取元素,等同于document.getElementById('i ...

  6. MySQL操作数据库值mysql事务

    创建一个无参数的事务     注意要写START TRANSACTION或者是Begin;Mysql会默认直接执行一个单元 MYSQL默认是自动提交的,也就是你提交一个QUERY,它就直接执行!我们可 ...

  7. selenium,webdriver爬取斗鱼主播信息 实操

    from selenium import webdriver import time from bs4 import BeautifulSoup class douyuSelenium(): #初始化 ...

  8. Windows重置网络命令

    我们在日常使用电脑的时候会碰到网络异常,网络故障,想要针对性地去解决是很困难的. 有时候可能查遍了资料,花了大量时间,怎么搞都还是搞不定.所以,这次直接给大家分享一个通过重置网络来解决所有问题的方法. ...

  9. 【计算机网络】两个网络模型——OSI参考模型和TCP/IP模型

    计算机网络 两个网络模型 计算机网络模型 分层机制----规划通讯细节 层与层之间之间是独立的.屏蔽的,下层为上层提供服务. 一些概念 实体: 任何发送/接收信息的软件/硬件进程. 对等层: 两个不同 ...

  10. 【Nacos】数据一致性

    转自:https://blog.csdn.net/liyanan21/article/details/89320872 目录 一.Raft算法 二.Nacos中Raft部分源码 init() 1. 获 ...