当前位置:网站首页>【Flutter -- 布局】层叠布局(Stack和Positioned)
【Flutter -- 布局】层叠布局(Stack和Positioned)
2022-07-25 11:39:00 【Kevin-Dev】

Flutter 中使用 Stack 和 Positioned 这两个组件来配合实现绝对定位。Stack 允许子组件堆叠,而 Positioned 用于根据 Stack 的四个角来确定子组件的位置。
Stack
1. 简介
Stack 可以类比 web 中的 absolute,绝对布局。绝对布局一般在移动端开发中用的较少,但是在某些场景下,还是有其作用。当然,能用 Stack 绝对布局完成的,用其他控件组合也都能实现。
2. 属性
alignment:对齐方式,默认是左上角(topStart)。
textDirection:文本的方向,绝大部分不需要处理。
fit:定义如何设置non-positioned节点尺寸,默认为loose。
其中StackFit有如下几种:
- loose:子节点宽松的取值,可以从min到max的尺寸;
- expand:子节点尽可能的占用空间,取max尺寸;
- passthrough:不改变子节点的约束条件。
overflow:超过的部分是否裁剪掉(clipped)。
3. 实例
1. 效果图

3. 代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('层叠布局'),
),
body: Center(
child: Stack(
alignment: const Alignment(0.6, 0.6),
children: <Widget>[
CircleAvatar(
backgroundImage: AssetImage('images/avatar.png'),
radius: 100.0,
),
Container(
decoration: BoxDecoration(
color: Colors.black45
),
child: Text(
'KevinDev',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
)
],
),
)
)
);
}
}
Positioned
1. 简介
绝对定位,用于确定子组件的位置
2. 属性
left、top、right、bottom:他们分别代表离 Stack 左、上、右、下四边的距离。
width、height:指定需要定位元素的宽度和高度。
注:Positioned的width、height和其它地方的意义稍微有点区别,此处用于配合left、top 、right、 bottom来定位组件,
3. 实例
1. 效果图

2. 代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('层叠布局'),
),
body: ConstrainedBox(
constraints: BoxConstraints.expand(),
child: Stack(
alignment:Alignment.center , //指定未定位或部分定位widget的对齐方式
children: <Widget>[
Container(
child: Text("Hello world",style: TextStyle(color: Colors.white)),
color: Colors.blue,
),
Positioned(
left: 18.0,
child: Text("I am Kevin"),
),
Positioned(
top: 18.0,
child: Text("Your friend"),
)
],
),
)
)
);
}
}
边栏推荐
- Ansible
- Pytorch advanced training skills
- Ansible
- 【八】取色器的巧用
- NLP知识----pytorch,反向传播,预测型任务的一些小碎块笔记
- Feign use
- 第一个scrapy爬虫
- Heterogeneous graph neural network for recommendation system problems (ackrec, hfgn)
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化点状条带图、设置palette参数配置不同水平数据点的颜色、设置add参数在点状条带图中添加均值标准差竖线
- Basic concepts of NLP 1
猜你喜欢

Transformer variants (routing transformer, linformer, big bird)

Meta learning (meta learning and small sample learning)

Eureka注册中心开启密码认证-记录

Knowledge maps are used to recommend system problems (mvin, Ctrl, ckan, Kred, gaeat)

PyTorch进阶训练技巧

Dr. water 2

PyTorch可视化

RestTemplate与Ribbon简单使用

氢能创业大赛 | 国家能源局科技司副司长刘亚芳:构建高质量创新体系是我国氢能产业发展的核心

Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge
随机推荐
Zero shot image retrieval (zero sample cross modal retrieval)
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化点状条带图、设置palette参数配置不同水平数据点的颜色、设置add参数在点状条带图中添加均值标准差竖线
R语言组间均值是否相同的成对比较:使用pairwise.t.test函数执行多个分组数据均值的两两成对假设检验
Feign use
GPT plus money (OpenAI CLIP,DALL-E)
从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」
pytorch环境配置及基础知识
投屏收费背后:爱奇艺季度盈利,优酷急了?
After having a meal with trump, I wrote this article
【4】 Layout view and layout toolbar usage
R语言ggplot2可视化:可视化散点图并为散点图中的部分数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(为数据点标签添加线段、指定线段的角度
Basic concepts of NLP 1
MySQL练习二
【二】栅格数据显示拉伸色带(以DEM数据为例)
Jenkins配置流水线
Ansible
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
【九】坐标格网添加以及调整
919. Complete binary tree inserter: simple BFS application problem
Ansible