在 Scaffold 组件里面传入 drawer 参数可以定义左侧边栏,传入 endDrawer 可以定义右侧边栏。侧边栏默认是隐藏的,我们可以通过手指滑动显示侧边栏,也可以通过点击按钮显示侧边栏。 
我们还是在前面TabBar项目的基础上实现侧边栏。
为了能在底部TabBar的三个页面都实现侧边栏效果,这里将侧边栏加在Tabs.dart页面中。
只需要在

BottomNavigationBar下面继续添加drawer属性就可以了。
import 'package:flutter/material.dart';
import 'tabs/Home.dart';
import 'tabs/Category.dart';
import 'tabs/Setting.dart'; class Tabs extends StatefulWidget {
final index;
Tabs({Key key,this.index=0}) : super(key: key); _TabsState createState() => _TabsState(this.index);
} class _TabsState extends State<Tabs> { // int _currentIndex=0;
int _currentIndex;
_TabsState(index){
this._currentIndex=index;
}
List _pageList=[
HomePage(),
CategoryPage(),
SettingPage(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Demo"),
),
body: this._pageList[this._currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: this._currentIndex,
onTap: (int index){
setState(() {
this._currentIndex=index;
});
},
iconSize:36.0, //icon的大小
fixedColor:Colors.red, //选中的颜色
type:BottomNavigationBarType.fixed, //配置底部tabs可以有多个按钮
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("首页")
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
title: Text("分类")
), BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text("设置")
)
],
),
drawer: Drawer(
child:Text('这里是测边栏')
),
);
}
}

此时在顶部会出现一个可点击的图标按钮,不管是点击这个按钮还是滑动屏幕,都会出现侧边栏:

   

如果要实现右边的侧边栏,只需要使用endDrawer就可以了:

上面的样式太丑了,所以,可以稍微调整一下了:

    

现在,虽然侧边栏里面多了一下内容,但是顶部还是出现了遮盖的现象,因此,可以借助DrawerHeader 组件来设置一下侧边栏顶部的样式。

DrawerHeader

在flutter中,DrawerHeader 有以下一些常用的属性:

  • decoration :设置顶部背景颜色
  • child :配置子元素
  • padding :内边距
  • margin :外边距

  

现在,借助了DrawerHeader实现了侧边栏的头部样式,除此之外,还可以借助UserAccountsDrawerHeader实现侧边栏头部的布局。

UserAccountsDrawerHeader

在flutter中,UserAccountsDrawerHeader有以下一些常用的属性:

  • decoration :设置顶部背景颜色
  • accountName:账户名称
  • accountEmail :账户邮箱
  • currentAccountPicture :用户头像
  • otherAccountsPictures :用来设置当前账户其他账户头像
  • margin :

   

侧边栏中的路由跳转

要实现路由跳转,首先需要有供跳转的页面:

User.dart

import 'package:flutter/material.dart';

class UserPage extends StatelessWidget {
const UserPage({Key key}) : super(key: key); @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("用户中心"),
),
);
}
}

然后配置路由:

最后,在用户中心处添加路由跳转按钮就可以了。但是有一个问题:在侧边栏进行路由跳转,那么侧边栏是开启的,这样,页面跳转后再返回,侧边栏还是存在的,并且动画上面还会出现不流畅性,因此,在路由跳转前,需要先隐藏侧边栏:

    

Tabs.dart

import 'package:flutter/material.dart';
import 'tabs/Home.dart';
import 'tabs/Category.dart';
import 'tabs/Setting.dart'; class Tabs extends StatefulWidget {
// Tabs({Key key}) : super(key: key);
// _TabsState createState() => _TabsState();
final index;
Tabs({Key key,this.index=0}) : super(key: key); _TabsState createState() => _TabsState(this.index);
} class _TabsState extends State<Tabs> { // int _currentIndex=0;
int _currentIndex;
_TabsState(index){
this._currentIndex=index;
}
List _pageList=[
HomePage(),
CategoryPage(),
SettingPage(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Demo"),
),
body: this._pageList[this._currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: this._currentIndex, //配置对应的索引值选中
onTap: (int index){
setState(() { //改变状态
this._currentIndex=index;
});
},
iconSize:36.0, //icon的大小
fixedColor:Colors.red, //选中的颜色
type:BottomNavigationBarType.fixed, //配置底部tabs可以有多个按钮
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("首页")
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
title: Text("分类")
), BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text("设置")
)
],
),
drawer: Drawer(
child:Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
// child: DrawerHeader(
// child: Text('你好'),
// decoration: BoxDecoration(
// color:Colors.yellow,
// image: DecorationImage(
// image: NetworkImage("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3230740943,2194698121&fm=27&gp=0.jpg"),
// fit:BoxFit.cover,
// ),
// ),
// ),
child:UserAccountsDrawerHeader(
currentAccountPicture: CircleAvatar(
backgroundImage: NetworkImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1547205045,3791549413&fm=27&gp=0.jpg"),
),
accountName: Text('侧边栏'),
accountEmail: Text('12345678@qq.com'),
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3230740943,2194698121&fm=27&gp=0.jpg"),
fit:BoxFit.cover,
)
),
),
),
],
),
ListTile(
leading: CircleAvatar(child: Icon(Icons.home)),
title:Text('我的主页')
),
Divider(), //一根线的效果
ListTile(
leading: CircleAvatar(child: Icon(Icons.people)),
title:Text('用户中心'),
onTap: (){
Navigator.of(context).pop(); //隐藏侧边栏
Navigator.pushNamed(context, '/user');//路由跳转
},
),
Divider(),
ListTile(
leading: CircleAvatar(child: Icon(Icons.settings)),
title:Text('设置中心')
),
],
)
),
);
}
}

