網站首頁 編程語言 正文
本文實例為大家分享了Flutter定義tabbar底部導航路由跳轉的具體代碼,供大家參考,具體內容如下
效果展示
整體實現的目錄結構
第一步 把三個頁面放到tabs里 Category.dart || Home.dart || Setting.dart
在這里我只展示?Home.dart 另外兩個頁面相同
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
? HomePage({Key key}) : super(key: key);
? @override
? _HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
? @override
? Widget build(BuildContext context) {
? ? return Text("我是首頁組件");
? }
}
在?Tabs.dart 里import 引入三個頁面
import 'package:flutter/material.dart';
import 'tabs/Home.dart';
import 'tabs/Category.dart';
import 'tabs/Setting.dart';
class Tabs extends StatefulWidget {
? Tabs({Key key}) : super(key: key);
? @override
? _TabsState createState() => _TabsState();
}
class _TabsState extends State<Tabs> {
? int _currentIndex = 0;
? // 把頁面存放到數組里
? List _pageList = [
? ? HomePage(),
? ? CategoryPage(),
? ? SettingPage(),
? ];
? @override
? Widget build(BuildContext context) {
? ? return Scaffold(
? ? ? appBar: AppBar(
? ? ? ? title: Text('首頁'),
? ? ? ),
? ? ? body: this._pageList[this._currentIndex],
? ? ? bottomNavigationBar: BottomNavigationBar(
? ? ? ? // 默認選中第幾項
? ? ? ? currentIndex: this._currentIndex,
? ? ? ? // 導航欄點擊獲取索引值
? ? ? ? onTap: (int index) {
? ? ? ? ? setState(() {
? ? ? ? ? ? this._currentIndex = index;
? ? ? ? ? });
? ? ? ? },
? ? ? ? iconSize: 30.0, //icon的大小
? ? ? ? fixedColor: Colors.red, //選中的顏色
? ? ? ? type: BottomNavigationBarType.fixed, //配置底部tabs可以有多個按鈕
? ? ? ? //定義導航欄的圖片+名稱
? ? ? ? items: [
? ? ? ? ? BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("首頁")),
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.category), title: Text("分類")),
? ? ? ? ? BottomNavigationBarItem(
? ? ? ? ? ? ? icon: Icon(Icons.settings), title: Text("設置")),
? ? ? ? ],
? ? ? ),
? ? );
? }
}
main.dart
import 'package:flutter/material.dart'; //快捷方式:fim
import 'pages/Tabs.dart';
void main() {
? runApp(MyApp());
}
//自定義組件
class MyApp extends StatelessWidget {
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? return MaterialApp(
? ? ? home: Tabs(),
? ? ? theme: ThemeData(primarySwatch: Colors.deepOrange),
? ? ? debugShowCheckedModeBanner: false,
? ? );
? }
}
原文鏈接:https://blog.csdn.net/AK852369/article/details/104815661
相關推薦
- 2023-03-11 C/C++?-?從代碼到可執行程序的過程詳解_C 語言
- 2022-08-01 C#線程池ThreadPool用法簡介_C#教程
- 2022-10-20 利用Python的tkinter模塊實現界面化的批量修改文件名_python
- 2022-05-10 Install fail Error: Unsupported URL Type “workspac
- 2022-01-15 Meteor 項目部署至服務器(windows)
- 2023-03-13 react的ui庫antd中form表單使用SelectTree反顯問題及解決_React
- 2022-06-19 C#?多線程中經常訪問同一資源可能造成哪些問題_C#教程
- 2023-04-07 C語言解決字符串中插入和刪除某段字符串問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支