更多文章请查看 lutter从入门 到精通

实现底部导航栏并点击切换页面可简述为有三种方式

  • TabBar + TabBarView
  • BottomNavigationBar + BottomNavigationBarItem
  • 自定义 BottomAppBar

flutter BottomNavigationBar

  • 一般来说,点击底部导航栏都是要进行页面我们需要动态的改变一些状态,所以,我们要继承自StatefulWidget.
  • bottomNavigationBar 是属于 Scaffold 中的一个位于底部的控件。通常和 BottomNavigationBarItem 配合 开发底部导航栏。

1 基本构造方法说明

1.1 BottomNavigationBar构造方法

  BottomNavigationBar({Key key,@required this.items,  this.onTap,this.currentIndex = 0,BottomNavigationBarType type,this.fixedColor,this.iconSize = 24.0,})
属性 值类型 说明
items BottomNavigationBarItem类型的List 底部导航栏的显示项
onTap ValueChanged < int > 点击导航栏子项时的回调
currentIndex int 当前显示项的下标
type BottomNavigationBarType 底部导航栏的类型,有fixed和shifting两个类型,显示效果不一样
fixedColor Color 底部导航栏type为fixed时导航栏的颜色,如果为空的话默认使用ThemeData.primaryColor
iconSize double BottomNavigationBarItem icon的大小

1.2 BottomNavigationBarItem

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

BottomNavigationBarItem的构造方法

  const BottomNavigationBarItem({@required this.icon,this.title,Widget activeIcon,this.backgroundColor,})
属性 值类型 说明
icon Widget 要显示的图标控件,一般都是Iocn
title Widget 要显示的标题控件,一般都是Text
activeIcon Widget 选中时要显示的icon,一般也是Icon
backgroundColor Color BottomNavigationBarType为shifting时的背景颜色

2 效果实现

import 'package:flutter/material.dart';/*** 有状态StatefulWidget*  继承于 StatefulWidget,通过 State 的 build 方法去构建控件*/
class BotomeMenumPage extends StatefulWidget {通过构造方法传值BotomeMenumPage();//主要是负责创建state@overrideBotomeMenumPageState createState() => BotomeMenumPageState();
}/*** 在 State 中,可以动态改变数据* 在 setState 之后,改变的数据会触发 Widget 重新构建刷新*/
class BotomeMenumPageState extends State<BotomeMenumPage> {BotomeMenumPageState();@overridevoid initState() {///初始化,这个函数在生命周期中只调用一次super.initState();}@overrideWidget build(BuildContext context) {//构建页面return buildBottomTabScaffold();}//当前显示页面的int currentIndex = 0;//底部导航栏显示的内容final List<BottomNavigationBarItem> bottomNavItems = [BottomNavigationBarItem(backgroundColor: Colors.blue,icon: Icon(Icons.home),title: Text("首页"),),BottomNavigationBarItem(backgroundColor: Colors.blue[600],icon: Icon(Icons.format_indent_increase),title: Text("发现"),),BottomNavigationBarItem(backgroundColor: Colors.blue[800],icon: Icon(Icons.message),title: Text("动态"),),BottomNavigationBarItem(backgroundColor: Colors.blue[900],icon: Icon(Icons.person),title: Text("我的"),),];//点击导航项是要显示的页面final pages = [ChildItemView("首页"),ChildItemView("发现"),ChildItemView("动态"),ChildItemView("我的")];Widget buildBottomTabScaffold() {return Scaffold(bottomNavigationBar: BottomNavigationBar(items: bottomNavItems,currentIndex: currentIndex,//所以一般都是使用fixed模式,此时,导航栏的图标和标题颜色会使用fixedColor指定的颜色,// 如果没有指定fixedColor,则使用默认的主题色primaryColortype: BottomNavigationBarType.fixed,//底部菜单点击回调onTap: (index) {_changePage(index);},),//对应的页面body: pages[currentIndex],);}/*切换页面*/void _changePage(int index) {/*如果点击的导航项不是当前项  切换 */if (index != currentIndex) {setState(() {currentIndex = index;});}}
}

对应的页面View


//子页面
class ChildItemView extends StatefulWidget {String _title;ChildItemView(this._title);@override_ChildItemViewState createState() => _ChildItemViewState();
}class _ChildItemViewState extends State<ChildItemView> {@overrideWidget build(BuildContext context) {return Container(child: Center(child: Text(widget._title)),);}
}

flutter底部导航栏相关推荐

  1. Flutter底部导航栏BottomNavigationBar页面状态保持解决方案

    网易云[玩转大前端]配套课程 EDU配套 教程 Flutter开发的点滴积累系列文章 一行代代码置灰应用?对就是这么干 在实际应用开发中,一般应用的首页面会有这种结构,在Flutter应用开发中,有多 ...