代码下载:点这里(提取码:ukur)

Drawer实现侧边栏布局的更多相关文章

  1. Flutter常用布局组件

    Flutter控件本身通常由许多小型.单用途的控件组成,结合起来产生强大的效果,例如,Container是一种常用的控件,由负责布局.绘画.定位和大小调整的几个控件组成,具体来说,Container是 ...

  2. Android实现侧边栏SlidingPaneLayout

    //主布局 1 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widg ...

  3. android121 zhihuibeijing SlidingMenu(侧边栏效果,使用开源库)

    ## Splash ## - 旋转 RotateAnimation - 缩放 ScaleAnimation - 渐变 AlphaAnimation 工程可以作为一个库被其他工程当成一个Library使 ...

  4. Android 侧边栏(使用Support Library 4提供的扩展组件)

    本文转自:http://www.apkbus.com/android-117148-1-1.html 写在前面的话:接触Android已经有一段时间了,自己积累的东西也算蛮多的.总结以往的经验,凡是关 ...

  5. ToolBar+Drawable实现一个好用的侧滑栏(侧边栏)和工具栏

    先参考下ToolBar的使用和DrawableLayout的使用: 1.主界面布局,主要结构包含一个ToolBar和一个DrawableLayout,DrawableLayout里面有左侧边栏布局和主 ...

  6. 2.SlidingMenu(侧边栏效果)

    > 使用步骤库:别的程序可以用它的方法.图片. 下载的其中一个框架的例子是没有actionBar的,example_update 引入出错可能是俩个v4包冲突了,删掉工程里的一个,不要删了库里的 ...

  7. div+css网页标准布局实例教程(一)

    今天学习<十天学会web标准(div+css)>的最后一个章节,本章节把前面学习的零碎内容串联起来,组织成一个网站,将根据本人这些年来的从业经验,从建立站点到一个完整的div+css网页的 ...

  8. Android开发——布局性能优化的一些技巧(一)

    0. 前言 上一篇我们分析了为什么LinearLayout会比RelativeLayout性能更高,意义在于分析了这两种布局的实现源码,算是对一个小结论的证明过程,但是对布局性能的优化效果,对这两种布 ...

  9. 抽屉效果的实现(DrawerLayout和SlidingMenu的对比)

    在做谷歌电子市场的时候用的是DrawerLayout实现的抽屉效果,在新闻客户端的时候用的是开源框架SlidingMenu来实现的,总的来说,各有个的优点,侧滑(开源框架)实现的效果更好,但是Draw ...

随机推荐

  1. Application.Restore不起作用了

    http://www.myexception.cn/delphi/695243.html Application.Restore不起作用了窗体上只有一个Button和一个Timer(1秒计时)代码如下 ...

  2. Spring学习01——HelloSpring

    这是一个spring入门demo: package com.su.test; public class HelloWorld { public void say(){ System.out.print ...

  3. G2 基本使用 折线图 柱状图 饼图 基本配置

    G2的基本使用 1.浏览器引入  <!-- 引入在线资源 --> <script src="https://gw.alipayobjects.com/os/lib/antv ...

  4. 【Linux】limits.conf 不重启就生效或者不生效的原因

    前阵子,我要用到使LInux的文件打开数为65534个,而且需要永久生效,于是将配置写到了: vim /etc/security/limits.conf * soft nofile 65534* ha ...

  5. windows 10上玩耍ubuntu

    win10 已经支持运行子系统ubuntu了. 安装ubuntu 程序和功能>>启用或关闭Windows功能>>勾选"适用于Linux的Windows子系统" ...

  6. 002/Node.js(Mooc)--Http知识

    1.什么是Http 菜鸟教程:http://www.runoob.com/http/http-tutorial.html 视频地址:https://www.imooc.com/video/6713 h ...

  7. EasyUI选项卡避免重复打开

    前台代码: <div data-options="region:'west',title:'我的工作平台',split:true,iconCls:'icon-desk'"  ...

  8. C++ 中赋值运算符重载以及深拷贝浅拷贝解析

    转载自:http://blog.csdn.net/business122/article/details/21242857 关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符 ...

  9. 单片机BootLoader

    http://bbs.elecfans.com/jishu_467138_1_1.html 作用:app程序,固件程序升级

  10. Web API 入门一

    之前我也了解过Web API 这部分,但是没有系统学习,更没有相关记录,故现在,写些博客记录入门学习过程.首先,关于API,只要学习编程的都应该知道,也都用过,API(应用程序编程接口)是一些预先定义 ...