在 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. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_3_Map接口中的常用方法

    这个方法比较特殊,它的返回值是V他也就是Vlaue get remove containsKey: put value没有重复的所以v1返回的是null key值有重复,所以会返回被替换的值,范冰冰1 ...

  2. python自动化测试接口测试http请求报404的其中一个坑

    在敲代码的路上 ,总是会遇到报错找半天原因,最后发现是个低级错误的时候! 这不今天为了这个错误找了半天原因.......... http请求接口测试中报404我遇到的大部分都是url的问题: 但是今天 ...

  3. 【Unity Shader】---入门知识点

    着色器声明(“名字”)Shader "ShaderDiffuseExample" { 一.属性定义(作用:外部传入参数) 属性定义语法:PropName("Display ...

  4. 执行 bower -v 时出现内部错误

    安装nodejs ,我的位置是D:\node.js_install.全局模块安装默认放在C:\Users\Administrator\AppData\Roaming\npm\node_modules里 ...

  5. [LeetCode] 834. Sum of Distances in Tree

    LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...

  6. C# DropDownList绑定添加新数据的三种方法

    一.在前台手动绑定 <asp:DropDownList ID="DropDownList1" runat="server">    <asp: ...

  7. jenkins无法显示html样式问题解决

    利用jenkins的以下两个插件可以巧妙解决这个问题 Startup Trigger: 可实现在Jenkins节点(master/slave)启动时触发构建: Groovy plugin: 可实现直接 ...

  8. [Markdown] 02 简单应用 第二弹

    目录 4. 插入链接 4.1 Markdown 的方式 用法 1 用法 2 4.2 HTML5 的方法 用法 1 用法 2 5. 插入图片 5.1 使用网络地址 5.2 使用本地地址 5.2.1 小括 ...

  9. stylus快速上手

    定义变量,比如一键切换主题色 1.创建xxx.styl文件,定义变量 $bgColor = #00bcg4 2.在其他页面的style区域里,先引入这个xxx.styl文件 <style> ...

  10. JS对象总结

    JS对象总结   复习: 1.1 JS中对象有三种:内置对象(数组Array对象.String字符串对象.RegExp正则表达式对象.Math对象). 宿主对象(JS脚本所在的运行环境,目前我们讲的脚 ...