flutter實(shí)現(xiàn)底部導(dǎo)航欄
本文實(shí)例為大家分享了flutter實(shí)現(xiàn)底部導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
一.flutter底部導(dǎo)航欄常用組件BottomNavigationBar 屬性介紹
BottomNavigationBar({
? ? Key? key,
? ? required this.items, //必填項(xiàng),設(shè)置各個(gè)按鈕
? ? this.onTap, //點(diǎn)擊事件
? ? this.currentIndex = 0, //當(dāng)前選中item下標(biāo)
? ? this.elevation, //控制陰影高度
? ? this.type, //BottomNavigationBarType,默認(rèn) fixed,設(shè)置為 shifting 時(shí),需要設(shè)置選中樣式,和未選中樣式,提供一個(gè)特殊動(dòng)畫(huà)
? ? Color? fixedColor, //選中 item 填充色
? ? this.backgroundColor, //整個(gè)BottomNavigationBar 背景色
? ? this.iconSize = 24.0, //圖標(biāo)大小
? ? Color? selectedItemColor, //選中title填充色
? ? this.unselectedItemColor, //未選中title填充色
? ? this.selectedIconTheme, //選中item圖標(biāo)主題
? ? this.unselectedIconTheme, //未選中item圖標(biāo)主題
? ? this.selectedFontSize = 14.0, //選中title字體大小
? ? this.unselectedFontSize = 12.0, //未選中title字體大小
? ? this.selectedLabelStyle, //選中title樣式 TextStyle
? ? this.unselectedLabelStyle, //未選中title樣式 TextStyle
? ? this.showSelectedLabels, //是否展示選中title,默認(rèn)為true
? ? this.showUnselectedLabels, //是否展示未選中title,默認(rèn)為true
? ? this.mouseCursor, //鼠標(biāo)懸停
? ? this.enableFeedback,
? ? this.landscapeLayout,
? })?二.BottomNavigationBar的具體實(shí)現(xiàn)
1.創(chuàng)建四個(gè)頁(yè)面,分別為,首頁(yè),通訊錄,發(fā)現(xiàn)和我的,這里以homepage.dart為例,其他頁(yè)面只需要修改對(duì)應(yīng)內(nèi)容顯示即可,eg:
import 'package:flutter/material.dart';
?
class HomePage extends StatefulWidget{
?
? const HomePage({Key? key}) : super(key: key);
?
? @override
? State<StatefulWidget> createState()=>_HomePageState();
?
}
?
class _HomePageState extends State<HomePage>{
?
? @override
? Widget build(BuildContext context) {
? ? return const Center(
? ? ? child: Text(
? ? ? ? "主頁(yè)",
? ? ? ? style:TextStyle(
? ? ? ? ? color: Colors.black,
? ? ? ? ? fontSize: 20
? ? ? ? ),
? ? ? ),
? ? );
? }
?
}2.添加BottomNavigationBar,需要在主頁(yè)中實(shí)現(xiàn)BottomNavigationBar,eg:
import 'package:flutter/material.dart';
import 'findpage.dart';
import 'mypage.dart';
import 'contactpage.dart';
import 'homepage.dart';
?
class MainPage extends StatefulWidget{
? const MainPage({Key? key}) : super(key: key);
?
? @override
? State<StatefulWidget> createState()=>_MainPageState();
}
?
class _MainPageState extends State<MainPage>{
?
? var allPages=[HomePage(),ContactPage(),FindPage(),MyPage()];
? var currentIndex=0;
?
?
? @override
? Widget build(BuildContext context) {
? ? return Scaffold(
? ? ? appBar: AppBar(
? ? ? ? title: Text(
? ? ? ? ? "導(dǎo)航欄",
? ? ? ? ? style: TextStyle(
? ? ? ? ? ? color: Colors.black,
? ? ? ? ? ? fontSize: 30,
? ? ? ? ? ),
? ? ? ? ? textAlign: TextAlign.center,
? ? ? ? ),
? ? ? ),
? ? ? body: allPages[currentIndex],
? ? ? backgroundColor: Colors.green,
? ? ? bottomNavigationBar: BottomNavigationBar(
? ? ? ? currentIndex: currentIndex,
? ? ? ? type: BottomNavigationBarType.fixed,
? ? ? ? unselectedItemColor: Colors.grey,
? ? ? ? selectedItemColor: Colors.blue,
? ? ? ? /*unselectedLabelStyle:TextStyle(
? ? ? ? ? color: Colors.black
? ? ? ? ),*/
? ? ? ? items: [
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.home),
? ? ? ? ? ? ? label: "首頁(yè)",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.person),
? ? ? ? ? ? ? label: "通訊錄",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.find_in_page),
? ? ? ? ? ? ? label: "發(fā)現(xiàn)",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
?
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.flip_outlined),
? ? ? ? ? ? ? label: "我的",
? ? ? ? ? ? ? //backgroundColor:Colors.blue
? ? ? ? ? ),
? ? ? ? ],
?
? ? ? ? onTap: (index){
? ? ? ? ? setState(() {
? ? ? ? ? ? print("the index is :$index");
? ? ? ? ? ? currentIndex=index;
? ? ? ? ? });
? ? ? ? },
? ? ? ),
? ? );
? }
}三.實(shí)際效果展示,eg:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Flutter實(shí)戰(zhàn)之為照片添加顏色濾鏡
這篇文章我們將利用TweenAnimationBuilder來(lái)實(shí)現(xiàn)一個(gè)圖片調(diào)色的過(guò)渡動(dòng)畫(huà),從而實(shí)現(xiàn)為照片添加顏色濾鏡的效果,感興趣的可以了解一下2022-12-12
Android Studio 配置忽略文件的方法實(shí)現(xiàn)
這篇文章主要介紹了Android Studio 配置忽略文件的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android 物理游戲之重力系統(tǒng)開(kāi)發(fā)示例代碼
介紹Android 物理游戲之重力系統(tǒng),這里提供了詳細(xì)的資料整理,并附示例代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-08-08
Android自定義View實(shí)現(xiàn)打字機(jī)效果
最近在做Android開(kāi)發(fā)的時(shí)候,需要做類(lèi)似于打字機(jī)打字的效果,字一個(gè)一個(gè)地蹦出來(lái),顯示每一個(gè)字都帶有打字的聲音。現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-08-08
解決Could not find com.android.tools.build:gradle:3.0.0
這篇文章主要介紹了在Android Studio升級(jí)時(shí)碰到Could not find com.android.tools.build:gradle:3.0.0問(wèn)題的解決方法,需要的朋友跟隨小編一起看看吧2021-08-08
Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
這篇文章將介紹如何定制當(dāng)今主流的對(duì)話框,通過(guò)自定義dialog實(shí)現(xiàn)加載對(duì)話框效果,具體實(shí)現(xiàn)代碼大家通過(guò)本文學(xué)習(xí)吧2018-05-05
使用Android的OkHttp包實(shí)現(xiàn)基于HTTP協(xié)議的文件上傳下載
OkHttp(GitHub主頁(yè)https://github.com/square/okhttp)是近來(lái)人氣攀升的一款安卓第三方HTTP包,這里我們來(lái)講解一下如何使用Android的OkHttp包實(shí)現(xiàn)基于HTTP協(xié)議的文件上傳下載:2016-07-07
Android獲取、更改包名的小技巧分享(超實(shí)用)
這篇文章主要給大家分享介紹了關(guān)于利用Android更改包名的一個(gè)小技巧,通過(guò)這個(gè)方法大家可以很方便的修改包名,再也不用為頻繁修改包名而感到頭疼,文中還給大家分享利一個(gè)android獲取手機(jī)所有應(yīng)用包名的方法,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
viewPager+fragment刷新緩存fragment的方法
這篇文章主要介紹了viewPager+fragment刷新緩存fragment的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03

