当前位置:   article > 正文

【Flutter】二十三、Flutter常用组件——抽屉(侧边栏)Drawer_flutter 右侧抽屉怎么手动关闭

flutter 右侧抽屉怎么手动关闭


    使用Scaffold控件的drawer和endDrawer可以实现左侧边栏及右侧边栏。侧边栏默认隐藏,可通过手指滑动或点击事件呼出侧边栏。

一、Drawer构造器

    Drawer构造器十分简单:

Drawer({
    Key key,
    this.elevation = 16.0, // 侧边阴影范围
    this.child, // 
    this.semanticLabel,
  })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

    Flutter提供了UserAccountsDrawerHeader控件可用来作为Drawer的头部,可以更为方便地进行布局,UserAccountsDrawerHeader展示如下:
在这里插入图片描述
示例源码:

import 'package:flutter/material.dart';

class DrawerDemo extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Drawer(
      elevation: 16.0,
      child: Stack(
        children: <Widget>[
          Column(
            children: <Widget>[
              UserAccountsDrawerHeader(
                accountName: Text('name:xxxx'),
                accountEmail: Text('email:22222@163.com'),
                currentAccountPicture: CircleAvatar(backgroundImage: AssetImage('assets/images/account_picture.jpeg'),),
                otherAccountsPictures: <Widget>[
                  CircleAvatar(backgroundImage: AssetImage('assets/images/account_picture.jpeg')),
                  CircleAvatar(backgroundImage: AssetImage('assets/images/account_picture.jpeg')),
                  CircleAvatar(backgroundImage: AssetImage('assets/images/account_picture.jpeg'))
                ],
                arrowColor: Colors.red,
                onDetailsPressed: () {
                  print(1);
                },
                decoration: BoxDecoration(
                  image: DecorationImage(
                      image: NetworkImage(
                          'http://pic36.nipic.com/20131203/3822951_102145644000_2.jpg'
                      ),
                      fit: BoxFit.cover
                  ),
                ),
              ),
              Expanded(
                child: ListView(
                  padding: EdgeInsets.symmetric(vertical: 10.0),
                  children: <Widget>[
                    ListTile(
                      leading: Icon(Icons.location_on),
                      title: Text('附近的人'),
                    ),
                    Divider(),
                    ListTile(
                      leading: Icon(Icons.settings),
                      title: Text('设置'),
                    ),
                  ],
                ),
              ),
            ],
          ),
          Positioned(
            bottom: 10,
            right: 10,
            child: InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  IconButton(
                    icon: Icon(Icons.power_settings_new),
                  ),
                  Text('退出')
                ],
              ),
              onTap: () => Navigator.pop(context),
            )
          )
        ],
      )
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

二、侧边栏的隐藏及显示

    默认从手机左侧边框向右滑动可呼出左侧边栏,通过Scaffold.of(context).openDrawer()可以主动呼出左侧边栏,Scaffold.of(context).openEndDrawer()呼出右侧边栏。

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(     
    	home: Scaffold(
        body: OpenDrawer(),
        drawer: DrawerDemo(),
        endDrawer: DrawerDemo(),
      ),
    );
  }
}

class OpenDrawer extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return SizedBox.expand(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          RaisedButton(
            child: Text('呼出左侧侧边栏'),
            onPressed: () => Scaffold.of(context).showSnackBar( // 注意此处context不能直接使用MyApp中的context,如果要直接在MyApp中使用此方法,可使用Builder
              SnackBar(
                content: Text('Have a snack!'),
              ),
            ),
          ),
          RaisedButton(
            child: Text('呼出右侧侧边栏'),
            onPressed: () => Scaffold.of(context).openEndDrawer(),
          )
        ],
      ),
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

    Builder中使用Scaffold.of(context).openDrawer()

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Builder(
          builder: (context) => Center(
            child: RaisedButton(
              child: Text('呼出左侧侧边栏'),
              onPressed: () => Scaffold.of(context).openDrawer(),
            ),
          ),
        ),
        drawer: DrawerDemo(),
        endDrawer: DrawerDemo(),
      ),
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

    调用Navigator.pop(context)可关闭侧边栏。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/628197
推荐阅读
相关标签
  

闽ICP备14008679号