实现一个底部导航栏,包含3到4个功能标签,点击对应的导航标签可以切换到对应的页面内容,并且页面抬头显示的内容也会跟着改变。
实际上由于手机屏幕大小的限制,底部导航栏的功能标签一般在3到5个左右,如果太多,会比较拥挤,影响用户体验,实际上目前市面上大多数APP的底部导航标签都控制在4到5个左右。既美观、又不会让用户觉得功能繁杂。这个功能的实现需要用到flutter里的BottonNavigationBar这个控件。
属性名 类型 说明
currentIndex int 当前索引,用来切换按钮控制
fixedColor Color 选中按钮的颜色,如果没有指定值,则用系统主题色
iconSize double 按钮图标大小
items List<BottomNavigationBarItem> 底部导航条按钮集。每一项是一个BottomNavigationBarItem,有icon图标及title属性
onTap ValueChanged<int> 按下其中某一个按钮回调事件,需要根据返回的索引设置当前索引
首先我们新建一个底部导航控件,由于点击导航标签的时候页面内容是会发生改变的,所以这是一个有状态的控件。
import 'package:flutter/material.dart';
import './home.dart';
import './contacts.dart';
import './me.dart'; // 应用页面使用有状态Widget
class App extends StatefulWidget {
@override
AppState createState() => AppState();
}
//应用页面状态实现类
class AppState extends State<App> {
//当前选中页面索引
var _currentIndex = ;
//聊天页面
HomePage home;
//好友页面
Contacts contacts;
//我的页面
Personal me; //根据当前索引返回不同的页面
currentPage(){
switch(_currentIndex) {
case :
//返回聊天页面
if(home == null) {
home = new HomePage();
}
return home;
case :
//返回好友页面
if(contacts == null) {
contacts = new Contacts();
}
return contacts;
case :
//返回我的页面
if(me == null) {
me = new Personal();
}
return me;
}
} @override
Widget build(BuildContext context) { return Scaffold( //底部导航按钮
bottomNavigationBar: new BottomNavigationBar(
//通过fixedColor设置选中item的颜色
fixedColor:Colors.red,
type: BottomNavigationBarType.fixed,
//当前页面索引
currentIndex: _currentIndex,
//按下后设置当前页面索引
onTap: ((index){
setState(() {
_currentIndex = index;
});
}),
//底部导航按钮项
items: [
//导航按钮项传入文本及图标
new BottomNavigationBarItem(
title: new Text(
'聊天',
style: TextStyle(
color: _currentIndex == ? Color(0xFFF54343) : Color(0xff999999) //0x 后面开始 两位FF表示透明度16进制 后面是颜色
),
),
//判断当然索引做图片切换显示
icon: _currentIndex ==
? Image.asset(
'images/nav-icon-index.active.png',
width: 32.0,
height: 32.0,
)
: Image.asset(
'images/nav-icon-index.png',
width: 32.0,
height: 32.0,
)
),
new BottomNavigationBarItem(
title: new Text(
'好友',
style: TextStyle(
color: _currentIndex == ? Color(0xFFF54343) : Color(0xff999999)
),
),
//判断当然索引做图片切换显示
icon: _currentIndex ==
? Image.asset(
'images/nav-icon-cat.active.png',
width: 32.0,
height: 32.0,
)
: Image.asset(
'images/nav-icon-cat.png',
width: 32.0,
height: 32.0,
)
),
new BottomNavigationBarItem(
title: new Text(
'我的',
style: TextStyle(
color: _currentIndex == ? Color(0xFFF54343) : Color(0xff999999)
),
),
//判断当然索引做图片切换显示
icon: _currentIndex ==
? Image.asset(
'images/nav-icon-user.active.png',
width: 32.0,
height: 32.0,
)
: Image.asset(
'images/nav-icon-user.png',
width: 32.0,
height: 32.0,
)
),
],
), //中间显示当前页面
body: currentPage(), );
}
}
fixedColor设置选中的颜色,下面可以不单独写color。
 

新建三个dart文件,分别用于新建3个主体展示页面,这三个页面都是带一个appbar和body,appbar用于显示对应的导航标签,body里显示标签大图标。三个文件的写法基本是一致的。

import 'package:flutter/material.dart';

class Personal extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return new PersonalState();
}
} class PersonalState extends State<Personal>{
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('我的'),
),
body: new Center(
child: Icon(Icons.mood,size: 130.0,color: Colors.blue,),
),
);
}
}

另外一种实现方法,重新initState()方法:

List<Widget> blist = List();
@override
void initState(){
blist
..add(HomePage())
..add(Contacts())
..add(Personal());
super.initState();
}

这里的..add()是Dart语言的..语法,如果你学过编程模式,你一定听说过建造者模式,简单来说就是返回调用者本身。这里list后用了..add(),还会返回list,然后就一直使用..语法,能一直想list里增加widget元素。 最后我们调用了一些父类的initState()方法。

