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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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 做得“流畅且不阻塞”,核心…