網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了flutter實現(xiàn)底部導航欄的具體代碼,供大家參考,具體內容如下
一.flutter底部導航欄常用組件BottomNavigationBar 屬性介紹
BottomNavigationBar({ ? ? Key? key, ? ? required this.items, //必填項,設置各個按鈕 ? ? this.onTap, //點擊事件 ? ? this.currentIndex = 0, //當前選中item下標 ? ? this.elevation, //控制陰影高度 ? ? this.type, //BottomNavigationBarType,默認 fixed,設置為 shifting 時,需要設置選中樣式,和未選中樣式,提供一個特殊動畫 ? ? Color? fixedColor, //選中 item 填充色 ? ? this.backgroundColor, //整個BottomNavigationBar 背景色 ? ? this.iconSize = 24.0, //圖標大小 ? ? Color? selectedItemColor, //選中title填充色 ? ? this.unselectedItemColor, //未選中title填充色 ? ? this.selectedIconTheme, //選中item圖標主題 ? ? this.unselectedIconTheme, //未選中item圖標主題 ? ? this.selectedFontSize = 14.0, //選中title字體大小 ? ? this.unselectedFontSize = 12.0, //未選中title字體大小 ? ? this.selectedLabelStyle, //選中title樣式 TextStyle ? ? this.unselectedLabelStyle, //未選中title樣式 TextStyle ? ? this.showSelectedLabels, //是否展示選中title,默認為true ? ? this.showUnselectedLabels, //是否展示未選中title,默認為true ? ? this.mouseCursor, //鼠標懸停 ? ? this.enableFeedback, ? ? this.landscapeLayout, ? })?
二.BottomNavigationBar的具體實現(xiàn)
1.創(chuàng)建四個頁面,分別為,首頁,通訊錄,發(fā)現(xiàn)和我的,這里以homepage.dart為例,其他頁面只需要修改對應內容顯示即可,eg:
import 'package:flutter/material.dart'; ? class HomePage extends StatefulWidget{ ? ? const HomePage({Key? key}) : super(key: key); ? ? @override ? StatecreateState()=>_HomePageState(); ? } ? class _HomePageState extends State { ? ? @override ? Widget build(BuildContext context) { ? ? return const Center( ? ? ? child: Text( ? ? ? ? "主頁", ? ? ? ? style:TextStyle( ? ? ? ? ? color: Colors.black, ? ? ? ? ? fontSize: 20 ? ? ? ? ), ? ? ? ), ? ? ); ? } ? }
2.添加BottomNavigationBar,需要在主頁中實現(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 ? StatecreateState()=>_MainPageState(); } ? class _MainPageState extends State { ? ? var allPages=[HomePage(),ContactPage(),FindPage(),MyPage()]; ? var currentIndex=0; ? ? ? @override ? Widget build(BuildContext context) { ? ? return Scaffold( ? ? ? appBar: AppBar( ? ? ? ? title: Text( ? ? ? ? ? "導航欄", ? ? ? ? ? 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: "首頁", ? ? ? ? ? ? ? //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; ? ? ? ? ? }); ? ? ? ? }, ? ? ? ), ? ? ); ? } }
三.實際效果展示,eg:
原文鏈接:https://blog.csdn.net/j086924/article/details/122347292
相關推薦
- 2023-01-30 vite?+?react?+typescript?環(huán)境搭建小白入門教程_React
- 2022-06-07 關于python調用c++動態(tài)庫dll時的參數(shù)傳遞問題_C 語言
- 2022-09-24 如何將一個CSV格式的文件分割成兩個CSV文件_python
- 2022-09-23 python人工智能自定義求導tf_diffs詳解_python
- 2022-08-23 python實現(xiàn)GATK多線程加速示例_python
- 2022-04-16 pycharm如何設置自動生成作者信息_python
- 2022-03-15 linux系統(tǒng)中計劃任務介紹_Linux
- 2022-04-28 Python字符串的創(chuàng)建和駐留機制詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支