博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flutter UI基础 - 侧拉抽屉菜单
阅读量:4050 次
发布时间:2019-05-25

本文共 2481 字,大约阅读时间需要 8 分钟。

在移动开发中,我们可以通过底部导航栏、标签页或是侧边抽屉菜单来实现导航。这是在小屏幕上可以充分利用空间。我们设计不仅要实用而且要有趣,这样才算得上好的 UI 设计。这件我们在 Scaffold 通常是上下结构,头部是标题栏下面主界面。

 

@overrideWidget build(BuildContext context) {  // TODO: implement build  return Scaffold(    appBar: AppBar(title: Text(title),),    body: Center(child: Text('$title Demo'),),  ), ),);

Scaffold 除了 appBar 和 body 属性以为还有 drawer 属性方便我们定义侧边抽屉。

@overrideWidget build(BuildContext context) {  // TODO: implement build  return Scaffold(    appBar: AppBar(title: Text(title),),    body: Center(child: Text('$title Demo'),),    drawer: Drawer(    )    ),  ),);

这样便可以在 child 为侧拉抽屉添加内容,内容是添加一个列表。DrawerHeader 添加标题栏。然后 decoration 中添加背景颜色。然后通过 ListTile 组件来添加一条一条内容

child: ListView(      padding: EdgeInsets.zero,      children: 
[ DrawerHeader( child: Text('$title Demo'), decoration: BoxDecoration( color: Colors.blue ), ), ListTile( title: Text("React"), onTap: (){ Navigator.pop(context); }, ), ListTile( title: Text("Vue"), onTap: (){ Navigator.pop(context); }, ) ],),

为 ListTile 添加 onTap 事件来通过 Navigator 返回到主界面。

ListTile(      title: Text("Vue"),      onTap: (){        Navigator.pop(context);      }, )

完整代码

import 'package:flutter/material.dart';class DrawerApp extends StatelessWidget{  final appTitle = "侧滑抽屉";  @override  Widget build(BuildContext context) {    // TODO: implement build    return MaterialApp(      title: appTitle,      home: MyHomePage(title:appTitle),    );  }  }class MyHomePage extends StatelessWidget{  final String title;  MyHomePage({Key key,this.title}):super(key:key);  @override  Widget build(BuildContext context) {    // TODO: implement build    return Scaffold(      appBar: AppBar(title: Text(title),),      body: Center(child: Text('$title Demo'),),      drawer: Drawer(        child: ListView(          padding: EdgeInsets.zero,          children: 
[ DrawerHeader( child: Text('$title Demo'), decoration: BoxDecoration( color: Colors.blue ), ), ListTile( title: Text("React"), onTap: (){ Navigator.pop(context); }, ), ListTile( title: Text("Vue"), onTap: (){ Navigator.pop(context); }, ) ], ), ), ); }}

 

 

 

 

 

 

 

 

转载地址:http://lonci.baihongyu.com/

你可能感兴趣的文章
JavaScript的一些基础-数据类型
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>