  2. Flutter 底部导航栏(Tab 页)的快速实现

    Flutter 底部导航栏(Tab 页)的快速实现 我们知道 Scaffold 实现了基本的 Material 布局.只要是在 Material 中定义了的单个界面显示的布局控件元素,都可以使用 Sc ...

  3. Flutter底部导航栏BottomNavigationBar

    BottomNavigationBar是底部的导航栏,一般应用在多个视图进行选择.类比于Android的底部导航栏,由Text文本和Icon图标组成. 这里创建一个List为显示内容提供容器: sta ...

  4. Flutter底部导航栏的实现

    效果 实现 先将自动生成的main.dart里面的代码删除,并创建app.dart文件作为app的入口文件 import 'package:flutter/material.dart'; import ...

  5. Flutter - 底部导航详解与案例示范

    Flutter - 底部导航栏解析与示范 作者: jcLee95 邮箱 :291148484@163.com CSDN 主页:https://blog.csdn.net/qq_28550263?spm ...

  6. 【Flutter】底部导航栏页面框架 ( BottomNavigationBar 底部导航栏 | PageView 滑动页面 | 底部导航与滑动页面关联操作 )

    文章目录 一.BottomNavigationBar 底部导航栏 二.PageView 滑动页面 三.BottomNavigationBar 与 PageView 关联 四.完整代码示例 1.核心导航 ...

  7. 【Flutter】底部导航栏实现 ( BottomNavigationBar 底部导航栏 | BottomNavigationBarItem 导航栏条目 | PageView )

    文章目录 一.Scaffold 组件 二.底部导航栏整体架构 三.BottomNavigationBar 底部导航栏 四.BottomNavigationBarItem 导航栏条目 五.PageVie ...

  8. 【Flutter】StatefulWidget 组件 ( 底部导航栏组件 | BottomNavigationBar 组件 | BottomNavigationBarItem 组件 | 选项卡切换 )

    文章目录 一.BottomNavigationBar 组件 二.BottomNavigationBarItem 组件 三.BottomNavigationBar 底部导航栏代码示例 四.BottomN ...

  9. flutter TabBar 底部导航栏

    更多文章请查看 lutter从入门 到精通 实现底部导航栏并点击切换页面可简述为有三种方式 TabBar + TabBarView BottomNavigationBar + BottomNaviga ...

最新文章

  1. JavaScript语言基础10
  2. 【LeetCode从零单排】No83 Remove Duplicates from Sorted List
  3. 提高博客访问量14种方法
  4. 在vs2012下编译出现Msvcp120d.dll 丢失的问题
  5. leetcode最大矩形 (动态规划 python)
  6. java员工信息管理_基于jsp的员工信息管理-JavaEE实现员工信息管理 - java项目源码...
  7. mysqlplus 批量插入_ibatis结合oracle批量插入三种方法的测评
  8. hdu 1625(floyd判环)
  9. java两个数组中不同的数字_java将两个数组中不同的数字去掉一个剩下的保存到一个新的数组中...
  10. C++接收字符串数组_Java 中初始化数组
  11. openresty组成和技术特点
  12. web 前端(轮番插件)
  13. uu云验证码识别平台,验证码,验证码识别,全自动验证码识别技术,优优云全自动打码,代答题系统,优优云远程打码平台,uu云打码...
  14. 千年3 『自动杀猪·无限挂』千年3脚本 千年3外挂
  15. 旧电脑 软路由 openwrt 自定义安装ipk 操作记录
  16. 520情人节告白❤HTML+CSS+JavaScript实现抖音流动爱心
  17. OneZero第四周第一次站立会议(2016.4.11)
  18. 博士申请 | 华东师范大学张凯教授课题组招收图神经网络方向博士生
  19. MySQL更新数据流程
  20. 基于 SQL SERVER 的分布式数据库设计与实现

热门文章

  1. CVPR 2019 | 步步为营!通过迭代式模糊核预测提高超分辨质量
  2. mysql groupby rullup_关于group by的深入理解(扩展到rollup,增强groupby,主要用于小计)
  3. 计算机等级考试二级Python讲座(二)
  4. 由浅入深|让你彻底理解Python中的yield
  5. 画质评测|一次关于视频画质的探(zhǎn)讨(xiàn)
  6. 计算机视觉论文-2021-07-09
  7. 重读经典:完全解析特征学习大杀器 ResNet
  8. 速收藏,《机器学习实战》Python3环境算法实现代码
  9. 解决Python中字典出现“TypeError: 'dict_keys' object does not support indexing”错误
  10. 第5章 数据的共享与保护