日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

flutter實(shí)現(xiàn)底部導(dǎo)航欄_Android

作者:偉雪無痕 ? 更新時(shí)間: 2022-05-25 編程語言

本文實(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)畫
? ? 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è)頁面,分別為,首頁,通訊錄,發(fā)現(xiàn)和我的,這里以homepage.dart為例,其他頁面只需要修改對應(yīng)內(nèi)容顯示即可,eg:

import 'package:flutter/material.dart';
?
class HomePage extends StatefulWidget{
?
? const HomePage({Key? key}) : super(key: key);
?
? @override
? State createState()=>_HomePageState();
?
}
?
class _HomePageState extends State{
?
? @override
? Widget build(BuildContext context) {
? ? return const Center(
? ? ? child: Text(
? ? ? ? "主頁",
? ? ? ? style:TextStyle(
? ? ? ? ? color: Colors.black,
? ? ? ? ? fontSize: 20
? ? ? ? ),
? ? ? ),
? ? );
? }
?
}

2.添加BottomNavigationBar,需要在主頁中實(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 createState()=>_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(
? ? ? ? ? "導(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: "首頁",
? ? ? ? ? ? ? //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:

原文鏈接:https://blog.csdn.net/j086924/article/details/122347292

欄目分類
最近更新