
一个完整的路由页可能会包含导航栏、抽屉菜单(Drawer)以及底部Tab导航菜单等。如果每个路由页面都需要开发者自己手动去实现这些,这会是一件非常麻烦且无聊的事。幸运的是,Flutter Material组件库提供了一些现成的组件来减少我们的开发任务。Scaffold是一个路由页的骨架,我们使用它可以很容易地拼装出一个完整的页面。
构造函数
class Scaffold extends StatefulWidget {/// Creates a visual scaffold for material design widgets.const Scaffold({Key key,this.appBar, //标题栏this.body, //用于显示当前界面主要内容的Widgetthis.floatingActionButton, //悬浮在body上的按钮,默认显示在右下角this.floatingActionButtonLocation, //用于设置floatingActionButton显示的位置this.floatingActionButtonAnimator, //floatingActionButton移动到一个新的位置时的动画this.persistentFooterButtons, //多状态按钮this.drawer, //左侧抽屉this.endDrawer, //右侧抽屉this.bottomNavigationBar, //底部导航栏tabbarthis.bottomSheet, //显示在底部的工具栏this.backgroundColor,this.resizeToAvoidBottomPadding, //控制界面内容 body 是否重新布局来避免底部被覆盖,比如当键盘显示的时候,重新布局避免被键盘盖住内容this.resizeToAvoidBottomInset,this.primary = true, //Scaffold是否显示在页面的顶部this.drawerDragStartBehavior = DragStartBehavior.start,this.extendBody = false,this.extendBodyBehindAppBar = false,this.drawerScrimColor,this.drawerEdgeDragWidth,})
这里先上代码,以后再整理下



Main
import 'package:flutter/material.dart';import 'package:flutter_app_learn/MainPage.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(theme: ThemeData(primarySwatch: Colors.blue,),home: MainPage(),);}}
MainPage
import 'package:flutter/cupertino.dart';import 'package:flutter/material.dart';import 'package:flutter_app_learn/HomePage.dart';import 'package:flutter_app_learn/MinePage.dart';import 'package:flutter_app_learn/SchoolPage.dart';class MainPage extends StatefulWidget {@override_MainPageState createState() => _MainPageState();}class _MainPageState extends State<MainPage> {//默认选中int _selectedIndex = 0 ;//所有页面var _pageList = List();@overridevoid initState() {// TODO: implement initStatesuper.initState();_pageList = [new HomePaeg(), new SchoolPage(), new MinePage()];}@overrideWidget build(BuildContext context) {return Scaffold(bottomNavigationBar: BottomNavigationBar(items: <BottomNavigationBarItem>[BottomNavigationBarItem(icon: Icon(Icons.home),title: Text('首页')),BottomNavigationBarItem(icon: Icon(Icons.school),title: Text('学校')),BottomNavigationBarItem(icon: Icon(Icons.my_location),title: Text('我的'))],currentIndex: _selectedIndex,onTap: _onTap,),body: _pageList[_selectedIndex]);}void _onTap(int value) {setState(() {_selectedIndex = value;});}}
Home
import 'package:flutter/cupertino.dart';import 'package:flutter/material.dart';class HomePaeg extends StatefulWidget {@override_HomePaegState createState() => _HomePaegState();}class _HomePaegState extends State<HomePaeg> with SingleTickerProviderStateMixin {//必须实现SingleTickerProviderStateMixin协议TabController _tabController; //需要定义一个ControllerList tabs = ["北京", "济南", "上海",'济宁','天津'];@overridevoid dispose() {// TODO: implement disposesuper.dispose();}@overridevoid initState() {// TODO: implement initStatesuper.initState();_tabController = TabController(length: tabs.length, vsync: this);//监听_tabController.addListener( () {var index = _tabController.index;var previousIndex = _tabController.previousIndex;print("index: $index");});}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("首页"),actions: <Widget>[IconButton(icon: Icon(Icons.history),onPressed: () { print(111);})],//导航栏底部区域bottom: TabBar(controller: _tabController,tabs: tabs.map((e) => Tab(text: e)).toList())),//抽屉效果 (右上角默认添加打开抽屉的按钮)drawer: Drawer(child: Container(alignment: Alignment.center,child: Text('Hello Drawer'),),),body: ListView(children: <Widget>[ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),ListTile(title: Text('1111111')),Divider(color: Colors.grey, height: 0.5,),],),);}}
school
import 'package:flutter/cupertino.dart';import 'package:flutter/material.dart';class SchoolPage extends StatefulWidget {@override_SchoolPageState createState() => _SchoolPageState();}class _SchoolPageState extends State<SchoolPage> {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("学校"),),body: Container(alignment: Alignment.center,child: Column(children: <Widget>[Text('学校')],),),);}}
我和学校代码一样,文字不同.
