新建项目-9.png

一个完整的路由页可能会包含导航栏、抽屉菜单(Drawer)以及底部Tab导航菜单等。如果每个路由页面都需要开发者自己手动去实现这些,这会是一件非常麻烦且无聊的事。幸运的是,Flutter Material组件库提供了一些现成的组件来减少我们的开发任务。Scaffold是一个路由页的骨架,我们使用它可以很容易地拼装出一个完整的页面。

构造函数

  1. class Scaffold extends StatefulWidget {
  2. /// Creates a visual scaffold for material design widgets.
  3. const Scaffold({
  4. Key key,
  5. this.appBar, //标题栏
  6. this.body, //用于显示当前界面主要内容的Widget
  7. this.floatingActionButton, //悬浮在body上的按钮,默认显示在右下角
  8. this.floatingActionButtonLocation, //用于设置floatingActionButton显示的位置
  9. this.floatingActionButtonAnimator, //floatingActionButton移动到一个新的位置时的动画
  10. this.persistentFooterButtons, //多状态按钮
  11. this.drawer, //左侧抽屉
  12. this.endDrawer, //右侧抽屉
  13. this.bottomNavigationBar, //底部导航栏tabbar
  14. this.bottomSheet, //显示在底部的工具栏
  15. this.backgroundColor,
  16. this.resizeToAvoidBottomPadding, //控制界面内容 body 是否重新布局来避免底部被覆盖,比如当键盘显示的时候,重新布局避免被键盘盖住内容
  17. this.resizeToAvoidBottomInset,
  18. this.primary = true, //Scaffold是否显示在页面的顶部
  19. this.drawerDragStartBehavior = DragStartBehavior.start,
  20. this.extendBody = false,
  21. this.extendBodyBehindAppBar = false,
  22. this.drawerScrimColor,
  23. this.drawerEdgeDragWidth,
  24. })

这里先上代码,以后再整理下

image.pngimage.pngimage.png

一共五个文件.

Main

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_app_learn/MainPage.dart';
  3. void main() => runApp(MyApp());
  4. class MyApp extends StatelessWidget {
  5. // This widget is the root of your application.
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. theme: ThemeData(
  10. primarySwatch: Colors.blue,
  11. ),
  12. home: MainPage(),
  13. );
  14. }
  15. }

MainPage

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_app_learn/HomePage.dart';
  4. import 'package:flutter_app_learn/MinePage.dart';
  5. import 'package:flutter_app_learn/SchoolPage.dart';
  6. class MainPage extends StatefulWidget {
  7. @override
  8. _MainPageState createState() => _MainPageState();
  9. }
  10. class _MainPageState extends State<MainPage> {
  11. //默认选中
  12. int _selectedIndex = 0 ;
  13. //所有页面
  14. var _pageList = List();
  15. @override
  16. void initState() {
  17. // TODO: implement initState
  18. super.initState();
  19. _pageList = [new HomePaeg(), new SchoolPage(), new MinePage()];
  20. }
  21. @override
  22. Widget build(BuildContext context) {
  23. return Scaffold(
  24. bottomNavigationBar: BottomNavigationBar(
  25. items: <BottomNavigationBarItem>[
  26. BottomNavigationBarItem(icon: Icon(Icons.home),title: Text('首页')),
  27. BottomNavigationBarItem(icon: Icon(Icons.school),title: Text('学校')),
  28. BottomNavigationBarItem(icon: Icon(Icons.my_location),title: Text('我的'))
  29. ],
  30. currentIndex: _selectedIndex,
  31. onTap: _onTap,
  32. ),
  33. body: _pageList[_selectedIndex]
  34. );
  35. }
  36. void _onTap(int value) {
  37. setState(() {
  38. _selectedIndex = value;
  39. });
  40. }
  41. }

