Flutter - UI布局

一、容器Widget

1. Scaffold

Scaffold 作為頁面的腳手架,基礎區域包含頂部導航欄 appBar、主體內容區 body、側邊抽屜 drawer、懸浮按鈕 floatingActionButton、底部導航欄 bottomNavigationBar。

Scaffold(appBar: AppBar( // 頂部導航欄title: Text('首頁'),),body: Center( // 主體child: Text('頁面內容'),),drawer: Drawer( // 側邊欄child: ListView(children: [ListTile(title: Text('設置'))]),),floatingActionButton: FloatingActionButton( // 懸浮按鈕child: Icon(Icons.add),tooltip: '懸浮按鈕',onPressed: () {}, ),bottomNavigationBar:  BottomNavigationBar( // 底部導航欄items: [BottomNavigationBarItem(icon: Icon(Icons.home), label: '首頁')],onTap: (index) {},),
)

2. Container

Container 是一個多功能容器,它通過組合多種基礎功能(尺寸控制、邊距、裝飾、對齊等)簡化復雜 UI 的實現。

Container(width: 160, height: 40, // 寬高padding:  EdgeInsets.symmetric(horizontal: 6), // 內邊距alignment: Alignment.topLeft, // 頂部|左側對齊color: Colors.white, // 背景色(與 decoration互斥)decoration: BoxDecoration( // 裝飾組合(背景色、圓角、陰影等)color: Colors.black12,borderRadius: BorderRadius.circular(6),),child: Text("容器") // 子組件
)

二、布局Widget

1. Row

Row 用于水平排列子組件,通過主軸(水平方向)和交叉軸(垂直方向)控制子組件的排列方式。

Row(mainAxisAlignment: MainAxisAlignment.center, // 水平居中(默認值:start)crossAxisAlignment: CrossAxisAlignment.start, // 頂部對齊(默認值:center)textDirection: TextDirection.ltr, // 從左到右spacing: 6, // 間距children: [ // 子組件Expanded(flex: 1, child: Container(color: Colors.gray)), // 占1/3空間Expanded(flex: 2, child: Container(color: Colors.blue)), // 占2/3空間]
)

2. Column

Column 用于垂直排列子組件,通過主軸(垂直方向)和交叉軸(水平方向)控制子組件的排列方式。

Column(mainAxisAlignment: MainAxisAlignment.center, // 垂直居中(默認值:start)crossAxisAlignment: CrossAxisAlignment.start, // 左側對齊(默認值:center)verticalDirection: VerticalDirection.down, // 從上到下spacing: 6, // 間隔children: [ // 子組件Expanded(flex: 2, child: Container(color: Colors.gray)), // 占2/3空間Expanded(flex: 1, child: Container(color: Colors.blue)), // 占1/3空間]
)

3. Stack

Stack 用于實現??層疊布局??,允許子組件按繪制順序(從底部到頂部)堆疊在一起。

Stack(alignment: AlignmentDirectional.bottomStart, // 底部|左側對齊(默認值:topStart)textDirection: TextDirection.ltr, // 從左到右children: [Container(height: 44, color: Colors.blue), // 設置高度、背景色Positioned(top: 10, left: 10, width: 20, height: 20, child: Icon(Icons.star)), // 定位組件Positioned.fill(child: Container(color: Color(0x33000000))), // 半透明遮罩],
)

4. Flex

Flex 用于創建??彈性布局??的核心組件,它通過主軸(main axis)和交叉軸(cross axis)控制子組件的排列方式。

Flex(direction: Axis.horizontal, // 水平方向spacing: 20, // 間距children: [ Flexible(flex: 1, child: Container(color: Colors.grey)), // 填充剩余空間Expanded(flex: 2, child: Container(color: Colors.red)), // 強制填滿剩余空間],
)

5. SizedBox

SizedBox 用于??精確控制尺寸??,它既能約束子組件尺寸,也能作為空白占位符使用。

SizedBox(width: 180, height: 40, // 寬高child: Center(child: Text('精準寬高'),),
)Row(children: [SizedBox(width: 20), // 空白占位符Text('水平排列'),],
)

6.?Align

Align 用于??控制子組件在父容器中的位置。

Align(Alignment: Alignment.topLeft,child: Text('左側|頂部對齊'),
)

7. Center

Center 用于控制子組件的水平|垂直居中。

Center(child: Text('水平|垂直居中'),
)

8. Padding

Padding 用于??控制子組件內邊距??,通過在子組件周圍添加空白區域來調整布局間距。

Padding(padding: EdgeInsets.all(8), child: Text('帶內邊距的文本'),
)

三、滾動Widget

1. SingleChildScrollView

SingleChildScrollView 用于??包裹單個子組件??的滾動容器,支持垂直或水平方向的滾動。

SingleChildScrollView(scrollDirection: Axis.vertical, // 縱向滾動physics: BouncingScrollPhysics(), // 邊界回彈child: Container(height: 1000),
)

2. ListView

ListView 用于創建??可滾動列表??的核心組件,它可以高效地顯示線性排列的數據項,支持垂直或水平滾動。

ListView( // 靜態列表padding: EdgeInsets.symmetric(vertical: 10),children: [ListTile(title: Text('List')),Container(height: 50,alignment: Alignment.centerLeft,padding: EdgeInsets.symmetric(horizontal: 20),child: Text('Row 1'),),],
)
ListView.builder( // 動態列表itemCount: 20,itemBuilder: (context, index) {return Container(height: 50,alignment: Alignment.centerLeft,padding: EdgeInsets.symmetric(horizontal: 20),child: Text('Row ${index+1}'),);},
)
ListView.separated( // 動態列表(設置分隔線)itemCount: 20,separatorBuilder: (context, index) => Divider(height: 1),itemBuilder: (context, index) {return Container(height: 50,alignment: Alignment.centerLeft,padding: EdgeInsets.symmetric(horizontal: 20),child: Text('Row ${index+1}'),);},
)

3. GridView

GridView 用于創建??網格布局??,它可以在二維空間中排列子組件,支持垂直或水平滾動。

GridView( // 靜態網格gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, // 每行3列crossAxisSpacing: 10, // 列間距mainAxisSpacing: 10, // 行間距childAspectRatio: 0.7 // 寬高比),children: [Image.asset('images/image_1.png'),Image.asset('images/image_2.png'),...],
)
GridView.count( // 靜態網格crossAxisCount: 3, // 每行3列crossAxisSpacing: 10, // 列間距mainAxisSpacing: 10, // 行間距childAspectRatio: 0.7 // 寬高比children: [Image.asset('images/image_1.png'),Image.asset('images/image_2.png'),...],
)
GridView.builder( // 動態網格gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, // 每行3列crossAxisSpacing: 10, // 列間距mainAxisSpacing: 10, // 行間距childAspectRatio: 0.7 // 寬高比),itemBuilder: (BuildContext context, int index) {return Image.asset('images/image_${index+1}.png');}
)

四、內容Widget

1. Text

Text 用于文本內容的??顯示??,提供了豐富的文本樣式控制能力。

Text('文本內容',style: TextStyle(color: Color(0xFF333333), fontSize: 18), // 文本樣式textAlign: TextAlign.left, // 文本對齊方式overflow: TextOverflow.ellipsis, // 溢出處理方式maxLines: 3, // 最高行數
)

2. TextField

TextField 用于??文本輸入??,它提供了強大的文本輸入、編輯和驗證功能。

TextField(controller: TextEditingController(text: '123456'), // 管理文本內容decoration: InputDecoration( // 內容裝飾hintText: '請輸入密碼',hintStyle: TextStyle(fontWeight: FontWeight.normal),),style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), // 樣式keyboardType: TextInputType.phone, // 鍵盤類型textInputAction: TextInputAction.done, // 完成按鈕obscureText: false, // 是否隱藏內容onChanged: (value) { }, // 文本變化回調onSubmitted: (value) { }, // 提交回調onEditingComplete: () { }, // 編輯完成回調
)

3. Image

Image 用于??圖片內容的展示?,支持從多種來源加載圖片(本地資源、網絡、內存字節等)。

3.1 在工程的 pubspec.yaml 中配置 assets 路勁。

3.2 用 Image.asset 展示內置 images 圖片。

Image.asset('images/logo.png',width: 120, height: 40,fit: BoxFit.fill, // 拉伸填充
)

3.2 用 Image.network?展示網絡 URL 圖片。

Image.network('http://gips2.baidu.com/it/u=195724436,3554684702&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960',width: 300,height: 100,fit: BoxFit.cover, // 比例填充)

五、手勢Widget

1.?GestureDetector

GestureDetector 用于添加各種手勢事件,包含點擊、長按、拖拽、縮放等手勢操作。

GestureDetector(onTap: () => print('單擊'),onDoubleTap: () => print('雙擊'),onLongPress: () => print('長按'),child: Padding(padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),child: Text('手勢交互'),),
)

2. InkWell

InkWell 用于??添加觸摸事件??,它提供了單擊、長按等交互的視覺反饋,實現墨水漣漪效果必須包裹在 Material 組件內。

 Material( // 添加Material層child: InkWell(onTap: () => print('單擊'),onDoubleTap: () => print('雙擊'),onLongPress: () => print('長按'),splashColor: Color(0x22333333), // 飛濺顏色highlightColor: Color(0x44333333), // 高亮顏色child: Padding(padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),child: Text('點擊 - ??墨水漣漪效果'),),),
)

六、按鈕Widget

1. ElevatedButton

ElevatedButton 是??凸起按鈕,適用于主要操作、表單提交。

ElevatedButton(onPressed: () => print('點擊'),child: Text('凸起按鈕', style: TextStyle(fontSize: 20, color: Colors.white)),style: ElevatedButton.styleFrom(backgroundColor: Color(0xFF52d0c2), // 背景色elevation: 3, // 陰影高度shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20), // 圓角),padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15), // 內邊距),
)

2.?TextButton

TextButton 是??扁平化文本按鈕??,適用于次要操作、對話框選項和鏈接式操作。

TextButton(onPressed: () => print('點擊'),child: Text('文本按鈕', style: TextStyle(fontSize: 20, color: Colors.white)),style: TextButton.styleFrom(backgroundColor: Colors.black, // 背景色padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15), // 內邊距),
)

3. FilledButton

FilledButton 是??實心填充背景??的按鈕,介于 ElevatedButton 和 TextButton 之間。

FilledButton(onPressed: () => print('點擊'),child: Text('填充按鈕', style: TextStyle(fontSize: 20, color: Colors.white)),style: FilledButton.styleFrom(elevation: 3, // 陰影高度backgroundColor: Colors.black, // 背景色padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15), // 內邊距),
)

4. OutlinedButton

OutlinedButton 是帶邊框按鈕??,通過邊框強調按鈕形狀,同時保持背景透明。

OutlinedButton(onPressed: () => print('點擊'),child: Text('帶邊框按鈕', style: TextStyle(fontSize: 20, color: Colors.black)),style: OutlinedButton.styleFrom(side: BorderSide(width: 1, color: Colors.black), // 邊框屬性backgroundColor: Colors.white, // 背景色padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15), // 內邊距),
),

OutlinedButton(onPressed: () => print('點擊'),child: SizedBox(width: 196,child: Row(mainAxisAlignment: MainAxisAlignment.center,spacing: 10,children: [Icon(Icons.camera) , Text('帶邊框的圖文按鈕', style: TextStyle(fontSize: 18))],),),style: OutlinedButton.styleFrom(side: BorderSide(width: 1, color: Colors.black),backgroundColor: Colors.white, // 背景色padding: EdgeInsets.symmetric(horizontal: 0, vertical: 15), // 內邊距),
)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/96592.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/96592.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/96592.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

UNIKGQA論文筆記

UNIKGQA: UNIFIED RETRIEVAL AND REASONING FOR SOLVING MULTI-HOP QUESTION ANSWERING OVER KNOWLEDGE GRAPH(ICLR 2023)Introduction知識圖上的多跳問題回答(KGQA)的目的是在大規模知識圖譜(KG)上找到自然語言問題中提到的主題實…

MySQL 8.0.17 “Too Many Connections” 排查指南

MySQL 8.0.17 “Too Many Connections” 排查與優化指南 在 MySQL 8.0.17 中,當出現“Too many connections”錯誤時,通常意味著數據庫連接數已達上限。這不僅會影響應用性能,還可能導致連接池(如 Druid)無法獲取新連接…

GEO優化服務:智能時代營銷新賽道的中國引領者——全球行業格局與發展趨勢觀察

隨著全球人工智能技術的迅猛發展,以GPT-5、Claude Opus以及我國的DeepSeek Divine、豆包等為代表的新一代生成式AI搜索引擎,正深刻改變著信息獲取與商業決策模式。用戶通過直接向AI提問獲取整合答案的行為日益普遍,傳統搜索引擎的流量入口地位…

全面解析主流AI模型:功能對比與應用推薦

全面解析主流AI模型:功能對比與應用推薦 在當前人工智能技術飛速發展的背景下,市面上涌現了多種具備不同能力的AI模型。本文將系統梳理主流模型的特性、對比其核心能力,并結合實際場景推薦高效、穩定的API服務(如https://api.aaa…

【Nacos知識】Nacos 作為注冊中心的客戶端配置詳解

Nacos 作為注冊中心的客戶端配置詳解Nacos 作為注冊中心的客戶端配置詳解一、核心配置項全景圖二、基礎連接配置1. 服務端地址配置2. 命名空間配置3. 服務分組配置三、服務注冊配置1. 服務元數據配置2. 網絡位置配置3. 集群與權重配置四、健康檢查配置1. 心跳參數配置2. 健康檢…

TypeReference 泛型的使用場景及具體使用流程

簡介 在 Java 中,泛型類型在運行時會被擦除。這意味著當我們使用泛型時,運行時無法直接獲取到泛型的具體類型信息。例如,我們無法直接通過 Class 對象來獲取一個泛型類型的類型參數。這在某些情況下可能會導致問題,特別是在我們需…

商超場景徘徊識別誤報率↓79%!陌訊多模態時序融合算法落地優化

原創聲明本文為原創技術解析文章,核心技術參數與架構設計引用自 “陌訊技術白皮書(2024 版)”,所有技術描述均經過重寫轉換,無復制官網文案行為,嚴禁未經授權轉載。一、行業痛點:徘徊識別的場景…

KubeBlocks AI:AI時代的云原生數據庫運維探索

KubeBlocks AI:AI時代的云原生數據庫運維探索 REF Auto-detect-failure 架構Auto-bug-detect測試 引言 傳統的自動化運維診斷主要依賴基于規則的方法——無論是Ansible Playbooks的預定義腳本,還是Kubernetes Operator的固化邏輯,這些方法…

如何編譯botan加密庫?

Botan加密庫支持2.x版本和3.x版本,其中3.x版本需要支持C20。0、下載源碼git clone https://github.com/randombit/botan.gitcd botan切換分支到2.19.5版本git checkout 2.19.51、Windows編譯Botan加密庫1.1 配置生成MakefileRelease模式python configure.py --ccmsv…

Linux問答題:分析和存儲日志

目錄 1. RHEL 日志文件保存在哪個目錄中? 2.什么是 syslog 消息和非 syslog 消息? 3.哪兩個服務處理 RHEL 中的 syslog 消息? 4. 列舉常用的系統日志文件并說明其存儲的消息類型。 5. 簡單說下日志文件輪轉的作用 6.systemd-journald 服…

chapter05_從spring.xml讀取Bean

一、簡化Bean的注冊 如果每次注冊一個Bean,都要像上節一樣,手動寫PropertyValues相關的代碼,那太復雜了,我們希望讀取XML文件,自動注冊Bean,這樣對于使用者,甚至不知道有BeanDefinition的存在 二…

【數位DP】D. From 1 to Infinity

Problem - D - Codeforces 題目: 思路: 數位DP 數論 題目讓我們求這個無限序列 123456789101112.... 的前 k 個數的數位和 題目看起來很不好求,事實上確實是這樣的 我們可以先從簡單問題開始 問題①. 求 k 位置對應著第幾個數 那么顯然…

gitlab、jenkins等應用集成ldap

gitlab、jenkins等應用集成ldap 文檔 openldap安裝 -添加條目gitlab、jenkins等應用集成ldap gitlab集成ldap gitlab版本:gitlab-jh-17.7.0 ldap版本:openldap-2.6.10 修改/etc/gitlab/gitlab.rb文件,編輯相關信息 gitlab_rails[ldap_en…

Unity中國小游戲行業沙龍:抖音小游戲平臺分析與規劃

目錄 一、抖音小游戲市場全景分析 行業現狀與發展趨勢 行業發展關鍵議題 內容運營生態觀察 二、平臺技術架構與運營體系 用戶復訪與留存體系 技術支撐體系 三、平臺激勵與商業化政策 收益分成機制 資金服務升級 技術基礎建設 四、生態合作與發展規劃 開發者支持體系…

手機橫屏適配方案

CSS自動旋轉頁面實戰指南在移動端開發中,橫屏適配是一個常見但棘手的問題。本文將深入解析一套完整的CSS橫屏適配方案,讓你的網頁在手機旋轉時自動調整布局,提供無縫的用戶體驗。一、橫屏適配的重要性 隨著移動設備使用場景的多樣化&#xff…

藍橋杯算法之基礎知識(2)——Python賽道

1.循環里面套用遞歸,當遞歸執行return時,只會退出當前遞歸層2.不能一邊遍歷list 一邊pop解決辦法:倒序遍歷解決或者創建新的列表去存儲3.sqrt求出來的始終是小數形式,注意題目要求的結果有可能是整型你直接sqrt就提交,…

如何優雅解決 OpenCV 分段錯誤(Segfault):子進程隔離實戰

在分布式數據平臺(如 Databricks Spark)中跑視頻處理任務時,你是否遇到過這種惡心的報錯?Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe. : org.apache.spark.Spark…

Docker的六種網絡模式(詳解)

文章目錄1. bridge(默認)2. host3. none4. container5. overlay6. macvlan7. 總結對比Docker 六種網絡模式是容器網絡的基礎概念,不同模式決定容器與宿主機、外部網絡、其他容器之間的通信方式。 1. bridge(默認) Br…

微服務流量分發核心:Spring Cloud 負載均衡解析

目錄 理解負載均衡 負載均衡的實現方式 服務端負載均衡 客戶端負載均衡 Spring Cloud LoadBalancer快速上手 常見的負載均衡策略 自定義負載均衡策略 LoadBalancer 原理 理解負載均衡 在 Spring Cloud 微服務架構中,負載均衡(Load Balance&#…

鴻蒙異步處理從入門到實戰:Promise、async/await、并發池、超時重試全套攻略

摘要(介紹目前的背景和現狀) 在鴻蒙(HarmonyOS)里,網絡請求、文件操作、數據庫訪問這類 I/O 都是異步的。主流寫法跟前端類似:Promise、async/await、回調。想把 app 做得“流暢且不阻塞”,核心…