APP TabBar

主界面底部Tab导航

1. 在app主界面,tab底部导航栏是最常用的。通常基于Scaffold的bottomNavigationBar配合PageView使用。通过PageController控制PageView界面切换,同时使用BottomNavigationBar的currentIndex控制tab选中状态。

  1. import 'package:flutter/material.dart';
  2. import 'package:./pages/course_page.dart';
  3. import 'package:./pages/home_page.dart';
  4. import 'package:./pages/exam_page.dart';
  5. import 'package:./pages/mine_page.dart';
  6. class MyApp extends StatelessWidget {
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. // This is the theme of your application.
  14. primarySwatch: Colors.blue,
  15. // This makes the visual density adapt to the platform that you run
  16. // the app on. For desktop platforms, the controls will be smaller and
  17. // closer together (more dense) than on mobile platforms.
  18. visualDensity: VisualDensity.adaptivePlatformDensity,
  19. ),
  20. home: AppMainPage(),
  21. );
  22. }
  23. }
  24. class AppMainPage extends StatefulWidget {
  25. @override
  26. _AppMainPageState createState() => _AppMainPageState();
  27. }
  28. class _AppMainPageState extends State<AppMainPage> {
  29. int _selectedIndex;
  30. PageController _pageController;
  31. final _bottomBarItems = [
  32. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
  33. BottomNavigationBarItem(icon: Icon(Icons.book), title: Text('课程')),
  34. BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('考试')),
  35. BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('我的')),
  36. ];
  37. final _pages = [
  38. HomePage(),
  39. CoursePage(),
  40. ExamPage(),
  41. MinePage(),
  42. ];
  43. @override
  44. void initState() {
  45. super.initState();
  46. _pageController = PageController();
  47. _selectedIndex = _pageController.initialPage;
  48. }
  49. @override
  50. void dispose() {
  51. _pageController.dispose();
  52. super.dispose();
  53. }
  54. /// 点击 BottomNavigationBar item 回调
  55. void _onItemTapped(int index) {
  56. if (index == _selectedIndex) return;
  57. setState(() {
  58. _selectedIndex = index;
  59. _pageController.jumpToPage(index);
  60. });
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. BottomNavigationBar _bottomNavigationBar = BottomNavigationBar(
  65. items: _bottomBarItems,
  66. type: BottomNavigationBarType.fixed,
  67. currentIndex: _selectedIndex,
  68. selectedItemColor: Colors.blue,
  69. onTap: _onItemTapped,
  70. unselectedItemColor: Colors.grey,
  71. unselectedFontSize: 13,
  72. selectedFontSize: 14,
  73. showUnselectedLabels: true,
  74. );
  75. return Scaffold(
  76. bottomNavigationBar: _bottomNavigationBar,
  77. body: PageView(
  78. controller: _pageController,
  79. physics: AlwaysScrollableScrollPhysics(),
  80. children: _pages,
  81. onPageChanged: (index) {
  82. if (_selectedIndex == index) return;
  83. setState(() {
  84. _selectedIndex = index;
  85. _bottomNavigationBar.onTap(index);
  86. });
  87. },
  88. )
  89. );
  90. }
  91. }

2. 基于Scaffold的 bottomNavigationBar 的child为 TabBar 和 body 为TabBarView 控制tab选中及tabbarview滑动切换页面.

使用 tabs

3. IndexedStack

  1. import 'package:flutter/material.dart';
  2. import 'package:./pages/home_page.dart';
  3. import 'package:./pages/course_page.dart';
  4. import 'package:./pages/exam_page.dart';
  5. import 'package:./pages/mine_page.dart';
  6. class MyApp extends StatelessWidget {
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. // This is the theme of your application.
  14. primarySwatch: Colors.blue,
  15. visualDensity: VisualDensity.adaptivePlatformDensity,
  16. ),
  17. home: AppMainPage(),
  18. );
  19. }
  20. }
  21. class AppMainPage extends StatefulWidget {
  22. @override
  23. _AppMainPageState createState() => _AppMainPageState();
  24. }
  25. class _AppMainPageState extends State<AppMainPage> {
  26. int _selectedIndex = 0;
  27. final bottomBarItems = [
  28. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
  29. BottomNavigationBarItem(icon: Icon(Icons.book), title: Text('课程')),
  30. BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('考试')),
  31. BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('我的')),
  32. ];
  33. final pages = [
  34. HomePage(),
  35. CoursePage(),
  36. ExamPage(),
  37. MinePage(),
  38. ];
  39. @override
  40. Widget build(BuildContext context) {
  41. return Scaffold(
  42. bottomNavigationBar: BottomNavigationBar(
  43. items: bottomBarItems,
  44. type: BottomNavigationBarType.fixed,
  45. currentIndex: _selectedIndex,
  46. selectedItemColor: Colors.blue,
  47. onTap: _onItemTapped,
  48. unselectedItemColor: Colors.grey,
  49. unselectedFontSize: 13,
  50. selectedFontSize: 14,
  51. showUnselectedLabels: true,
  52. ),
  53. body: IndexedStack(
  54. index: _selectedIndex,
  55. children: pages,
  56. )
  57. );
  58. }
  59. void _onItemTapped(int index) {
  60. setState(() {
  61. _selectedIndex = index;
  62. });
  63. }
  64. }

使用 PageView 和 TabBarView 其子页面(children)在每次切换页面都会再次刷新重载

  1. 每个子页面使用关键字 with 继承实现 AutomaticKeepAliveClientMixin
  2. 重写 wantKeepAlive 的 get 方法为true
  3. 在 Widget build(BuildContext context) {} 方法中调用父类 build 方法super.build(context);

Flutter 三种方式实现页面切换后保持原页面状态
Flutter页面保活及保持页面跳转位置

路由 Route

图片资源

网络框架封装

  1. 网络状态获取监听
  2. 统一API接口设置
  3. 统一状态码提示
  4. get post
  5. 文件/图片上传
  6. 一组多个无序异步请求
  7. 一组多个有序请求

json数据转模型

在 Flutter 中解析复杂的 JSON

“flutter”数据model及json处理

参考

面对Flutter,我终于迈出了第一步

An Introduction to MVVM in Flutter