日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Flutter】StatefulWidget 组件 ( 底部导航栏组件 | BottomNavigationBar 组件 | BottomNavigationBarItem 组件 | 选项卡切换 )

發(fā)布時間:2025/6/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flutter】StatefulWidget 组件 ( 底部导航栏组件 | BottomNavigationBar 组件 | BottomNavigationBarItem 组件 | 选项卡切换 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、BottomNavigationBar 組件
  • 二、BottomNavigationBarItem 組件
  • 三、BottomNavigationBar 底部導航欄代碼示例
  • 四、BottomNavigationBar 底部導航欄選中狀態(tài)切換代碼示例
  • 五、BottomNavigationBar 底部導航欄切換選項卡界面
  • 六、 相關(guān)資源





一、BottomNavigationBar 組件



BottomNavigationBar 組件是底部導航欄 , 用于設(shè)置給 Scaffold 組件的 bottomNavigationBar 字段 ;

下面是 BottomNavigationBar 組件的構(gòu)造函數(shù)源碼 , 該構(gòu)造函數(shù)的可選參數(shù)列表就是可以設(shè)置的字段屬性 ;

class BottomNavigationBar extends StatefulWidget {/// Creates a bottom navigation bar which is typically used as a/// [Scaffold]'s [Scaffold.bottomNavigationBar] argument.////// The length of [items] must be at least two and each item's icon and title/// must not be null.////// If [type] is null then [BottomNavigationBarType.fixed] is used when there/// are two or three [items], [BottomNavigationBarType.shifting] otherwise.////// The [iconSize], [selectedFontSize], [unselectedFontSize], and [elevation]/// arguments must be non-null and non-negative.////// If [selectedLabelStyle.color] and [unselectedLabelStyle.color] values/// are non-null, they will be used instead of [selectedItemColor] and/// [unselectedItemColor].////// If custom [IconThemData]s are used, you must provide both/// [selectedIconTheme] and [unselectedIconTheme], and both/// [IconThemeData.color] and [IconThemeData.size] must be set.////// If both [selectedLabelStyle.fontSize] and [selectedFontSize] are set,/// [selectedLabelStyle.fontSize] will be used.////// Only one of [selectedItemColor] and [fixedColor] can be specified. The/// former is preferred, [fixedColor] only exists for the sake of/// backwards compatibility.////// The [showSelectedLabels] argument must not be non-null.////// The [showUnselectedLabels] argument defaults to `true` if [type] is/// [BottomNavigationBarType.fixed] and `false` if [type] is/// [BottomNavigationBarType.shifting].BottomNavigationBar({Key key,@required this.items,// 當前的若干 BottomNavigationBarItem 組件this.onTap,this.currentIndex = 0,// 當前選中條目 this.elevation = 8.0,BottomNavigationBarType type,Color fixedColor,this.backgroundColor,this.iconSize = 24.0,Color selectedItemColor,this.unselectedItemColor,this.selectedIconTheme = const IconThemeData(),this.unselectedIconTheme = const IconThemeData(),this.selectedFontSize = 14.0,this.unselectedFontSize = 12.0,this.selectedLabelStyle,this.unselectedLabelStyle,this.showSelectedLabels = true,bool showUnselectedLabels,}) }



二、BottomNavigationBarItem 組件



BottomNavigationBarItem 組件是 BottomNavigationBar 的 items 字段值 , 可以給該 items 字段設(shè)置多個 BottomNavigationBarItem 組件 ;


BottomNavigationBarItem 組件常用設(shè)置 :

  • 默認狀態(tài)圖標 : icon ;
  • 圖標下顯示的標題 : title ;
  • 激活狀態(tài)的圖標 : activeIcon ;
  • 背景顏色 : backgroundColor ;

BottomNavigationBarItem 組件構(gòu)造函數(shù)源碼 :

class BottomNavigationBarItem {/// Creates an item that is used with [BottomNavigationBar.items].////// The argument [icon] should not be null and the argument [title] should not be null when used in a Material Design's [BottomNavigationBar].const BottomNavigationBarItem({@required this.icon, // 默認狀態(tài)圖標this.title, // 圖標下顯示的標題Widget activeIcon, // 激活狀態(tài)的圖標 this.backgroundColor, // 背景顏色}) : activeIcon = activeIcon ?? icon,assert(icon != null); }



三、BottomNavigationBar 底部導航欄代碼示例



代碼示例 :

// 底部導航欄 BottomNavigationBar 設(shè)置// items 可以設(shè)置多個 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(items: [// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.home, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.home, color: Colors.red,),// 設(shè)置標題title: Text("主頁")),// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.settings, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.settings, color: Colors.red,),// 設(shè)置標題title: Text("設(shè)置"))],),

完整代碼示例 :

import 'package:flutter/material.dart';class StatefulWidgetPage extends StatefulWidget {@override_StatefulWidgetPageState createState() => _StatefulWidgetPageState(); }class _StatefulWidgetPageState extends State<StatefulWidgetPage> {// This widget is the root of your application.@overrideWidget build(BuildContext context) {// 文本組件樣式 , 可以設(shè)置給 Text 文本組件// 設(shè)置字體大小 20, 顏色紅色TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red);return MaterialApp(title: 'StatefulWidgetPage 組件示例',theme: ThemeData(primarySwatch: Colors.blue,),home: Scaffold(// 頂部標題欄appBar: AppBar(title: Text('StatefulWidgetPage 組件示例'),),// 底部導航欄 BottomNavigationBar 設(shè)置// items 可以設(shè)置多個 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(items: [// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.home, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.home, color: Colors.red,),// 設(shè)置標題title: Text("主頁")),// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.settings, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.settings, color: Colors.red,),// 設(shè)置標題title: Text("設(shè)置"))],),// Container 容器使用body: Container(// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 Text 文本組件children: <Widget>[],),),),);} }

運行效果 :





四、BottomNavigationBar 底部導航欄選中狀態(tài)切換代碼示例



BottomNavigationBar 底部導航欄每個 BottomNavigationBarItem 都有一個選中狀態(tài) , 通過 StatefulWidget 可以改變頁面狀態(tài) ;

設(shè)置一個成員變量 , 標識當前選中的索引值 ;

/// 當前被選中的底部導航欄索引int _currentSelectedIndex = 0;

將 BottomNavigationBar 組件的 currentIndex 設(shè)置為 _currentSelectedIndex 成員變量 ;

// 底部導航欄 BottomNavigationBar 設(shè)置// items 可以設(shè)置多個 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(// 設(shè)置當前選中的底部導航索引currentIndex: _currentSelectedIndex,)

設(shè)置 BottomNavigationBar 組件的 onTap 回調(diào)事件 , 傳入一個匿名回調(diào)函數(shù) , 在該匿名方法中回調(diào) StatefulWidget 組件的 setState 設(shè)置狀態(tài)的方法 , 修改當前選中索引 , 之后 BottomNavigationBar 組件會自動更新當前選中的選項卡 ;

// 底部導航欄 BottomNavigationBar 設(shè)置// items 可以設(shè)置多個 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(// 設(shè)置當前選中的底部導航索引currentIndex: _currentSelectedIndex,// 設(shè)置點擊底部導航欄的回調(diào)事件 , index 參數(shù)是點擊的索引值onTap: (index){// 回調(diào) StatefulWidget 組件的 setState 設(shè)置狀態(tài)的方法 , 修改當前選中索引// 之后 BottomNavigationBar 組件會自動更新當前選中的選項卡setState(() {// 改變 int _currentSelectedIndex 變量的狀態(tài)_currentSelectedIndex = index;});},)

完整代碼示例 :

import 'package:flutter/material.dart';class StatefulWidgetPage extends StatefulWidget {@override_StatefulWidgetPageState createState() => _StatefulWidgetPageState(); }class _StatefulWidgetPageState extends State<StatefulWidgetPage> {/// 當前被選中的底部導航欄索引int _currentSelectedIndex = 0;// This widget is the root of your application.@overrideWidget build(BuildContext context) {// 文本組件樣式 , 可以設(shè)置給 Text 文本組件// 設(shè)置字體大小 20, 顏色紅色TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red);return MaterialApp(title: 'StatefulWidgetPage 組件示例',theme: ThemeData(primarySwatch: Colors.blue,),home: Scaffold(// 頂部標題欄appBar: AppBar(title: Text('StatefulWidgetPage 組件示例'),),// 底部導航欄 BottomNavigationBar 設(shè)置// items 可以設(shè)置多個 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(// 設(shè)置當前選中的底部導航索引currentIndex: _currentSelectedIndex,// 設(shè)置點擊底部導航欄的回調(diào)事件 , index 參數(shù)是點擊的索引值onTap: (index){// 回調(diào) StatefulWidget 組件的 setState 設(shè)置狀態(tài)的方法 , 修改當前選中索引// 之后 BottomNavigationBar 組件會自動更新當前選中的選項卡setState(() {// 改變 int _currentSelectedIndex 變量的狀態(tài)_currentSelectedIndex = index;});},// 條目items: [// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.home, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.home, color: Colors.red,),// 設(shè)置標題title: Text("主頁")),// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.settings, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.settings, color: Colors.red,),// 設(shè)置標題title: Text("設(shè)置"))],),// Container 容器使用body: Container(// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 Text 文本組件children: <Widget>[],),),),);} }

運行效果 :





五、BottomNavigationBar 底部導航欄切換選項卡界面



BottomNavigationBar 底部導航欄的 onTap 回調(diào)方法中 , 設(shè)置當前選中的選項卡索引 , 根據(jù)該索引值修改 Scaffold 組件的 body 對應(yīng)組件 , 如果選項卡索引為 0 , 顯示組件 0 , 如果選項卡索引為 1 , 那么顯示組件 1 ;


設(shè)置 body 字段值時 , 根據(jù)當前的被中選的選項卡索引值 , 判斷應(yīng)該顯示哪個組件 ;

body: _currentSelectedIndex == 0 ? 組件0 : 組件1 ,

組件 0 :

Container( // 對應(yīng)底部導航欄主界面選項卡// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 Text 文本組件children: <Widget>[Text("主頁面選項卡")],),)

組件 1 :

Container( // 對應(yīng)底部導航欄設(shè)置選項卡// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 Text 文本組件children: <Widget>[Text("設(shè)置頁面選項卡")],),) , // 該設(shè)置與 _currentSelectedIndex == 0? 相對應(yīng), ?: 三目運算符

完整代碼 :

import 'package:flutter/material.dart';class StatefulWidgetPage extends StatefulWidget {@override_StatefulWidgetPageState createState() => _StatefulWidgetPageState(); }class _StatefulWidgetPageState extends State<StatefulWidgetPage> {/// 當前被選中的底部導航欄索引int _currentSelectedIndex = 0;// This widget is the root of your application.@overrideWidget build(BuildContext context) {// 文本組件樣式 , 可以設(shè)置給 Text 文本組件// 設(shè)置字體大小 20, 顏色紅色TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red);return MaterialApp(title: 'StatefulWidgetPage 組件示例',theme: ThemeData(primarySwatch: Colors.blue,),home: Scaffold(// 頂部標題欄appBar: AppBar(title: Text('StatefulWidgetPage 組件示例'),),// 底部導航欄 BottomNavigationBar 設(shè)置// items 可以設(shè)置多個 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(// 設(shè)置當前選中的底部導航索引currentIndex: _currentSelectedIndex,// 設(shè)置點擊底部導航欄的回調(diào)事件 , index 參數(shù)是點擊的索引值onTap: (index){// 回調(diào) StatefulWidget 組件的 setState 設(shè)置狀態(tài)的方法 , 修改當前選中索引// 之后 BottomNavigationBar 組件會自動更新當前選中的選項卡setState(() {// 改變 int _currentSelectedIndex 變量的狀態(tài)_currentSelectedIndex = index;});},// 條目items: [// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.home, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.home, color: Colors.red,),// 設(shè)置標題title: Text("主頁")),// 設(shè)置底部導航欄條目, 每個條目可以設(shè)置一個圖標BottomNavigationBarItem(// 默認狀態(tài)下的圖標icon: Icon(Icons.settings, color: Colors.grey,),// 激活狀態(tài)下的圖標activeIcon: Icon(Icons.settings, color: Colors.red,),// 設(shè)置標題title: Text("設(shè)置"))],),// Container 容器使用body:_currentSelectedIndex == 0 ?Container( // 對應(yīng)底部導航欄主界面選項卡// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 Text 文本組件children: <Widget>[Text("主頁面選項卡")],),):Container( // 對應(yīng)底部導航欄設(shè)置選項卡// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 Text 文本組件children: <Widget>[Text("設(shè)置頁面選項卡")],),) , // 該設(shè)置與 _currentSelectedIndex == 0? 相對應(yīng), ?: 三目運算符),);} }

運行效果 :





六、 相關(guān)資源



參考資料 :

  • Flutter 官網(wǎng) : https://flutter.dev/
  • Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強烈推薦 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社區(qū) : https://flutter.cn/
  • Flutter 實用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文檔 : https://dart.cn/
  • Dart 開發(fā)者官網(wǎng) : https://api.dart.dev/
  • Flutter 中文網(wǎng) ( 非官方 , 翻譯的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相關(guān)問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )

博客源碼下載 :

  • GitHub 地址 : https://github.com/han1202012/flutter_cmd ( 隨博客進度一直更新 , 有可能沒有本博客的源碼 )

  • 博客源碼快照 : https://download.csdn.net/download/han1202012/15484718 ( 本篇博客的源碼快照 , 可以找到本博客的源碼 )

總結(jié)

以上是生活随笔為你收集整理的【Flutter】StatefulWidget 组件 ( 底部导航栏组件 | BottomNavigationBar 组件 | BottomNavigationBarItem 组件 | 选项卡切换 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品久久久久9999爆乳 | 人人妻人人玩人人澡人人爽 | 亚洲精品乱码久久久久久蜜桃不卡 | 99热国| 福利片第一页 | 精品乱子伦 | 国产999久久久| 欧美aaa在线观看 | 蝌蚪自拍网站 | 国产一精品一aⅴ一免费 | 亚洲黄业| 香蕉视频在线观看免费 | 精品肉丝脚一区二区三区 | 一区二区三区国产 | www爱爱| 精品少妇人妻AV无码专区在线 | 久久久久97国产 | 欧美www视频 | 日韩中文字幕在线观看视频 | 岛国av动作片| 好大好爽好舒服 | 日韩人妻精品中文字幕 | 丁香花完整视频在线观看 | 熟女人妇 成熟妇女系列视频 | 国产欧美日韩在线视频 | 中文字幕三级 | 性视频网址| 欧亚免费视频 | 中文字幕福利视频 | 久久高清内射无套 | 免费观看黄色 | 黄频在线免费观看 | 黄色美女毛片 | av免费在线电影 | 日韩极品视频在线观看 | 欧美人成在线视频 | 亚洲av色区一区二区三区 | 精品三级在线观看 | 中文字幕一区二区三区5566 | 国产精品亚洲二区 | 久草视频免费在线 | 91亚洲精品久久久蜜桃借种 | 深爱激情五月婷婷 | 一区二区视频观看 | 精品人妻一区二区三区免费 | 午夜老司机免费视频 | 怎么可能高潮了就结束漫画 | 冲田杏梨一区二区三区 | 日韩和欧美一区二区 | 欧美操女人 | 日韩二区在线观看 | 一本一道波多野结衣一区二区 | 亚洲精品无码成人 | yes4444视频在线观看 | 亚洲国产精品激情在线观看 | 中文一区二区在线播放 | 自拍偷拍第 | 青青青在线视频 | 色妞色视频一区二区三区四区 | 欧美福利网 | 亚洲中文无码av在线 | 熟妇毛片 | 日批黄色片 | 东京av在线 | 国产精品第二页 | 日韩天堂在线视频 | 九九热在线精品视频 | 一区二区三区视频免费视 | 一本色道久久综合狠狠躁 | 日韩欧美亚洲一区二区 | 国产一区导航 | 亚洲国产毛片aaaaa无费看 | 亚洲激情三区 | 2019自拍偷拍| 中国在线观看免费视频 | zzjj国产精品一区二区 | 在线岛国| 中文字幕高潮 | 一区二区在线观看视频 | 欧美成人福利 | 亚洲精品乱码久久久久久国产主播 | 一本久久a精品一合区久久久 | 日韩不卡免费 | 毛片av在线观看 | 狠狠丁香| 无码人妻丰满熟妇区96 | 99这里只有精品 | 乖疼润滑双性初h | 97久久久| 日韩高清三区 | 五月开心网 | 91片黄在线观看 | 又紧又大又爽精品一区二区 | 男同互操gay射视频在线看 | 久久亚洲熟女cc98cm | 亚洲私人网站 | 精品亚洲国产成av人片传媒 | 尤物视频在线免费观看 | 蜜桃av免费 |