完整代码:

import 'package:flutter/material.dart';
import './home.dart';
import './contacts.dart';
import './me.dart'; class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
} class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('底部导航示例')),
body: BottomNavigationWidget(),
);
}
} class BottomNavigationWidget extends StatefulWidget { _BottomNavigationWidgetState createState() => _BottomNavigationWidgetState();
} class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
final _BottomNavigationColor = Colors.red;
int _currentIndex = ;
List<Widget> blist = List(); @override
void initState(){
blist
..add(HomePage()) //建造者模式,简单来说就是返回调用者本身。这里blist后用了..add(),还会返回blist
..add(Contacts())
..add(Personal());
super.initState();
} @override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar:BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home,color: _BottomNavigationColor,),
title:Text('Hmoe',style: TextStyle(color: _BottomNavigationColor),)
),
BottomNavigationBarItem(
icon: Icon(Icons.email,color: _BottomNavigationColor,),
title:Text('Email',style: TextStyle(color: _BottomNavigationColor),)
),
BottomNavigationBarItem(
icon: Icon(Icons.pages,color: _BottomNavigationColor,),
title:Text('Pages',style: TextStyle(color: _BottomNavigationColor),)
),
],
currentIndex: _currentIndex, //当前页面索引,高亮
onTap: (int index){
setState(() {
_currentIndex = index;
});
},
) ,
body: blist[_currentIndex],
);
}
}

Flutter 底部导航栏bottomNavigationBar的更多相关文章

  1. Android------底部导航栏BottomNavigationBar

    Android 的底部导航栏 BottomNavigationBar 由Google官方Material design中增加的. Android底部导航栏的实现方式特别多,例如TabHost,TabL ...

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

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

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

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

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

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

  5. Flutter - 创建底部导航栏

    之前写过的一篇文章介绍了 Flutter - 创建横跨所有页面的侧滑菜单, 这次就一起来学习一下底部导航栏. 底部导航栏在ios平台上非常常见,app store就是这样的风格.还有就是大家最常用的微 ...

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

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

  7. Flutter移动电商实战 --(3)底部导航栏制作

    1.cupertino_IOS风格介绍 在Flutter里是有两种内置风格的: material风格: Material Design 是由 Google 推出的全新设计语言,这种设计语言是为手机.平 ...

  8. 底部导航栏使用BottomNavigationBar

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

  9. Flutter实战视频-移动电商-03.底部导航栏制作

    03.底部导航栏制作 material是谷歌退出的 还有另外的一种:cupertino是IOS的风格 我们底部的导航栏,静态的widget是不合适的,这垃圾我们用到动态的widget 这重新改成动态的 ...

随机推荐

  1. C# 之 .net core -- MVC模式的显示、增、删、改

    上一篇介绍数据的创建连接,接下来就是基本的增删改 一.右键添加.控制器 二.选择这个带试图的 三.其他的不要动,点击添加 四.执行一下,改下url 试一下他的增删改. 其实自己写的话可以用form表单 ...

  2. WCF Windows基础通信

    概述 WCF,Windows Communication Foundation ,Windows通信基础, 面向服务的架构,Service Orientation Architechture=SOP ...

  3. android-studio打包APK出现有关apk图标问题

    报的错很多,有build gradle中的两个大红感叹号,由此引发了一大堆问题 注意到最后出现红色打包错误的代码: Failed to read PNG signature: file does no ...

  4. C# List<T> 集合使用

    1.初始化集合 , ); , ); , ); ) { graham, emerson, mario }; 2. 添加元素 racers.Add(, )); racers.Add(, )); racer ...

  5. linux笔试题

    1. cron 后台常驻程序 (daemon) 用于: A. 负责文件在网络中的共享 B. 管理打印子系统 C. 跟踪管理系统信息和错误 D. 管理系统日常任务的调度 2. 在大多数Linux发行版本 ...

  6. kafka 介绍与使用

    在介绍为什么使用kafka之前,我们有必要来了解一下什么是kafka? 1. 什么是kafka? Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率 ...

  7. 微信小程序之 map 地图使用

    1.在app.json中与pages平级的位置处,加上: "permission": { "scope.userLocation": { "desc& ...

  8. learning express step(四)

    learning express route function const express = require('express'); const app = express(); app.get(' ...

  9. 51nod 1060

    反素数定义:对于任意正整数 $n$, 其约数个数记为 $f(n)$, 如果某个正整数 $n$ 满足 对于任意正整数 $i, (0 < i < n)$, 都有 $f(i) < f(n) ...

  10. [Luogu] 开关

    https://www.luogu.org/problemnew/show/P3870 线段树区间翻转 + 区间查询 #include <iostream> #include <cs ...