flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄
本文實(shí)例為大家分享了flutter實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)底部導(dǎo)航欄并點(diǎn)擊切換頁(yè)面可簡(jiǎn)述為有三種方式
- TabBar + TabBarView
- BottomNavigationBar + BottomNavigationBarItem
- 自定義 BottomAppBar
在這里 使用 BottomAppBar 來(lái)實(shí)現(xiàn)

/**
* 有狀態(tài)StatefulWidget
* 繼承于 StatefulWidget,通過 State 的 build 方法去構(gòu)建控件
*/
class BotomeMenumBarPage extends StatefulWidget {
////通過構(gòu)造方法傳值
BotomeMenumBarPage();
//主要是負(fù)責(zé)創(chuàng)建state
@override
BotomeMenumBarPageState createState() => BotomeMenumBarPageState();
}
/**
* 在 State 中,可以動(dòng)態(tài)改變數(shù)據(jù)
* 在 setState 之后,改變的數(shù)據(jù)會(huì)觸發(fā) Widget 重新構(gòu)建刷新
*/
class BotomeMenumBarPageState extends State<BotomeMenumBarPage> {
BotomeMenumBarPageState();
@override
void initState() {
///初始化,這個(gè)函數(shù)在生命周期中只調(diào)用一次
super.initState();
}
@override
Widget build(BuildContext context) {
//構(gòu)建頁(yè)面
return buildBottomTabScaffold();
}
//當(dāng)前顯示頁(yè)面的
int currentIndex = 0;
//點(diǎn)擊導(dǎo)航項(xiàng)是要顯示的頁(yè)面
final pages = [
ChildItemView("首頁(yè)"),
ChildItemView("發(fā)現(xiàn)"),
ChildItemView("動(dòng)態(tài)"),
ChildItemView("我的")
];
Widget buildBottomTabScaffold() {
return SizedBox(
height: 100,
child: Scaffold(
//對(duì)應(yīng)的頁(yè)面
body: pages[currentIndex],
//appBar: AppBar(title: const Text('Bottom App Bar')),
//懸浮按鈕的位置
floatingActionButtonLocation:
FloatingActionButtonLocation.centerDocked,
//懸浮按鈕
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {
print("add press ");
},
),
//其他菜單欄
bottomNavigationBar: BottomAppBar(
//懸浮按鈕 與其他菜單欄的結(jié)合方式
shape: CircularNotchedRectangle(),
// FloatingActionButton和BottomAppBar 之間的差距
notchMargin: 6.0,
color: Colors.white,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
buildBotomItem(currentIndex, 0, Icons.home, "首頁(yè)"),
buildBotomItem(currentIndex, 1, Icons.library_music, "發(fā)現(xiàn)"),
buildBotomItem(currentIndex, -1, null, "發(fā)現(xiàn)"),
buildBotomItem(currentIndex, 2, Icons.email, "消息"),
buildBotomItem(currentIndex, 3, Icons.person, "我的"),
],
),
),
));
}
// ignore: slash_for_doc_comments
/**
* @param selectIndex 當(dāng)前選中的頁(yè)面
* @param index 每個(gè)條目對(duì)應(yīng)的角標(biāo)
* @param iconData 每個(gè)條目對(duì)就的圖標(biāo)
* @param title 每個(gè)條目對(duì)應(yīng)的標(biāo)題
*/
buildBotomItem(int selectIndex, int index, IconData iconData, String title) {
//未選中狀態(tài)的樣式
TextStyle textStyle = TextStyle(fontSize: 12.0,color: Colors.grey);
MaterialColor iconColor = Colors.grey;
double iconSize=20;
EdgeInsetsGeometry padding = EdgeInsets.only(top: 8.0);
if(selectIndex==index){
//選中狀態(tài)的文字樣式
textStyle = TextStyle(fontSize: 13.0,color: Colors.blue);
//選中狀態(tài)的按鈕樣式
iconColor = Colors.blue;
iconSize=25;
padding = EdgeInsets.only(top: 6.0);
}
Widget padItem = SizedBox();
if (iconData != null) {
padItem = Padding(
padding: padding,
child: Container(
color: Colors.white,
child: Center(
child: Column(
children: <Widget>[
Icon(
iconData,
color: iconColor,
size: iconSize,
),
Text(
title,
style: textStyle,
)
],
),
),
),
);
}
Widget item = Expanded(
flex: 1,
child: new GestureDetector(
onTap: () {
if (index != currentIndex) {
setState(() {
currentIndex = index;
});
}
},
child: SizedBox(
height: 52,
child: padItem,
),
),
);
return item;
}
}
//子頁(yè)面
class ChildItemView extends StatefulWidget {
String _title;
ChildItemView(this._title);
@override
_ChildItemViewState createState() => _ChildItemViewState();
}
class _ChildItemViewState extends State<ChildItemView> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(child: Text(widget._title)),
);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android視頻播放事件響應(yīng)及相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下2017-08-08
Android?NDK開發(fā)(C語(yǔ)言--聯(lián)合體與枚舉)
這篇文章主要介紹了Android?NDK開發(fā)C語(yǔ)言聯(lián)合體與枚舉,共用體是一種特殊的數(shù)據(jù)類型,允許您在相同的內(nèi)存位置存儲(chǔ)不同的數(shù)據(jù)類型。您可以定義一個(gè)帶有多成員的共用體,但是任何時(shí)候只能有一個(gè)成員帶有值。下面詳細(xì)介紹該內(nèi)容,需要的朋友可以參考一下2021-12-12
解析android 流量監(jiān)測(cè)的實(shí)現(xiàn)原理
本篇文章是對(duì)android中流量監(jiān)測(cè)的實(shí)現(xiàn)原理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android對(duì)話框AlertDialog.Builder使用方法詳解
這篇文章主要介紹了Android對(duì)話框AlertDialog.Builder使用方法詳解的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android 將本地資源圖片轉(zhuǎn)換成Drawable,進(jìn)行設(shè)置大小操作
這篇文章主要介紹了Android 將本地資源圖片轉(zhuǎn)換成Drawable,進(jìn)行設(shè)置大小操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-08-08
Android筆記之:深入為從右向左語(yǔ)言定義復(fù)雜字串的詳解
本篇文章是對(duì)Android中為從右向左語(yǔ)言定義復(fù)雜字串進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù)的操作方法
這篇文章主要介紹了Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù),本次也是介紹了用另外一種方法來(lái)實(shí)現(xiàn)RecyclerView高效刷新數(shù)據(jù)的功能,需要的朋友可以參考下2022-10-10

