Flutter——最详细(Positioned)使用教程

Positioned是Flutter框架中用于在Stack布局中精确控制子项位置的小组件。它可以设置width、height以及top、bottom、left、right属性来定位子项。当宽高不设时,需指定四个边缘位置;若四边都设为0,则子项会全屏铺满。示例代码展示了不同配置下的Positioned用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Positioned简介

创建一个小组件,用于控制 [Stack] 的子项的位置。

使用场景:

可以通过坐标的形式来摆放控件的位置,堆叠布局时可以使用;

属性作用
width宽度
height高度
top
bottom
left
right

注意事项:

1.如果设置了宽高,则left和right不能同时存在,top和bottom不能同时存在;
2.未设置宽高,则需要设置顶、底、左、右几个属性;
3.top、bottom、left、right 都设置为零时,则铺满全屏;

属性都为0时,则全屏铺满

          Positioned(
             top: 0,
             bottom: 0,
             left: 0,
             right: 0,
            child: Container(
              color: ColorsUtils.randomColor(),
              child: Text("1"),
            ),
          )

在这里插入图片描述

顶、左都为50的间距时

Stack(
        alignment: Alignment.topLeft,
        children: [
          Positioned(
            width: 150,
            height: 150,
            // top: 10,
            // bottom: 10,
            // left: 10,
            // right: 10,
            child: Container(
              color: ColorsUtils.randomColor(),
              child: Text("1"),
            ),
          ),
          Positioned(
            width: 150,
            height: 150,
            top: 50,
            // bottom: 10,
            left: 50,
            // right: 10,
            child: Container(
              color: ColorsUtils.randomColor(),
              child: Text("2"),
            ),
          ),
        ],
      )

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值