Flutter Drawer抽屉菜单示例详解
作者:伟雪无痕 时间:2022-07-30 12:34:11
本文实例为大家分享了Flutter Drawer抽屉菜单示例代码,供大家参考,具体内容如下
一.Flutter Drawer组件简介
1.源码查看
const Drawer({
Key? key,
this.elevation = 16.0, //阴影效果大小
this.child, //内容元素
this.semanticLabel, //关闭/打开抽屉时的通知信息
})
二.抽屉菜单示例
1.菜单项,使用 ListTile 实现
Expanded(
child: ListView(
children: <Widget>[
ListTile(
leading: const Icon(Icons.person),
title: const Text('Personal'),
),
ListTile(
leading: const Icon(Icons.message),
title: const Text('information'),
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('about'),
),
],
),
),
2.底部导航栏,使用BottomNavigationBar实现
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
type: BottomNavigationBarType.fixed,
unselectedItemColor: Colors.grey,
selectedItemColor: Colors.blue,
/*unselectedLabelStyle:TextStyle(
color: Colors.black
),*/
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "首页",
//backgroundColor:Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: "通讯录",
//backgroundColor:Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.find_in_page),
label: "发现",
//backgroundColor:Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.flip_outlined),
label: "我的",
//backgroundColor:Colors.blue
),
],
onTap: (index){
setState(() {
print("the index is :$index");
currentIndex=index;
});
},
),
参考:flutter底部导航栏
3.悬浮按钮,使用FloatingActionButton实现
floatingActionButton: FloatingActionButton( //悬浮按钮
child: Icon(Icons.add),
onPressed:_onAddNum
),
三.Demo及实际效果
1.mydrawer.dart
import 'package:flutter/material.dart';
class MyDrawer extends StatelessWidget {
const MyDrawer({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Drawer(
elevation: 30,
child: MediaQuery.removePadding(
context: context,
//移除抽屉菜单顶部默认的空白
removeTop: true,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 30.0),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15.0),
child: ClipOval(
child: Image.asset(
"images/cc.png",
width: 60,
),
),
),
Text(
"jon",
style: TextStyle(fontWeight: FontWeight.bold),
)
],
),
),
Expanded(
child: ListView(
children: <Widget>[
ListTile(
leading: const Icon(Icons.person),
title: const Text('Personal'),
),
ListTile(
leading: const Icon(Icons.message),
title: const Text('information'),
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('about'),
),
],
),
),
],
),
),
);
}
}
2.MainPage.dart
import 'package:flutter/material.dart';
import 'findpage.dart';
import 'mypage.dart';
import 'contactpage.dart';
import 'homepage.dart';
import 'mydrawer.dart';
class MainPage extends StatefulWidget{
const MainPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState()=>_MainPageState();
}
class _MainPageState extends State<MainPage>{
var allPages=[HomePage(),ContactPage(),FindPage(),MyPage()];
var currentIndex=0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar( //导航栏
title: Text("App Name"),
actions: <Widget>[ //导航栏右侧分享菜单
IconButton(icon: Icon(Icons.share), onPressed: () {}),
],
),
drawer: MyDrawer(), //菜单抽屉
body: allPages[currentIndex],
backgroundColor: Colors.green,
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
type: BottomNavigationBarType.fixed,
unselectedItemColor: Colors.grey,
selectedItemColor: Colors.blue,
/*unselectedLabelStyle:TextStyle(
color: Colors.black
),*/
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "首页",
//backgroundColor:Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: "通讯录",
//backgroundColor:Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.find_in_page),
label: "发现",
//backgroundColor:Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.flip_outlined),
label: "我的",
//backgroundColor:Colors.blue
),
],
onTap: (index){
setState(() {
print("the index is :$index");
currentIndex=index;
});
},
),
floatingActionButton: FloatingActionButton( //悬浮按钮
child: Icon(Icons.add),
onPressed:_onAddNum
),
);
}
void _onAddNum(){
}
}
3.效果
来源:https://blog.csdn.net/j086924/article/details/123204358
标签:Flutter,Drawer,抽屉菜单
0
投稿
猜你喜欢
Servlet+JDBC实现登陆功能的小例子(带验证码)
2021-05-29 03:04:25
Netty如何设置为Https访问
2021-12-06 02:00:40
C#设计模式之Facade外观模式解决天河城购物问题示例
2023-06-10 08:28:36
Android设置项目为系统APP方法
2022-11-16 08:55:02
MyBatis中XML 映射文件中常见的标签说明
2023-01-07 08:02:59
JAVA宝藏工具hutool的使用
2023-08-25 20:20:12
Android切面编程知识点详解
2023-05-05 04:18:04
解决@JsonInclude(JsonInclude.Include.NON_NULL)不起作用问题
2023-05-26 08:57:45
C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)
2023-06-16 07:54:29
Kotlin语言编程Regex正则表达式实例详解
2023-06-22 02:06:29
flutter 屏幕尺寸适配和字体大小适配的实现
2022-06-10 06:54:49
Java虚拟机内存分配与回收策略问题精细解读
2021-08-12 18:54:51
详解Java设计模式——命令模式
2023-11-23 05:00:07
Java创建线程的七种方法总结(全网最全面)
2023-11-03 14:27:26
Spring Boot Debug调试过程图解
2023-12-13 10:27:33
android Launcher3设置默认桌面应用
2022-07-20 20:18:42
Android编程基于Contacts读取联系人的方法(附demo源码)
2023-11-22 06:05:39
C# 大数据导出word的假死报错的处理方法
2022-09-25 07:19:48
Android 正则表达式验证手机号、姓名(包含少数民族)、身份证号
2022-05-26 02:17:11
Android实现百度地图两点画弧线
2022-08-17 14:52:03