https://api.flutter.dev/flutter/material/Drawer-class.html
从Scaffold边缘水平滑动以显示应用程序中导航链接的Material Design面板。
在Scaffold 组件里面传入drawer 参数可以定义左侧边栏,传入endDrawer 可以定义右侧边
栏。侧边栏默认是隐藏的,我们可以通过手指滑动显示侧边栏,也可以通过点击按钮显示侧
边栏。
Drawer
Drawer({
Key key,
this.elevation = 16.0,
this.child,
this.semanticLabel,
})
示例:
class Root extends StatelessWidget {
List _data = generateWordPairs().take(20).map((e) => e.asPascalCase).toList();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('我是导航标题')),
body: MyHomePage(),
//drawer:Drawer() 左侧 endDrawer: Drawer() 右侧
drawer: Drawer(
child: Column(
children: [
Row(
children: [
Expanded(
child: DrawerHeader(
child: Text('sdfs'),
decoration: BoxDecoration(
color: Colors.green,
),
),
),
],
),
Expanded(
child: ListView.separated(
itemCount: 20,
itemBuilder: (context, index) {
return ListTile(
title: Text(_data[index]),
onTap: () {
Navigator.of(context).pop(); //关闭抽屉
},
);
},
separatorBuilder: (context, index) => Divider(height: 0.0),
),
),
],
),
),
);
}
}
DrawerHeader 侧边栏头部
DrawerHeader({
Key key,
this.decoration,
this.margin = const EdgeInsets.only(bottom: 8.0),
this.padding = const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 8.0),
this.duration = const Duration(milliseconds: 250),
this.curve = Curves.fastOutSlowIn,
@required this.child,
})
UserAccountsDrawerHeader
Row(
children: [
Expanded(
child: UserAccountsDrawerHeader(
accountName: Text('朱朝阳'), //账户名称
accountEmail: Text('zcy_2013@163.com'), //账户邮箱
currentAccountPicture: CircleAvatar( //用户头像
backgroundImage: AssetImage('assets/images/avatar.jpg'),
),
otherAccountsPictures: [ //其它头像组
Image.asset('assets/images/avatar.jpg'),
Image.asset('assets/images/1.jpg'),
Image.asset('assets/images/avatar.jpg'),
],
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/1.jpg'),
fit: BoxFit.cover,
),
),
),
),
],
),
api直接调用打开或关闭
onPressed: (){
//打开
Scaffold.of(context).openDrawer();
Scaffold.of(context).openEndDrawer();
//关闭
Navigator.of(context).pop();
}
import "package:flutter/material.dart";
class MyHomePage extends StatelessWidget {
_openDrawer(context) {
Scaffold.of(context).openDrawer(); //打开侧栏
}
@override
Widget build(BuildContext context) {
return Column(
children: [
RaisedButton(
child: Text('打开'),
onPressed: () => this._openDrawer(context),
),
],
);
}
}