Home

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class HomePaeg extends StatefulWidget {
  4. @override
  5. _HomePaegState createState() => _HomePaegState();
  6. }
  7. class _HomePaegState extends State<HomePaeg> with SingleTickerProviderStateMixin {
  8. //必须实现SingleTickerProviderStateMixin协议
  9. TabController _tabController; //需要定义一个Controller
  10. List tabs = ["北京", "济南", "上海",'济宁','天津'];
  11. @override
  12. void dispose() {
  13. // TODO: implement dispose
  14. super.dispose();
  15. }
  16. @override
  17. void initState() {
  18. // TODO: implement initState
  19. super.initState();
  20. _tabController = TabController(length: tabs.length, vsync: this);
  21. //监听
  22. _tabController.addListener( () {
  23. var index = _tabController.index;
  24. var previousIndex = _tabController.previousIndex;
  25. print("index: $index");
  26. });
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return Scaffold(
  31. appBar: AppBar(
  32. title: Text("首页"),
  33. actions: <Widget>[
  34. IconButton(icon: Icon(Icons.history),onPressed: () { print(111);})
  35. ],
  36. //导航栏底部区域
  37. bottom: TabBar(
  38. controller: _tabController,
  39. tabs: tabs.map((e) => Tab(text: e)).toList()
  40. )
  41. ),
  42. //抽屉效果 (右上角默认添加打开抽屉的按钮)
  43. drawer: Drawer(
  44. child: Container(
  45. alignment: Alignment.center,
  46. child: Text('Hello Drawer'),
  47. ),
  48. ),
  49. body: ListView(
  50. children: <Widget>[
  51. ListTile(title: Text('1111111')),
  52. Divider(color: Colors.grey, height: 0.5,),
  53. ListTile(title: Text('1111111')),
  54. Divider(color: Colors.grey, height: 0.5,),
  55. ListTile(title: Text('1111111')),
  56. Divider(color: Colors.grey, height: 0.5,),
  57. ListTile(title: Text('1111111')),
  58. Divider(color: Colors.grey, height: 0.5,),
  59. ListTile(title: Text('1111111')),
  60. Divider(color: Colors.grey, height: 0.5,),
  61. ListTile(title: Text('1111111')),
  62. Divider(color: Colors.grey, height: 0.5,),
  63. ListTile(title: Text('1111111')),
  64. Divider(color: Colors.grey, height: 0.5,),
  65. ListTile(title: Text('1111111')),
  66. Divider(color: Colors.grey, height: 0.5,),
  67. ListTile(title: Text('1111111')),
  68. Divider(color: Colors.grey, height: 0.5,),
  69. ListTile(title: Text('1111111')),
  70. Divider(color: Colors.grey, height: 0.5,),
  71. ListTile(title: Text('1111111')),
  72. Divider(color: Colors.grey, height: 0.5,),
  73. ListTile(title: Text('1111111')),
  74. Divider(color: Colors.grey, height: 0.5,),
  75. ListTile(title: Text('1111111')),
  76. Divider(color: Colors.grey, height: 0.5,),
  77. ListTile(title: Text('1111111')),
  78. Divider(color: Colors.grey, height: 0.5,),
  79. ListTile(title: Text('1111111')),
  80. Divider(color: Colors.grey, height: 0.5,),
  81. ListTile(title: Text('1111111')),
  82. Divider(color: Colors.grey, height: 0.5,),
  83. ListTile(title: Text('1111111')),
  84. Divider(color: Colors.grey, height: 0.5,),
  85. ListTile(title: Text('1111111')),
  86. Divider(color: Colors.grey, height: 0.5,),
  87. ListTile(title: Text('1111111')),
  88. Divider(color: Colors.grey, height: 0.5,),
  89. ListTile(title: Text('1111111')),
  90. Divider(color: Colors.grey, height: 0.5,),
  91. ListTile(title: Text('1111111')),
  92. Divider(color: Colors.grey, height: 0.5,),
  93. ListTile(title: Text('1111111')),
  94. Divider(color: Colors.grey, height: 0.5,),
  95. ListTile(title: Text('1111111')),
  96. Divider(color: Colors.grey, height: 0.5,),
  97. ],
  98. ),
  99. );
  100. }
  101. }

school

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class SchoolPage extends StatefulWidget {
  4. @override
  5. _SchoolPageState createState() => _SchoolPageState();
  6. }
  7. class _SchoolPageState extends State<SchoolPage> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. appBar: AppBar(
  12. title: Text("学校"),
  13. ),
  14. body: Container(
  15. alignment: Alignment.center,
  16. child: Column(
  17. children: <Widget>[
  18. Text('学校')
  19. ],
  20. ),
  21. ),
  22. );
  23. }
  24. }

我和学校代码一样,文字不同.