flutter控件练习demo地址:githubgit
Scaffold 实现了基本的Material Design布局结构github
在Material设计中定义的单个界面上的各类布局元素,在 Scaffold 中都有支持,好比 左边栏(Drawers)、snack bars、以及 (底部弹窗)bottom sheets。(底部导航栏)BottomNavigationBarbash
Scaffold 有下面几个主要属性:app
appBar:显示在界面顶部的一个 AppBarless
body:当前界面所显示的主要内容 Widgetide
floatingActionButton:Material设计中所定义的 FAB,界面的主要功能按钮函数
floatingActionButtonLocation:floatingActionButton 的位置布局
floatingActionButtonAnimator:floatingActionButton 的动画动画
drawer:左边侧边栏控件ui
endDrawer:右边侧边栏控件
backgroundColor: 内容的背景颜色,默认使用的是 ThemeData.scaffoldBackgroundColor 的值
bottomNavigationBar: 显示在页面底部的导航栏
bottomSheet: 底部弹出来的东西(基本上没用)
persistentFooterButtons:固定在下方显示的按钮,好比对话框下方的肯定、取消按钮 (基本上没用)
resizeToAvoidBottomPadding:控制界面内容 body 是否从新布局来避免底部被覆盖了,好比当键盘显示的时候,从新布局避免被键盘盖住内容。默认值为 true。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ScaffoldDemo(),
);
}
}
class ScaffoldDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: _appBar(),
body: _body(),
floatingActionButton: new FloatingActionButton(
tooltip: 'Add', // used by assistive technologies
child: new Icon(Icons.add),
onPressed: null,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
drawer: _drawer(context),
endDrawer: Drawer(child: Text("右抽屉")),
bottomNavigationBar: _BottomNavigationBar(),
);
}
AppBar _appBar() {
return new AppBar(
// 若是有 侧边栏 右边是默认的事条目,点击默认打开 左侧边栏。 若是没有侧边栏,就显示返回箭头
title: new Text('Example title'),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.search),
tooltip: 'Search', // 长按的一个提示
onPressed: null,
),
new IconButton(
icon: new Icon(Icons.access_time),
tooltip: 'time',
onPressed: null,
),
],
);
}
Drawer _drawer(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
),
child: Center(
child: SizedBox(
width: 60.0,
height: 60.0,
child: CircleAvatar(
child: Text('头像'),
),
),
),
),
ListTile(
title: Text('Item 1'),
leading: new CircleAvatar(
child: new Icon(Icons.school),
),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
leading: new CircleAvatar(
child: new Text('B2'),
),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 3'),
leading: new CircleAvatar(
child: new Icon(Icons.list),
),
onTap: () {
Navigator.pop(context);
},
),
],
),
);
}
Builder _body() {
return Builder(builder: (BuildContext context) {
return Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('显示snackbar (Text 没有 onPressed 属性)'),
onPressed: () {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('我是snackbar'),
));
},
),
RaisedButton(
child: Text('显示BottomSheet (Text 没有 onPressed 属性)'),
onPressed: () {
Scaffold.of(context).showBottomSheet((BuildContext context) {
return ListView(
padding: EdgeInsets.zero,
children: <Widget>[
ListTile(
title: Text('Item 1'),
leading: new CircleAvatar(
child: new Icon(Icons.school),
),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
leading: new CircleAvatar(
child: new Text('B2'),
),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 3'),
leading: new CircleAvatar(
child: new Icon(Icons.list),
),
onTap: () {
Navigator.pop(context);
},
),
],
);
});
},
),
],
),
);
});
}
}
// BottomNavigationBar 默认的实例
class _BottomNavigationBar extends StatefulWidget {
const _BottomNavigationBar() : super();
@override
State<StatefulWidget> createState() => _BottomNavigationBarFullDefault();
}
// BottomNavigationBar 默认的实例,有状态
class _BottomNavigationBarFullDefault extends State {
int _currentIndex = 1;
void _onItemTapped(int index) {
if(mounted) {
setState(() {
_currentIndex = index;
});
}
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed, // BottomNavigationBarType 中定义的类型,有 fixed 和 shifting 两种类型
iconSize: 24.0, // BottomNavigationBarItem 中 icon 的大小
currentIndex: _currentIndex, // 当前所高亮的按钮index
onTap: _onItemTapped, // 点击里面的按钮的回调函数,参数为当前点击的按钮 index
fixedColor: Colors.blue, // 若是 type 类型为 fixed,则经过 fixedColor 设置选中 item 的颜色
items: <BottomNavigationBarItem> [
BottomNavigationBarItem(
title: Text("主页"), icon: Icon(Icons.home)),
BottomNavigationBarItem(
title: Text("发现"), icon: Icon(Icons.search)),
BottomNavigationBarItem(
title: Text("个人"), icon: Icon(Icons.image)),
],
);
}
}
复制代码
ScaffoldState 能够代替打开侧边栏和snackbar
_scaffoldKey.currentState.openDrawer();默认打开左边侧边栏
_scaffoldKey.currentState.showSnackBar
import 'package:flutter/material.dart';
class ScaffoldStateDemo extends StatefulWidget {
@override
State<StatefulWidget> createState() => _ScaffoldStateDemo();
}
class _ScaffoldStateDemo extends State with SingleTickerProviderStateMixin {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text("ScaffoldStateDemo"),
),
body: RaisedButton(
child: Text('显示snackbar'),
onPressed: _showSnackBar,
),
);
}
_showSnackBar() {
_scaffoldKey.currentState
.showSnackBar(SnackBar(content: Text("经过ScaffoldState打开的")));
}
}
复制代码