網站首頁 編程語言 正文
在移動開發中,我們可以通過底部導航欄、標簽頁或是側邊抽屜菜單來實現導航。這是在小屏幕上可以充分利用空間。我們設計不僅要實用而且要有趣,這樣才算得上好的 UI 設計。這件我們在 Scaffold 通常是上下結構,頭部是標題欄下面主界面。
@override Widget build(BuildContext context) { ? // TODO: implement build ? return Scaffold( ? ? appBar: AppBar(title: Text(title),), ? ? body: Center(child: Text('$title Demo'),), ? ), ?), );
Scaffold 除了 appBar 和 body 屬性以為還有 drawer 屬性方便我們定義側邊抽屜。
@override Widget 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); ? ? ? ? ? ? ? }, ? ? ? ? ? ? ) ? ? ? ? ? ], ? ? ? ? ), ? ? ? ), ? ? ); ? } }
原文鏈接:https://blog.csdn.net/shanghaibao123/article/details/107629748
相關推薦
- 2023-03-15 React受控組件與非受控組件實例分析講解_React
- 2022-05-10 torch.cuda.is_available()返回false最終解決方案
- 2024-07-15 arthas操作spring被代理目標對象命令速查
- 2022-03-21 詳解c++優先隊列priority_queue的用法_C 語言
- 2022-04-11 SQL?Server的觸發器你了解多少_MsSql
- 2023-01-14 python與matlab一些常用函數互轉問題_python
- 2022-03-19 .NET中的Husky工具及安裝方式_實用技巧
- 2023-02-10 C/C++中智能指針的用法詳解_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同步修改后的遠程分支