flutter Container容器布局

Container容器布局

Container是一个拥有绘制、定位、调整大小的 widget,是开发中最常用、最基础的组件,和html中的div相似
相关属性

Container({
    super.key,
    this.alignment,
    this.padding,
    this.color,
    this.decoration,
    this.foregroundDecoration,
    double? width,
    double? height,
    BoxConstraints? constraints,
    this.margin,
    this.transform,
    this.transformAlignment,
    this.child,
    this.clipBehavior = Clip.none,
  }) 

alignment

aligment可以设置内部组件child的位置对齐方式
alignment: Alignment.bottomLeft表示在左下角

transform

对控件进行变换,其中包括位移,旋转,缩放,沿x轴或者y轴进行旋转

位移

transform: Matrix4.translationValues(x, y, z),

其中x,y,z分别表示沿该方向的移动

旋转

transform: Matrix4.rotationZ(0.2),

width与height

表示整个容器的宽和高

margin(外边距)与padding(内边距)

margin: const EdgeInsets.all(21), 设置所有方向的外边距为21
也可以设置为EdgeInsets.fromLTRB(left, top, right, bottom)分别数据各个方向的边距
margin和padding用法一样

child

相当于容器内的子元素,可以添加文本控件

decoration

可以在里面定义容器的背景设定(边框、圆角、阴影、形状、渐变、背景图像等)
有以下属性

 const BoxDecoration({
    this.color,  //背景颜色
    this.image,  //背景图片
    this.border,  //边框
    this.borderRadius, //边框圆角
    this.boxShadow, //阴影
    this.gradient,  //背景渐变
    this.backgroundBlendMode,
    this.shape = BoxShape.rectangle,
     })

更多参考:https://blog.csdn.net/chenlove1/article/details/83627831

gradient

在这个渐变属性中有多种渐变方式
扫描式渐变 gradient: SweepGradient
在这里插入图片描述

线性渐变 gradient: LinearGradient
在这里插入图片描述

径向渐变 gradient: RadialGradient
在这里插入图片描述

代码示例

 Center(
      child: Container(
        //相当于div
        width: 200,
        height: 100,
        alignment: Alignment.center, //设置内部组件的位置方式
        decoration: BoxDecoration(
          // color: Color.fromRGBO(240, 230, 140, 1), //设置填充
          border: Border.all(color: Colors.deepOrange, width: 2), //设置边框以及变宽颜色
          borderRadius: BorderRadius.circular(5), //设置圆角边框效果
          gradient: LinearGradient(
              colors: [Colors.red, Colors.yellow]), //设置线性渐变从蓝色到黄色
          boxShadow: [
            BoxShadow(
              color: Colors.indigoAccent, //设置阴影颜色
              blurRadius: 20, //设置阴影效果
            )
          ],
        ),
        child: Text(  //设置为文本控件
          "你好flutter!",   //设置文字内容
          textDirection: TextDirection.ltr,   //设置文字的方向
          style: TextStyle(color: Color.fromRGBO(0, 0, 255, 1), fontSize: 19),//设置文字样式属性
        ),
      ),
    );

images与icon

图片加载分为两种,一种是本地加载(asset),一种是远程加载(network)

圆形图片的设置
代码演示:

 Center(
      child: Container(
        width: 50,   //设置容器宽高
        height: 50,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(100), //设置圆形边框
            image: const DecorationImage(
              image: NetworkImage(
                  "http://xxxxx.xx:2333/upload/2022/11/4dffadc42c8c2eac3caf44d569b04cf.jpg"),   //远程加载图片
              fit: BoxFit.cover,   //设置超出容器的剪切方式
            )),
        // child: Image.network("xxxxxxx.xx:2333/upload/2022/11/4dffadc42c8c2eac3caf44d569b04cf.jpg"),
      ),
    );

方式二

ClipOval(
      child: Image.network(
        "http://xxxxxx/upload/2022/11/4dffadc42c8c2eac3caf44d569b04cf.jpg",
        width: 50,
        height: 50,
        fit: BoxFit.cover,
      ),
    );
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值