Linuxword Global
当前位置: 软件下载 > 如何在Flutter中實現花式底部導覽列?

Fancy Bottom NavigationBar是一個MaterialApp 小部件,它顯示一個底部花式導航欄,其中項目在2 到4 之間。Bottom NavigationBar 是水平形式的圖標或文本列表,稱為選項卡,每個選項卡都包含自己的頁面。

逐步實施
步驟1:在Android Studio中建立一個新項目
要在 Android Studio 上設定 Flutter 開發,請參閱Android Studio Flutter 開發設置,然後在 Android Studio 中建立新項目,請參閱 在 Flutter 中建立簡單應用程式。

步驟 2:匯入pubspec.yaml 檔案中的套件。
現在我們需要將套件匯入到 pubspec.yaml 檔案中,您可以在根資料夾的最後找到該檔案。

從命令列:
flutter pub 新增 fancy_bottom_navigation
這會將類似的內容添加到您的 pubspec.yaml 檔案中:

Dart

fnb1

dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
fancy_bottom_navigation: ^0.3.3dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
fancy_bottom_navigation: ^0.3.3
別忘了領取包裹。
注意: 當您新增依賴項時,插件的版本可能會變更。
第三步:將插件導入main.dart檔案中
import 'package:fancy_bottom_navigation/fancy_bottom_navigation.dart';
花式底部NavigationBar的一些要點:
選項卡資料:
取得顯示圖示的 iconData
標題要顯示的文字
onClick 操作 點選時執行某些任務。
初始選擇:
啟動時的活動頁面索引
圓圈顏色:
設定圓形圖示的背景。
非活動圖示顏色:
設定非選定圓圈通配圖示的背景。
欄背景顏色:
設定主欄的背景顏色
句法:
建立要顯示的PageIndex Var:

Creating PageIndex To Be Shown Var:
int currentPage = 0;
Creating StateKey Var:
GlobalKey bottom NavigationKey = GlobalKey();
Creating a Fancy BottomNavigationBar:
Dart
Scaffold(
bottomNavigationBar:FancyBottomNavigation(
initialSelection:0, key: bottomNavigationKey, circleColor:Colors.teal,
inactiveIconColor:Colors.white, barBackgroundColor:Colors.lightBlueAccent,
tabs: [
TabData(
iconData: Icons.add,
title: 'Add',
onclick: 00
), //You Can Add More
]
onTabChangedListener:(indexPage) {
setState() ({
currentPage = indexPage;
});
},
),
)
 
Code:Dart
import 'package:flutter/material.dart';
import 'package:fancy_bottom_navigation/fancy_bottom_navigation.dart';
 
void main() => runApp(MaterialApp(home: FancyBottomNavBarRun() ,));
 
class FancyBottomNavBarRun extends StatefulWidget {
const FancyBottomNavBarRun({Key? key}) : super(key: key);
 
@override
_FancyBottomNavBarState createState() => _FancyBottomNavBarState();
}
 
class _FancyBottomNavBarState extends State<FancyBottomNavBarRun> {
int currentPage = 0;
// CurrentPage that hold the current tab value
GlobalKey bottomNavigationKey = GlobalKey();
 
@override
Widget build(BuildContext context) {
return MaterialApp(
// MaterialApp with debugShowCheckedModeBanner false,
debugShowCheckedModeBanner:false,
home:Scaffold(
appBar: AppBar(
title: const Text("Fancy Bottom Navigation"),
),
body:Container(
width:MediaQuery.of(context).size.width,
height:MediaQuery.of(context).size.height,
child: // Showing the body according to the currentPage index.
(currentPage ==0 )?
AddPage():
(currentPage ==1 )?
ListPage():
ComparePage(),
),
// NavigationBar which have three tab, each tab have icons and color
bottomNavigationBar: FancyBottomNavigation(
initialSelection:0,
key: bottomNavigationKey,
circleColor:Colors.teal,
inactiveIconColor:Colors.white,
barBackgroundColor:Colors.lightBlueAccent,
tabs: [
TabData(
iconData: Icons.add,
title: "Add",
onclick: () {}
),
TabData(
iconData: Icons.list,
title: "List",
onclick: () {}
),
TabData(
iconData: Icons.compare_arrows,
title: "Compare",
onclick: () {}
),
],
// When tab changes we also called the setState method to change the currentpage
onTabChangedListener: (indexPage) {
setState(() {
currentPage = indexPage;
});
},
),
),
);
}
}
 
// Classes for showing in the body of the app according to the current page index.
class AddPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment:MainAxisAlignment.center,
crossAxisAlignment:CrossAxisAlignment.center,
children: [
const Icon(Icons.add, size: 30),
const Text("Add Page")
],
);
}
}
 
class ListPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment:MainAxisAlignment.center,
crossAxisAlignment:CrossAxisAlignment.center,
children: [
const Icon(Icons.list, size: 30),
const Text("List Page")
],
);
}
}
 
class ComparePage extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment:MainAxisAlignment.center,
crossAxisAlignment:CrossAxisAlignment.center,
children: [
const Icon(Icons.compare_arrows, size: 30),
const Text("Compare Page")
],
);
}
}
 

 

 

 

「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」

赞(0) 打赏
一分也是爱

支付宝扫一扫打赏

微信扫一扫打赏

上一篇:

下一篇:

相关推荐

博客简介

本站CDN采用VmShell免费提供离中国大陆最近的香港CMI高速网络做支撑,ToToTel打造全球最快速的边沿网络支撑服务,具体详情请见 :https://vmshell.com/ 以及 https://tototel.com/,网站所有的文件和内容禁止大陆网站搬迁复制,谢谢,VPS营销投稿邮箱: admin@linuxxword.com,我们免费帮大家发布,不收取任何费用,请提供完整测试文稿!

精彩评论

友情链接

他们同样是一群网虫,却不是每天泡在网上游走在淘宝和网游之间、刷着本来就快要透支的信用卡。他们或许没有踏出国门一步,但同学却不局限在一国一校,而是遍及全球!申请交换友链

站点统计

  • 文章总数: 2348 篇
  • 草稿数目: 13 篇
  • 分类数目: 6 个
  • 独立页面: 0 个
  • 评论总数: 2 条
  • 链接总数: 0 个
  • 标签总数: 6136 个
  • 注册用户: 139 人
  • 访问总量: 8,731,258 次
  • 最近更新: 2024年5月1日