import 'package:flutter/material.dart';import 'home.dart';import 'search.dart';import 'me.dart';import 'detail.dart';import 'news.dart';class MyBottomNavigationBar extends StatefulWidget {final int index;MyBottomNavigationBar({this.index = 0});@override_MyBottomNavigationBarState createState() =>_MyBottomNavigationBarState(this.index);}class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {_MyBottomNavigationBarState(index) {this._currentIndex = index;}int _currentIndex = 0;List _pageList = [HomePage(),SearchPage(),DetailPage(),NewsPage(),MePage(),];@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Lynn'),),floatingActionButton: FloatingActionButton(onPressed: () {setState(() {this._currentIndex = 2;});},child: Icon(Icons.add,color: Colors.white,),),floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,body: this._pageList[this._currentIndex],bottomNavigationBar: BottomNavigationBar(currentIndex: _currentIndex,onTap: (int index) {setState(() {if(index == 2) return; // 如果你想要独立这个按钮的事件, 可以把这个事件禁止,这样就不会出现该页面this._currentIndex = index;});},// iconSize: 20,// fixedColor: Colors.pink,type: BottomNavigationBarType.fixed, // 配置多个tabItemitems: [BottomNavigationBarItem(icon: Icon(Icons.home),label: '首页',),BottomNavigationBarItem(icon: Icon(Icons.search),label: '搜索',),BottomNavigationBarItem(icon: Icon(Icons.home, color: Colors.white.withOpacity(0.05)),label: '详情',),BottomNavigationBarItem(icon: Icon(Icons.forum),label: '新闻',),BottomNavigationBarItem(icon: Icon(Icons.near_me),label: '我的',),],),);}}
底部导航凹进去的布局:
import 'package:flutter/material.dart';import 'home.dart';import 'search.dart';import 'me.dart';import 'detail.dart';import 'news.dart';class MyBottomNavigationBar extends StatefulWidget {final int index;MyBottomNavigationBar({this.index = 0});@override_MyBottomNavigationBarState createState() =>_MyBottomNavigationBarState(this.index);}class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {_MyBottomNavigationBarState(index) {this._currentIndex = index;}int _currentIndex = 0;List _pageList = [HomePage(),SearchPage(),DetailPage(),NewsPage(),MePage(),];void onPageChanged(int value) {setState(() {this._currentIndex = value;});}Color getColor(int value) {return this._currentIndex == value? Theme.of(context).cardColor: Color(0XFFBBBBBB);}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Lynn'),),floatingActionButton: FloatingActionButton(onPressed: () {setState(() {this._currentIndex = 2;});},child: Icon(Icons.add,color: Colors.white,),),floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,body: this._pageList[this._currentIndex],// BottomAppBar 组件 配置 shape: CircularNotchedRectangle(), 这样就会在中间有个 凹进入的半圆用于放FloatingActionButton按钮bottomNavigationBar: BottomAppBar(color: Theme.of(context).accentColor,shape: CircularNotchedRectangle(),child: Padding(padding: EdgeInsets.fromLTRB(0, 6, 0, 6),child: Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.spaceAround,children: [GestureDetector(onTap: () {onPageChanged(0);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.home, color: getColor(0)),Text("首页", style: TextStyle(color: getColor(0)))],)),GestureDetector(onTap: () {onPageChanged(1);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.search, color: getColor(1)),Text("搜索", style: TextStyle(color: getColor(1)))],)),GestureDetector(onTap: () {onPageChanged(2);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.home, color: getColor(2)),Text("详情", style: TextStyle(color: getColor(2)))],)),GestureDetector(onTap: () {onPageChanged(3);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.forum, color: getColor(3)),Text("新闻", style: TextStyle(color: getColor(3)))],)),GestureDetector(onTap: () {onPageChanged(4);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.near_me, color: getColor(4)),Text("我的", style: TextStyle(color: getColor(4)))],)),],),),),);}}
底部导航左右滑动页面切换:
import 'package:flutter/material.dart';import 'home.dart';import 'search.dart';import 'me.dart';import 'detail.dart';import 'news.dart';class MyBottomNavigationBar extends StatefulWidget {final int index;MyBottomNavigationBar({this.index = 0});@override_MyBottomNavigationBarState createState() =>_MyBottomNavigationBarState(this.index);}class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {_MyBottomNavigationBarState(index) {this._currentIndex = index;}PageController pageController; // 控制器int _currentIndex = 0;List _pageList = <Widget>[HomePage(),SearchPage(),DetailPage(),NewsPage(),MePage(),];@overridevoid initState() {super.initState();// 初始化 pageController , 默认显示第一个页面pageController = PageController(initialPage: this._currentIndex);}// 定义页面切换方法void onPageChanged(int value) {setState(() {this._currentIndex = value;});}Color getColor(int value) {return this._currentIndex == value? Theme.of(context).cardColor: Color(0XFFBBBBBB);}// 点击 tabbar 切换页面 需要使用 pageController.animateToPage , 传递页面的索引值。void onTap(int value) {pageController.animateToPage(value,duration: const Duration(milliseconds: 100), curve: Curves.ease);}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Lynn'),),floatingActionButton: FloatingActionButton(onPressed: () {setState(() {onTap(2);});},child: Icon(Icons.add,color: Colors.white,),),floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,// 利用 PageView 实现页面左右滑动切换body: PageView(children: _pageList,onPageChanged: onPageChanged, // 切换页面的方法controller: pageController, // 页面控制器),bottomNavigationBar: BottomAppBar(color: Theme.of(context).accentColor,shape: CircularNotchedRectangle(), // 凹园child: Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10),child: Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.spaceAround,children: [// GestureDetector 手势检测器GestureDetector(// 定义点击事件onTap: () {onTap(0); // 调用方法},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.home, color: getColor(0)),Text("首页", style: TextStyle(color: getColor(0)))],)),GestureDetector(onTap: () {onTap(1);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.search, color: getColor(1)),Text("搜索", style: TextStyle(color: getColor(1)))],)),GestureDetector(onTap: () {onTap(2);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.home, color: Colors.transparent), // 设置透明Text("详情", style: TextStyle(color: getColor(2)))],)),GestureDetector(onTap: () {onTap(3);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.forum, color: getColor(3)),Text("新闻", style: TextStyle(color: getColor(3)))],)),GestureDetector(onTap: () {onTap(4);},child: Column(mainAxisSize: MainAxisSize.min,children: <Widget>[Icon(Icons.near_me, color: getColor(4)),Text("我的", style: TextStyle(color: getColor(4)))],)),],),),),);}}
