本文介绍: Container、Padding、Center、Align、FittedBox、AspectRatio、ConstrainedBox、Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth、LimitedBox、Offstage、OverflowBox、SizedBox、SizedOverflowBox、Transform、CustomSingleChildLayout

布局

基本布局

Container

参数解释

补充

FittedBox

构造函数

const FittedBox({
    super.key,
    this.fit = BoxFit.contain,  //默认包含
    this.alignment = Alignment.center,
    this.clipBehavior = Clip.none,  //默认超出不剪裁
    super.child,
});

常见实践

补充

AspectRatio

构造函数

const AspectRatio({
    super.key,
    required this.aspectRatio,  // 宽:高,比如2.0/1.0,比如0.5,反正是个double
    super.child,
})

常见实践

ConstrainedBox

构造函数

ConstrainedBox({
    super.key,
    required this.constraints,
    super.child,
})
/// constraints构造函数
const BoxConstraints({
    this.minWidth = 0.0,
    this.maxWidth = double.infinity,
    this.minHeight = 0.0,
    this.maxHeight = double.infinity,
})

代码

ConstrainedBox(
  constraints: BoxConstraints(
    minWidth: double.infinity, //宽度尽量大
    minHeight: 50.0 //最小高度为50
  ),
  child: Container(
      height: 10.0, 
      child: xx 
  ),
)

Baseline

构造函数

const Baseline({
    super.key,
    required this.baseline,
    required this.baselineType,
    super.child,
})
/// TextBaseline枚举
enum TextBaseline {
  alphabetic,  //对齐字母字符字形底部
  ideographic,  //对齐表意字符的水平线
} 
  • 对齐字母字符字形底部,指的是如jpg这些字母,基线都在靠上一点,而“对齐表意字符的水平线”,则当汉字和jpg这些字母在一起时,底部都是对齐的。
  • baseline 可以为负值,会把基线下调

常见实践

FractionallySizedBox

构造函数

const FractionallySizedBox({
    super.key,
    this.alignment = Alignment.center,
    this.widthFactor,  // 宽因子,父widget宽度*这个指=child的宽度
    this.heightFactor,  // 高因子,同理
    super.child,
})
  • 若不设置宽高因子,则对应方向默认填满父widget

示例

Container(
    color: Colors.blue,
    height: 150.0,
    width: 150.0,
    child: new FractionallySizedBox(
        alignment: Alignment.center,
        widthFactor: 1.5,
        heightFactor: 0.5,
        child: xxWidget,
    ),
)
  • xxWidget的宽度为1501.5,高度为1500.5

IntrinsicHeight

示例

IntrinsicHeight(
    child: Row(
        children: [
            Container(
                width: 50,
                color: Colors.red,
            ),
            Container(
                width: 28,
                height: 50,
                color: Colors.red,
            ),
        ],
    ),
),

IntrinsicWidth

构造函数

IntrinsicWidth({ super.key, this.stepWidth, this.stepHeight, super.child })

代码

  • 展示用步长的,不用步长或者步长=1,和IntrinsicHeight用法一致
IntrinsicWidth(
    stepWidth: 100,
    child: Container(
        color: Colors.blue,
        child: Center(
        child: Container(
            color: Colors.red,
            width: 50,
            height: 50,
        ),
        ),
    ),
),

这段代码IntrinsicWidth的宽度就是100

LimitedBox

构造函数

const LimitedBox({
    super.key,
    this.maxWidth = double.infinity,
    this.maxHeight = double.infinity,
    super.child,
})

代码

Container(
    color: Colors.blue,
    child: UnconstrainedBox(
        child: LimitedBox(
        maxWidth: 100,
        maxHeight: 100,
        child: Text(
            '啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊'),
        ),
    ),
),

用UnconstrainedBox就是为了让LimitedBox没有外部限制

Offstage

构造函数

Offstage({ super.key, this.offstage = true, super.child })

OverflowBox

构造函数

