https://api.flutter.dev/flutter/material/Drawer-class.html
从Scaffold边缘水平滑动以显示应用程序中导航链接的Material Design面板。

在Scaffold 组件里面传入drawer 参数可以定义左侧边栏,传入endDrawer 可以定义右侧边
栏。侧边栏默认是隐藏的,我们可以通过手指滑动显示侧边栏,也可以通过点击按钮显示侧
边栏。

Drawer

  1. Drawer({
  2. Key key,
  3. this.elevation = 16.0,
  4. this.child,
  5. this.semanticLabel,
  6. })

示例:

image.png

  1. class Root extends StatelessWidget {
  2. List _data = generateWordPairs().take(20).map((e) => e.asPascalCase).toList();
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(title: Text('我是导航标题')),
  7. body: MyHomePage(),
  8. //drawer:Drawer() 左侧 endDrawer: Drawer() 右侧
  9. drawer: Drawer(
  10. child: Column(
  11. children: [
  12. Row(
  13. children: [
  14. Expanded(
  15. child: DrawerHeader(
  16. child: Text('sdfs'),
  17. decoration: BoxDecoration(
  18. color: Colors.green,
  19. ),
  20. ),
  21. ),
  22. ],
  23. ),
  24. Expanded(
  25. child: ListView.separated(
  26. itemCount: 20,
  27. itemBuilder: (context, index) {
  28. return ListTile(
  29. title: Text(_data[index]),
  30. onTap: () {
  31. Navigator.of(context).pop(); //关闭抽屉
  32. },
  33. );
  34. },
  35. separatorBuilder: (context, index) => Divider(height: 0.0),
  36. ),
  37. ),
  38. ],
  39. ),
  40. ),
  41. );
  42. }
  43. }

DrawerHeader 侧边栏头部

  1. DrawerHeader({
  2. Key key,
  3. this.decoration,
  4. this.margin = const EdgeInsets.only(bottom: 8.0),
  5. this.padding = const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 8.0),
  6. this.duration = const Duration(milliseconds: 250),
  7. this.curve = Curves.fastOutSlowIn,
  8. @required this.child,
  9. })

UserAccountsDrawerHeader

image.png

  1. Row(
  2. children: [
  3. Expanded(
  4. child: UserAccountsDrawerHeader(
  5. accountName: Text('朱朝阳'), //账户名称
  6. accountEmail: Text('zcy_2013@163.com'), //账户邮箱
  7. currentAccountPicture: CircleAvatar( //用户头像
  8. backgroundImage: AssetImage('assets/images/avatar.jpg'),
  9. ),
  10. otherAccountsPictures: [ //其它头像组
  11. Image.asset('assets/images/avatar.jpg'),
  12. Image.asset('assets/images/1.jpg'),
  13. Image.asset('assets/images/avatar.jpg'),
  14. ],
  15. decoration: BoxDecoration(
  16. image: DecorationImage(
  17. image: AssetImage('assets/images/1.jpg'),
  18. fit: BoxFit.cover,
  19. ),
  20. ),
  21. ),
  22. ),
  23. ],
  24. ),


api直接调用打开或关闭

  1. onPressed: (){
  2. //打开
  3. Scaffold.of(context).openDrawer();
  4. Scaffold.of(context).openEndDrawer();
  5. //关闭
  6. Navigator.of(context).pop();
  7. }
  1. import "package:flutter/material.dart";
  2. class MyHomePage extends StatelessWidget {
  3. _openDrawer(context) {
  4. Scaffold.of(context).openDrawer(); //打开侧栏
  5. }
  6. @override
  7. Widget build(BuildContext context) {
  8. return Column(
  9. children: [
  10. RaisedButton(
  11. child: Text('打开'),
  12. onPressed: () => this._openDrawer(context),
  13. ),
  14. ],
  15. );
  16. }
  17. }