flutter软件开发笔记08-容器使用方法
liuian 2025-05-21 14:59 16 浏览
在 Flutter 3 中,容器组件是用于布局、装饰或约束子组件的核心部件,能让程序更加美观,如何学习呢,能快速的应用起来,下面通过例子,来快速理解各种容器组件的使用方法。
一程序界面
二 代码实现
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter 容器组件示例',
theme: ThemeData(primarySwatch: Colors.blue),
home: const ContainerDemoScreen(),
);
}
}
class ContainerDemoScreen extends StatelessWidget {
const ContainerDemoScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('容器组件示例')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: Column(
children: [
// 1. Container 示例
_buildContainerDemo(),
const SizedBox(height: 20),
// 2. Row/Column 示例
_buildRowColumnDemo(),
const SizedBox(height: 20),
// 3. Stack 示例
_buildStackDemo(),
const SizedBox(height: 20),
// 4. ListView/GridView 示例
_buildListAndGridDemo(),
const SizedBox(height: 20),
// 5. Expanded/Flexible 示例
_buildExpandedDemo(),
const SizedBox(height: 20),
// 6. Card 示例
_buildCardDemo(),
],
),
),
);
}
// ---------- 以下是各个容器的构建方法 ----------
// 1. Container 示例
Widget _buildContainerDemo() {
return Container(
width: 200,
height: 100,
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
color: Colors.blue[100],
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue, width: 2),
),
child: const Center(
child: Text('Container', style: TextStyle(color: Colors.blue)),
),
);
}
// 2. Row/Column 示例
Widget _buildRowColumnDemo() {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(width: 50, height: 50, color: Colors.red),
Container(width: 50, height: 50, color: Colors.green),
Container(width: 50, height: 50, color: Colors.blue),
],
),
const SizedBox(height: 10),
Column(
children: const [
Text('Row 水平排列'),
Text('Column 垂直排列'),
],
),
],
);
}
// 3. Stack 示例
Widget _buildStackDemo() {
return SizedBox(
width: 200,
height: 100,
child: Stack(
children: [
Container(color: Colors.yellow[100]),
Positioned(
top: 10,
left: 10,
child: Container(width: 40, height: 40, color: Colors.red),
),
const Positioned(
bottom: 10,
right: 10,
child: Text('Stack 层叠'),
),
],
),
);
}
// 4. ListView/GridView 示例
Widget _buildListAndGridDemo() {
return Column(
children: [
SizedBox(
height: 100,
child: ListView(
scrollDirection: Axis.horizontal,
children: List.generate(
5,
(index) => Container(
width: 80,
margin: const EdgeInsets.all(5),
color: Colors.orange[100],
child: Center(child: Text('Item $index')),
),
),
),
),
const SizedBox(height: 10),
GridView.count(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
crossAxisCount: 3,
children: List.generate(
6,
(index) => Container(
margin: const EdgeInsets.all(2),
color: Colors.purple[100],
child: Center(child: Text('Grid $index')),
),
),
),
],
);
}
// 5. Expanded/Flexible 示例
Widget _buildExpandedDemo() {
return Container(
height: 80,
color: Colors.grey[200],
child: Row(
children: [
Expanded(
flex: 2,
child: Container(color: Colors.red, child: const Center(child: Text('Expanded'))),
),
Flexible(
flex: 1,
child: Container(color: Colors.blue, child: const Center(child: Text('Flexible'))),
),
],
),
);
}
// 6. Card 示例
Widget _buildCardDemo() {
return Card(
elevation: 5,
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: const [
Icon(Icons.star, color: Colors.amber, size: 40),
SizedBox(height: 10),
Text('Card 组件示例', style: TextStyle(fontWeight: FontWeight.bold)),
Text('带阴影的卡片式布局'),
],
),
),
);
}
}
三 代码讲解
运行效果
这个示例会展示以下内容:
- 蓝色圆角 Container
- 红绿蓝方块水平排列的 Row
- 黄色背景叠加红色方块和文字的 Stack
- 水平滚动 ListView 和 3列 GridView
- 红蓝比例分割的 Expanded/Flexible
- 带阴影的 Card 卡片
核心容器组件讲解
1. Container
- 用途:全能布局容器,可设置尺寸、边距、颜色等。
- 关键属性:
- margin // 外边距 padding // 内边距 decoration // 装饰(颜色、边框、圆角等)
2. Row 与 Column
- 用途:水平/垂直排列子组件。
- 关键属性:
- mainAxisAlignment // 主轴对齐方式(如居中、两端对齐) crossAxisAlignment // 交叉轴对齐方式
3. Stack
- 用途:层叠布局,结合 Positioned 控制子组件位置。
- 典型场景:图标+文字叠加、浮动按钮。
4. ListView 与 GridView
- 用途:滚动列表和网格布局。
- 注意:
- shrinkWrap: true // 当嵌套在其他滚动组件中时需要设置 scrollDirection // 滚动方向(水平/垂直)
5. Expanded 与 Flexible
- 用途:在 Row/Column 中按比例分配剩余空间。
- 区别:Expanded 必须填满剩余空间Flexible 可以自适应内容大小
6. Card
- 用途:带阴影的卡片式设计。
- 关键属性:
- elevation // 阴影强度 shape // 自定义形状(如圆角半径)
如何调试?
- 复制代码到 lib/main.dart
- 运行 flutter run
- 尝试修改以下参数观察变化:修改 Container 的 margin 和 padding调整 Row 的 mainAxisAlignment改变 Expanded 的 flex 比例
通过这个示例,你可以直观理解 Flutter 容器组件如何协作构建复杂界面。如果需要更高级的布局,可以学习 LayoutBuilder 或 CustomScrollView。
相关推荐
- MySQL合集-mysql5.7及mysql8的一些特性
-
1、Json支持及虚拟列1.1jsonJson在5.7.8原生支持,在8.0引入了json字段的部分更新(jsonpartialupdate)以及两个聚合函数,JSON_OBJECTAGG,JS...
- MySQL 双表架构在房产中介房源管理中的深度实践
-
MySQL房源与价格双表封神:降价提醒实时推送客户房产中介实战:MySQL空间函数精准定位学区房MySQL狠招:JSON字段实现房源标签自由组合筛选房源信息与价格变更联动:MySQL黄金搭档解决客户看...
- MySQL 5.7 JSON 数据类型使用总结
-
从MySQL5.7.8开始,MySQL支持原生的JSON数据类型。MySQL支持RFC7159定义的全部json数据类型,具体的包含四种基本类型(strings,numbers,boolea...
- MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
-
前言提到SQL优化,大多数人想到的还是那些经典套路:建索引、避免全表扫描、优化JOIN顺序…这些确实是基础,但如果你还停留在MySQL5.7时代的优化思维,那就out了。MySQL8.0已经发布好...
- 如何在 MySQL 中使用 JSON 数据(mysql的json函数与实例)
-
在MySQL中学习“NoSQL”MySQL从5.7版本开始就支持JSON格式的数据类型,该数据类型支持JSON文档的自动验证和优化存储和访问。尽管JSON数据最好存储在MongoDB等...
- MySQL中JSON的存储原理(mysql中json字段操作)
-
前言:表中有json字段后,非索引查询性能变得非常糟糕起因是我有一张表,里面有json字段后,而当mysql表中有200w数据的时候,走非索引查询性能变得非常糟糕需要3到5s。因此对mysql的jso...
- mysql 之json字段详解(多层复杂检索)
-
MySQL5.7.8开始支持JSON数据类型。MySQL8.0版本中增加了对JSON类型的索引支持。示例表CREATETABLE`users`(`id`intNOTNULLAU...
- VMware vCenter Server 8.0U3b 发布下载,新增功能概览
-
VMwarevCenterServer8.0U3b发布下载,新增功能概览ServerManagementSoftware|vCenter请访问原文链接:https://sysin.or...
- Spring Boot 3.x 新特性详解:从基础到高级实战
-
1.SpringBoot3.x简介与核心特性1.1SpringBoot3.x新特性概览SpringBoot3.x是建立在SpringFramework6.0基础上的重大版...
- 如何设计Agent的记忆系统(agent记忆方法)
-
最近看了一张画Agent记忆分类的图我觉得分类分的还可以,但是太浅了,于是就着它的逻辑,仔细得写了一下在不同的记忆层,该如何设计和选型先从流程,作用,实力和持续时间的这4个维度来解释一下这几种记忆:1...
- Spring Boot整合MyBatis全面指南:从基础到高级应用(全网最全)
-
一、基础概念与配置1.1SpringBoot与MyBatis简介技术描述优点SpringBoot简化Spring应用开发的框架,提供自动配置、快速启动等特性快速开发、内嵌服务器、自动配置、无需X...
- 5大主流方案对比:MySQL千亿级数据线上平滑扩容实战
-
一、扩容方案剖析1、扩容问题在项目初期,我们部署了三个数据库A、B、C,此时数据库的规模可以满足我们的业务需求。为了将数据做到平均分配,我们在Service服务层使用uid%3进行取模分片,从而将数据...
- PostgreSQL 技术内幕(五)Greenplum-Interconnect模块
-
Greenplum是在开源PostgreSQL的基础上,采用MPP架构的关系型分布式数据库。Greenplum被业界认为是最快最具性价比的数据库,具有强大的大规模数据分析任务处理能力。Greenplu...
- 在实际操作过程中如何避免出现SQL注入漏洞
-
一前言本文将针对开发过程中依旧经常出现的SQL编码缺陷,讲解其背后原理及形成原因。并以几个常见漏洞存在形式,提醒技术同学注意相关问题。最后会根据原理,提供解决或缓解方案。二SQL注入漏洞的原理、形...
- 运维从头到尾安装日志服务器,看这一篇就够了
-
一、rsyslog部署1.1)rsyslog介绍Linux的日志记录了用户在系统上一切操作,看日志去分析系统的状态是运维人员必须掌握的基本功。rsyslog日志服务器的优势:1、日志统一,集中式管理...
- 一周热门
-
-
Python实现人事自动打卡,再也不会被批评
-
Psutil + Flask + Pyecharts + Bootstrap 开发动态可视化系统监控
-
【验证码逆向专栏】vaptcha 手势验证码逆向分析
-
一个解决支持HTML/CSS/JS网页转PDF(高质量)的终极解决方案
-
再见Swagger UI 国人开源了一款超好用的 API 文档生成框架,真香
-
网页转成pdf文件的经验分享 网页转成pdf文件的经验分享怎么弄
-
C++ std::vector 简介
-
系统C盘清理:微信PC端文件清理,扩大C盘可用空间步骤
-
10款高性能NAS丨双十一必看,轻松搞定虚拟机、Docker、软路由
-
python使用fitz模块提取pdf中的图片
-
- 最近发表
-
- MySQL合集-mysql5.7及mysql8的一些特性
- MySQL 双表架构在房产中介房源管理中的深度实践
- MySQL 5.7 JSON 数据类型使用总结
- MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
- 如何在 MySQL 中使用 JSON 数据(mysql的json函数与实例)
- MySQL中JSON的存储原理(mysql中json字段操作)
- mysql 之json字段详解(多层复杂检索)
- VMware vCenter Server 8.0U3b 发布下载,新增功能概览
- Spring Boot 3.x 新特性详解:从基础到高级实战
- 如何设计Agent的记忆系统(agent记忆方法)
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- table.render (33)
- uniapp textarea (33)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)