const OverflowBox({
    super.key,
    this.alignment = Alignment.center,
    this.minWidth,
    this.maxWidth,
    this.minHeight,
    this.maxHeight,
    super.child,
});

代码

@override
Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
        title: Text('OverflowBox'),
        ),
        body: Container(
        color: Colors.blue,
        height: 100,
        child: OverflowBox(
            alignment: Alignment.topCenter,
            minWidth: 20,
            maxWidth: 100,
            maxHeight: 200,
            minHeight: 20,
            child: Container(
            width: 50,
            height: 250,
            color: Colors.red,
            ),
        ),
        ),
    );
}
  • 如上代码,就会让OverflowBox包裹的Container,超过OverflowBox的父布局Container约束,形成类似Stack效果

SizedBox

代码

Row(
    children:[
        Container(),
        SizedBox(width:100),
        Container(),
    ]
)

这样两个Container之间就有100间距

SizedOverflowBox

构造函数

const SizedOverflowBox({
    super.key,
    required this.size,
    this.alignment = Alignment.center,
    super.child,
})

代码示例

SizedOverflowBox(
    size: Size(100.0, 200.0),
    child: Container(
        color: Colors.red,
        width: 200.0,
        height: 100.0,
    ),
)

Transform

构造函数

const Transform({
    super.key,
    required this.transform,
    this.origin,
    this.alignment,
    this.transformHitTests = true,
    this.filterQuality,
    super.child,
});

旋转child

  • pi/4就是 180 / 4 = 45 度
Transform.rotate(
    angle: pi/4,
    child: const Icon(
        "图片地址",
        size: 200,
        color: Colors.blue,
    ),
);

缩放child

Transform.scale(
    scale: 0.5,
    child: const Icon(
        "图片地址",
        size: 200,
        color: Colors.blue,
    ),
);

平移child

Transform.translate(
    offset:const Offset(50.0, 0),
    child: const Icon(
        "图片地址",
        size: 200,
        color: Colors.blue,
    ),
);

CustomSingleChildLayout

构造函数

const CustomSingleChildLayout({
    super.key,
    required this.delegate,
    super.child,
})
/// SingleChildLayoutDelegate定义
abstract class SingleChildLayoutDelegate {

  const SingleChildLayoutDelegate({ Listenable? relayout }) : _relayout = relayout;

  final Listenable? _relayout;

///获取父容器约束条件,来确定layout的大小
  Size getSize(BoxConstraints constraints) => constraints.biggest;

///确定child的约束
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) => constraints;

///确定child的位置,返回一个相对parent偏移值;size是layout的大小,由getSize确定;childSize由getConstraintsForChild得出的Constraints对child进行约束,得到child自身的size
  Offset getPositionForChild(Size size, Size childSize) => Offset.zero;

///是否需要relayout
  bool shouldRelayout(covariant SingleChildLayoutDelegate oldDelegate);
}

delegate代码

class _MyDelegate extends SingleChildLayoutDelegate {
  @override
  Size getSize(BoxConstraints constraints) {
    //用父容器约束条件
    return super.getSize(constraints);
  }

  @override
  bool shouldRelayout(covariant SingleChildLayoutDelegate oldDelegate) {
    //不需要relayout
    return false;
  }

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    //child的大小
    var childWidth = min(constraints.maxWidth, constraints.maxHeight);
    var childBoxConstraints = BoxConstraints.tight(
      Size(childWidth / 2, childWidth / 2),
    );
    return childBoxConstraints;
  }

  @override
  Offset getPositionForChild(Size size, Size childSize) {
    // 确定child的位置,返回一个相对parent偏移值
    // size由getSize确定
    // childSize由getConstraintsForChild得出的Constraints对child进行约束,得到child自身的size
    var dx = (size.width - childSize.width) / 2;
    var dy = (size.height - childSize.height) / 2;
    return Offset(dx, dy);
  }
}

原文地址:https://blog.csdn.net/sadkhkuhkjasga/article/details/134727276

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_33648.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注