網站首頁 編程語言 正文
本文實例為大家分享了Flutter Drawer抽屜菜單示例代碼,供大家參考,具體內容如下
一.Flutter Drawer組件簡介
1.源碼查看
const Drawer({ ? ? Key? key, ? ? this.elevation = 16.0, //陰影效果大小 ? ? this.child, //內容元素 ? ? this.semanticLabel, //關閉/打開抽屜時的通知信息 ? })?
二.抽屜菜單示例
1.菜單項,使用 ListTile 實現
Expanded( ? ? ? ? ? ? ? child: ListView( ? ? ? ? ? ? ? ? children:[ ? ? ? ? ? ? ? ? ? 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:[ ? ? ? ? ? ? Padding( ? ? ? ? ? ? ? padding: const EdgeInsets.only(top: 30.0), ? ? ? ? ? ? ? child: Row( ? ? ? ? ? ? ? ? children: [ ? ? ? ? ? ? ? ? ? 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: [ ? ? ? ? ? ? ? ? ? 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 ? StatecreateState()=>_MainPageState(); } ? class _MainPageState extends State { ? ? var allPages=[HomePage(),ContactPage(),FindPage(),MyPage()]; ? var currentIndex=0; ? ? ? @override ? Widget build(BuildContext context) { ? ? return Scaffold( ? ? ? appBar: AppBar( //導航欄 ? ? ? ? title: Text("App Name"), ? ? ? ? actions: [ //導航欄右側分享菜單 ? ? ? ? ? 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
相關推薦
- 2022-05-18 基于python介紹pytorch保存和恢復參數_python
- 2022-03-15 React?Router?V6更新內容詳解_React
- 2021-12-06 樹莓派4B+EdgeX+MQTT的填坑之旅
- 2022-07-22 linux centos 7 vim配置詳解
- 2023-07-16 uniapp 微信小程序獲取當前位置的坐標
- 2022-09-16 深度學習中shape[0]、shape[1]、shape[2]的區別詳解_python
- 2021-11-28 深入講解Socket原理_C 語言
- 2023-11-13 linux平臺下ZeroMQ zmq(C++)編譯安裝以及調用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支