flutter软件开发笔记08-容器使用方法
liuian 2025-05-21 14:59 27 浏览
在 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。
相关推荐
- python入门到脱坑函数—定义函数_如何定义函数python
-
Python函数定义:从入门到精通一、函数的基本概念函数是组织好的、可重复使用的代码块,用于执行特定任务。在Python中,函数可以提高代码的模块性和重复利用率。二、定义函数的基本语法def函数名(...
- javascript函数的call、apply和bind的原理及作用详解
-
javascript函数的call、apply和bind本质是用来实现继承的,专业点说法就是改变函数体内部this的指向,当一个对象没有某个功能时,就可以用这3个来从有相关功能的对象里借用过来...
- JS中 call()、apply()、bind() 的用法
-
其实是一个很简单的东西,认真看十分钟就从一脸懵B到完全理解!先看明白下面:例1obj.objAge;//17obj.myFun()//小张年龄undefined例2shows(...
- Pandas每日函数学习之apply函数_apply函数python
-
apply函数是Pandas中的一个非常强大的工具,它允许你对DataFrame或Series中的数据应用一个函数,可以是自定义的函数,也可以是内置的函数。apply可以作用于DataF...
- Win10搜索不习惯 换个设定就好了_window10搜索用不了怎么办
-
Windows10的搜索功能是真的方便,这点用惯了Windows10的小伙伴应该都知道,不过它有个小问题,就是Windows10虽然会自动联网搜索,但默认使用微软自家的Bing搜索引擎和Edge...
- 面试秘籍:call、bind、apply的区别,面试官为什么总爱问这三位?
-
引言你有没有发现,每次JavaScript面试,面试官总爱问你call、bind和apply的区别?好像这三个方法成了通关密码,掌握了它们,就能顺利过关。其实不难理解,面试官问这些问题,不...
- 记住这8招,帮你掌握“追拍“摄影技法—摄影早自习第422日
-
杨海英同学提问:请问叶梓老师,我练习追拍时,总也不能把运动的人物拍清晰,速度一般掌握在1/40-1/60,请问您如何把追拍拍的清晰?这跟不同的运动形式有关系吗?请您给讲讲要点,谢谢您!摄影:Damia...
- [Sony] 有点残酷的测试A7RII PK FS7
-
都是好机!手中利器!主要是最近天天研究fs5,想知道fs5与a7rii后期匹配问题,苦等朋友的fs5月底到货,于是先拿手里现有的fs7小测一下,十九八九也能看到fs5的影子,另外也了解一下fs5k标配...
- AndroidStudio_Android使用OkHttp发起Http请求
-
这个okHttp的使用,其实网络上有很多的案例的,但是,如果以前没用过,copy别人的直接用的话,可以发现要么导包导不进来,要么,人家给的代码也不完整,这里自己整理一下.1.引入OkHttp的jar...
- ESL-通过事件控制FreeSWITCH_es事务控制
-
通过事件提供的最底层控制机制,允许我们有效地利用工具箱,适时选择使用其中的单个工具。FreeSWITCH是一个核心交换与混合矩阵,它周围有几十个模块提供各种功能特性。我们完全控制了所有的即时信息,这些...
- 【调试】perf和火焰图_perf生成火焰图
-
简介perf是linux上的性能分析工具,perf可以对event进行统计得到event的发生次数,或者对event进行采样,得到每次event发生时的相关数据(cpu、进程id、运行栈等),利用这些...
- 文本检索控件也玩安卓?dtSearch Engine发布Android测试版
-
dtSearchEngineforLinux(原生64-bit/32-bitC++和JavaAPIs)和dtSearchEngineforWin&.NET(原生64-bi...
- 网站后台莫名增加N个管理员,记一次SQL注入攻击
-
网站没流量,但却经常被SQL注入光顾。最近,网站真的很奇怪,网站后台不光莫名多了很多“管理员”,所有的Wordpres插件还会被自动暂停,导致一些插件支持的页面,如WooCommerce无法正常访问、...
- 多元回归树分析Multivariate Regression Trees,MRT
-
多元回归树(MultivariateRegressionTrees,MRT)是单元回归树的拓展,是一种对一系列连续型变量递归划分成多个类群的聚类方法,是在决策树(decision-trees)基础...
- JMETER性能测试_JMETER性能测试指标
-
jmeter为性能测试提供了一下特色:jmeter可以对测试静态资源(例如js、html等)以及动态资源(例如php、jsp、ajax等等)进行性能测试jmeter可以挖掘出系统最大能处...
- 一周热门
-
-
【验证码逆向专栏】vaptcha 手势验证码逆向分析
-
Python实现人事自动打卡,再也不会被批评
-
Psutil + Flask + Pyecharts + Bootstrap 开发动态可视化系统监控
-
一个解决支持HTML/CSS/JS网页转PDF(高质量)的终极解决方案
-
再见Swagger UI 国人开源了一款超好用的 API 文档生成框架,真香
-
网页转成pdf文件的经验分享 网页转成pdf文件的经验分享怎么弄
-
C++ std::vector 简介
-
飞牛OS入门安装遇到问题,如何解决?
-
系统C盘清理:微信PC端文件清理,扩大C盘可用空间步骤
-
10款高性能NAS丨双十一必看,轻松搞定虚拟机、Docker、软路由
-
- 最近发表
-
- python入门到脱坑函数—定义函数_如何定义函数python
- javascript函数的call、apply和bind的原理及作用详解
- JS中 call()、apply()、bind() 的用法
- Pandas每日函数学习之apply函数_apply函数python
- Win10搜索不习惯 换个设定就好了_window10搜索用不了怎么办
- 面试秘籍:call、bind、apply的区别,面试官为什么总爱问这三位?
- 记住这8招,帮你掌握“追拍“摄影技法—摄影早自习第422日
- [Sony] 有点残酷的测试A7RII PK FS7
- AndroidStudio_Android使用OkHttp发起Http请求
- ESL-通过事件控制FreeSWITCH_es事务控制
- 标签列表
-
- 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)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)