组合属性
- topLeft、topRight、bottomLeft、bottomRight
BgContainer(
child: Stack(
alignment: Alignment.center,
// 设置填充方式展接受父类约束最大值
fit: StackFit.expand,
children: [
Positioned(
top: 10,
left: 10,
child: getItem(‘topLeft’),
),
Positioned(
top: 10,
right: 10,
child: getItem(‘topRight’),
),
Positioned(
bottom: 10,
left: 10,
child: getItem(‘bottomLeft’),
),
Positioned(
bottom: 10,
right: 10,
child: getItem(‘bottomRight’),
),
],
),
)

- 如果我们将
left和right一起使用呢?
BgContainer(
child: Stack(
alignment: Alignment.center,
// 设置填充方式展接受父类约束最大值
fit: StackFit.expand,
children: [
Positioned(
left: 10,
right: 10,
child: getItem(‘leftRight’),
),
Positioned(
top: 10,
bottom: 10,
child: getItem(‘topBottom’),
),
],
),
)
| leftRight | topBottom |
|---|---|
![]() | ![]() |
- 如果我们将
left或right与width组合呢?
BgContainer(
child: Stack(
alignment: Alignment.center,
// 设置填充方式展接受父类约束最大值
fit: StackFit.expand,
children: [
Positioned(
left: 10,
width: 100,
child: getItem(‘leftWidth 100’),
),
Positioned(
right: 10,
width: 100,
child: getItem(‘RightWidth 100’),
),
Positioned(
top: 10,
height: 100,
child: getItem(‘topHeight 100’),
),
Positioned(
bottom: 10,
height: 100,
child: getItem(‘bottomHeight 100’),
),
],
),
)
| (left | right) & width | (top | bottom) & height | | :—: | :—: | |
|
|
- 如果同纬度 3 个属性组合呢?
BgContainer(
child: Stack(
alignment: Alignment.center,
// 设置填充方式展接受父类约束最大值
fit: StackFit.expand,
children: [
Positioned(
left: 10,
right: 10,
width: 100,
child: getItem(‘leftRightWidth 100’),
)
],
),
)
那肯定是报错啦,刚才看源码的时候已经看到断言说不能这样写啦,因为
left和right 可以确定一个宽度,再设置一个宽度就冲突了
- 如果不同维度 3 个属性组合呢?
// bottomLeftWidth 100
Positioned(
left: 10,
bottom: 10,
width: 100,
child: getItem(‘bottomLeftWidth 100’),
),
// rightTopBottom
Positioned(
right: 10,
top: 10,
bottom: 10,
child: getItem(‘rightTopBottom’),
)

| bottomLeftWidth 100 | rightTopBottom |
|---|---|
![]() | ![]() |
只要是不同维度的随便你怎么组合?
小总结
到这里我们基本对 Positioned 基本的使用就全部聊完了,Positioned 可以对当个子项目进行设置定位和宽高,达到更加精准的参数调配。
Align(对齐)
其实实现我们开头的效果还有更加简单一点的方式,就是使用 Align 进行子项的设置

BgContainer(
child: Stack(
alignment: Alignment.center,
// 设置填充方式展接受父类约束最大值
fit: StackFit.expand,
children: [
Align(
child: getItem(‘5’),
//对齐到右下角
alignment: Alignment.bottomRight,
),
Align(
child: getItem(‘4’),
//对齐到左下角
alignment: Alignment.bottomLeft,
),
//居中
Align(
child: getItem(‘3’),
// 居中是默认值
alignment: Alignment.center,
),
Align(
child: getItem(‘2’),
//对齐到右上角
alignment: Alignment.topRight,
),
Align(
child: getItem(‘1’),
//对齐到左上角
alignment: Alignment.topLeft,
),
],
),
)
对于 Align 中的 alignment 属性我们已经聊过很多了,这里就不多聊了,都是一样的意思。这里我们间 3 号,居中方式改成了 Align 的方式,开头我们使用的 Center 接下来给大家介绍一下。
Center(居中)
我们为什么可以将 Center 替换成 Align 而效果是一样的呢?我们看看源码就会明白了。
Center 源码

Align 源码

看到这里就明白了吧,其实 Center 就是 Align 的一个子类,然后啥也没干,其实我们可以叫他别名,因为 Align 的 alignment 属性默认值就是居中的。
源码仓库
基于 Flutter 🔥 最新版本
- Flutter Widgets 仓库
参考链接
- Positioned (Flutter Widget of the Week)
- Flutter-Positioned
- Align (Flutter Widget of the Week)
- Flutter-Align
- Flutter-Center
关注专栏
- 此文章已收录到下面👇 的专栏,可以直接关注
- 更多文章继续阅读|系列文章持续更新
👏 欢迎点赞➕收藏➕关注,有任何问题随时在下面👇评论,我会第一时间回复哦
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。





既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
最后这里放上我这段时间复习的资料,这个资料也是偶然一位朋友分享给我的,里面包含了腾讯、字节跳动、阿里、百度2019-2021面试真题解析,并且把每个技术点整理成了视频和PDF(知识脉络 + 诸多细节)。
还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,也可以分享给身边好友一起学习。


一起互勉~
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
还有 高级架构技术进阶脑图、高级进阶架构资料 帮助大家学习提升进阶,也可以分享给身边好友一起学习。
[外链图片转存中…(img-ot00Ikc5-1712753335988)]
[外链图片转存中…(img-igXQQo6b-1712753335988)]
一起互勉~
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
本文详细探讨了Flutter中的Positioned组件如何用于精确布局,以及Align的使用如何简化对齐。作者还介绍了如何结合不同维度的属性(如left,right,width,height)进行组合,以及Center与Align的关系。文章最后提到了与布局相关的库和资源,适合Android开发者学习和提升。




5877

被折叠的 条评论
为什么被折叠?



