const FittedBox({
super.key,
this.fit = BoxFit.contain, //默认包含
this.alignment = Alignment.center,
this.clipBehavior = Clip.none, //默认超出不剪裁
super.child,
});
const AspectRatio({
super.key,
required this.aspectRatio, // 宽:高,比如2.0/1.0,比如0.5,反正是个double
super.child,
})
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
),
)
const Baseline({
super.key,
required this.baseline,
required this.baselineType,
super.child,
})
/// TextBaseline枚举
enum TextBaseline {
alphabetic, //对齐字母字符字形底部
ideographic, //对齐表意字符的水平线
}
const FractionallySizedBox({
super.key,
this.alignment = Alignment.center,
this.widthFactor, // 宽因子,父widget宽度*这个指=child的宽度
this.heightFactor, // 高因子,同理
super.child,
})
Container(
color: Colors.blue,
height: 150.0,
width: 150.0,
child: new FractionallySizedBox(
alignment: Alignment.center,
widthFactor: 1.5,
heightFactor: 0.5,
child: xxWidget,
),
)
IntrinsicHeight(
child: Row(
children: [
Container(
width: 50,
color: Colors.red,
),
Container(
width: 28,
height: 50,
color: Colors.red,
),
],
),
),
IntrinsicWidth({ super.key, this.stepWidth, this.stepHeight, super.child })
IntrinsicWidth(
stepWidth: 100,
child: Container(
color: Colors.blue,
child: Center(
child: Container(
color: Colors.red,
width: 50,
height: 50,
),
),
),
),
这段代码IntrinsicWidth的宽度就是100
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({ super.key, this.offstage = true, super.child })
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,
),
),
),
);
}
Row(
children:[
Container(),
SizedBox(width:100),
Container(),
]
)
这样两个Container之间就有100间距了
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,
),
)
const Transform({
super.key,
required this.transform,
this.origin,
this.alignment,
this.transformHitTests = true,
this.filterQuality,
super.child,
});
Transform.rotate(
angle: pi/4,
child: const Icon(
"图片地址",
size: 200,
color: Colors.blue,
),
);
Transform.scale(
scale: 0.5,
child: const Icon(
"图片地址",
size: 200,
color: Colors.blue,
),
);
Transform.translate(
offset:const Offset(50.0, 0),
child: const Icon(
"图片地址",
size: 200,
color: Colors.blue,
),
);
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);
}
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);
}
}