APP TabBar
主界面底部Tab导航
1. 在app主界面,tab底部导航栏是最常用的。通常基于Scaffold的bottomNavigationBar配合PageView使用。通过PageController控制PageView界面切换,同时使用BottomNavigationBar的currentIndex控制tab选中状态。
import 'package:flutter/material.dart';import 'package:./pages/course_page.dart';import 'package:./pages/home_page.dart';import 'package:./pages/exam_page.dart';import 'package:./pages/mine_page.dart';class MyApp extends StatelessWidget {// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(// This is the theme of your application.primarySwatch: Colors.blue,// This makes the visual density adapt to the platform that you run// the app on. For desktop platforms, the controls will be smaller and// closer together (more dense) than on mobile platforms.visualDensity: VisualDensity.adaptivePlatformDensity,),home: AppMainPage(),);}}class AppMainPage extends StatefulWidget {@override_AppMainPageState createState() => _AppMainPageState();}class _AppMainPageState extends State<AppMainPage> {int _selectedIndex;PageController _pageController;final _bottomBarItems = [BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),BottomNavigationBarItem(icon: Icon(Icons.book), title: Text('课程')),BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('考试')),BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('我的')),];final _pages = [HomePage(),CoursePage(),ExamPage(),MinePage(),];@overridevoid initState() {super.initState();_pageController = PageController();_selectedIndex = _pageController.initialPage;}@overridevoid dispose() {_pageController.dispose();super.dispose();}/// 点击 BottomNavigationBar item 回调void _onItemTapped(int index) {if (index == _selectedIndex) return;setState(() {_selectedIndex = index;_pageController.jumpToPage(index);});}@overrideWidget build(BuildContext context) {BottomNavigationBar _bottomNavigationBar = BottomNavigationBar(items: _bottomBarItems,type: BottomNavigationBarType.fixed,currentIndex: _selectedIndex,selectedItemColor: Colors.blue,onTap: _onItemTapped,unselectedItemColor: Colors.grey,unselectedFontSize: 13,selectedFontSize: 14,showUnselectedLabels: true,);return Scaffold(bottomNavigationBar: _bottomNavigationBar,body: PageView(controller: _pageController,physics: AlwaysScrollableScrollPhysics(),children: _pages,onPageChanged: (index) {if (_selectedIndex == index) return;setState(() {_selectedIndex = index;_bottomNavigationBar.onTap(index);});},));}}
2. 基于Scaffold的 bottomNavigationBar 的child为 TabBar 和 body 为TabBarView 控制tab选中及tabbarview滑动切换页面.
3. IndexedStack
import 'package:flutter/material.dart';import 'package:./pages/home_page.dart';import 'package:./pages/course_page.dart';import 'package:./pages/exam_page.dart';import 'package:./pages/mine_page.dart';class MyApp extends StatelessWidget {// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(// This is the theme of your application.primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,),home: AppMainPage(),);}}class AppMainPage extends StatefulWidget {@override_AppMainPageState createState() => _AppMainPageState();}class _AppMainPageState extends State<AppMainPage> {int _selectedIndex = 0;final bottomBarItems = [BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),BottomNavigationBarItem(icon: Icon(Icons.book), title: Text('课程')),BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('考试')),BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('我的')),];final pages = [HomePage(),CoursePage(),ExamPage(),MinePage(),];@overrideWidget build(BuildContext context) {return Scaffold(bottomNavigationBar: BottomNavigationBar(items: bottomBarItems,type: BottomNavigationBarType.fixed,currentIndex: _selectedIndex,selectedItemColor: Colors.blue,onTap: _onItemTapped,unselectedItemColor: Colors.grey,unselectedFontSize: 13,selectedFontSize: 14,showUnselectedLabels: true,),body: IndexedStack(index: _selectedIndex,children: pages,));}void _onItemTapped(int index) {setState(() {_selectedIndex = index;});}}
使用 PageView 和 TabBarView 其子页面(children)在每次切换页面都会再次刷新重载
- 每个子页面使用关键字 with 继承实现 AutomaticKeepAliveClientMixin
- 重写 wantKeepAlive 的 get 方法为true
- 在
Widget build(BuildContext context) {}方法中调用父类 build 方法super.build(context);
Flutter 三种方式实现页面切换后保持原页面状态
Flutter页面保活及保持页面跳转位置
路由 Route
图片资源
网络框架封装
- 网络状态获取监听
- 统一API接口设置
- 统一状态码提示
- get post
- 文件/图片上传
- 一组多个无序异步请求
- 一组多个有序请求
