Flutter之层叠布局Stack、Positioned

     Flutter中使用StackPositioned这两个组件来配合实现绝对定位。Stack允许子组件堆叠,而Positioned用于根据Stack的四个角来确定子组件的位置。

一.属性列表

     Stack

Stack({
  this.alignment = AlignmentDirectional.topStart,  
  this.textDirection,
  this.fit = StackFit.loose,
  this.overflow = Overflow.clip,
  List<Widget> children = const <Widget>[],
})
  • alignment:此参数决定如何去对齐没有定位(没有使用Positioned)或部分定位的子组件。
  • textDirection:与RowWraptextDirection的功能一样,都用于确定alignment对齐的参考系,即:textDirection的值为TextDirection.ltr,则alignmentstart代表左,end代表右,即从左往右的顺序;textDirection的值为TextDirection.rtl,则alignment的start代表右,end代表左,即从右往左的顺序。
  • fit:此参数用于确定没有定位的子组件如何去适应Stack的大小。
  • overflow:此属性决定如何显示超出Stack显示空间的子组件。

 Positioned 

const Positioned({
  Key? key,
  this.left, //子元素距离左侧的距离
  this.top,  //子元素距离顶部的距离
  this.right, //子元素距离右侧的距离
  this.bottom, //子元素距离底部的距离
  this.width,
  this.height,
  required Widget child,
})

二.示例

 上图的源代码如下(仅截取使用到Stack、Positioned部分)

Container(
        child: Stack(
            children: [
              Container(
                height: 160.0,
                width: 290.0,
                margin: const EdgeInsets.only(top: 75,left: 20),
                decoration: const BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey,
                      offset: Offset(0.0, 0.1),//阴影xy轴偏移量
                      blurRadius: 10,//阴影模糊程度
                      spreadRadius: 0.5//阴影扩散测程度
                    ),
                  ],
                ),
        ),
              Container(
                height: 160.0,
                width: 330.0,
                margin: const EdgeInsets.only(top: 60),
                decoration: const BoxDecoration(
                  color: Colors.pinkAccent,
                    boxShadow: [
                    BoxShadow(
                        color: Colors.grey,
                        offset: Offset(0.0, 0.1),//阴影xy轴偏移量
                        blurRadius: 10,//阴影模糊程度
                        spreadRadius: 0.5//阴影扩散测程度
                    ),
                  ],
                ),
              ),
              Positioned(
                bottom: 20,
                  child: Image.asset(
                   "images/Ding.jpeg",
                    width: 110,
                    height: 250,
                  ),
              ),
              const Positioned(
                top: 65,
                left: 130,
                child:Text(
                  "Seven",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 30,
                  ),
                ) ,
              )
          ],
        )
    ),

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值