build method
最终构建方法 - 一次性创建Container
Implementation
Widget build() {
// 构建 BoxDecoration
BoxDecoration? decoration;
// 处理边框:合并 Border 和 BorderDirectional
Border? finalBorder = _border;
if (_borderDirectional != null) {
// 如果有 BorderDirectional,需要与现有 Border 合并
// BoxDecoration 不支持 BorderDirectional,我们需要根据 TextDirection 转换
// 使用 Builder 来获取当前的 TextDirection
final borderDir = _borderDirectional!;
final textDir = _textDirection;
// 根据 TextDirection 决定 start/end 对应 left/right
BorderSide leftSide = borderDir.start;
BorderSide rightSide = borderDir.end;
// 如果指定了 TextDirection 且为 RTL,交换 left 和 right
if (textDir == TextDirection.rtl) {
leftSide = borderDir.end;
rightSide = borderDir.start;
}
// 合并边框
if (_border != null) {
finalBorder = Border(
top: _border!.top,
bottom: _border!.bottom,
left: borderDir.start.width > 0 ? leftSide : _border!.left,
right: borderDir.end.width > 0 ? rightSide : _border!.right,
);
} else {
finalBorder = Border(
top: borderDir.top,
bottom: borderDir.bottom,
left: leftSide,
right: rightSide,
);
}
}
if (_backgroundColor != null ||
finalBorder != null ||
_borderRadius != null ||
_boxShadow != null ||
_gradient != null) {
decoration = BoxDecoration(
color: _gradient == null ? _backgroundColor : null, // 如果有渐变就不设置color
border: finalBorder,
borderRadius: _borderRadius,
boxShadow: _boxShadow,
gradient: _gradient,
backgroundBlendMode: _backgroundBlendMode,
shape: _shape,
);
}
// 构建约束
BoxConstraints? constraints = _constraints;
if (_minWidth != null || _maxWidth != null || _minHeight != null || _maxHeight != null) {
constraints = BoxConstraints(
minWidth: _minWidth ?? (constraints?.minWidth ?? 0),
maxWidth: _maxWidth ?? (constraints?.maxWidth ?? double.infinity),
minHeight: _minHeight ?? (constraints?.minHeight ?? 0),
maxHeight: _maxHeight ?? (constraints?.maxHeight ?? double.infinity),
);
}
// 如果有 BorderDirectional 且需要 RTL 支持,使用 Builder 获取 TextDirection
Widget container;
if (_borderDirectional != null && _textDirection == null) {
// 如果没有指定 TextDirection,使用 Builder 从上下文获取
container = Builder(
builder: (context) {
final textDir = Directionality.of(context);
Border? dynamicBorder = _border;
if (_borderDirectional != null) {
final borderDir = _borderDirectional!;
BorderSide leftSide = borderDir.start;
BorderSide rightSide = borderDir.end;
if (textDir == TextDirection.rtl) {
leftSide = borderDir.end;
rightSide = borderDir.start;
}
if (_border != null) {
dynamicBorder = Border(
top: _border!.top,
bottom: _border!.bottom,
left: borderDir.start.width > 0 ? leftSide : _border!.left,
right: borderDir.end.width > 0 ? rightSide : _border!.right,
);
} else {
dynamicBorder = Border(
top: borderDir.top,
bottom: borderDir.bottom,
left: leftSide,
right: rightSide,
);
}
}
BoxDecoration? dynamicDecoration = decoration;
if (dynamicBorder != finalBorder && dynamicDecoration != null) {
dynamicDecoration = BoxDecoration(
color: dynamicDecoration.color,
border: dynamicBorder,
borderRadius: dynamicDecoration.borderRadius,
boxShadow: dynamicDecoration.boxShadow,
gradient: dynamicDecoration.gradient,
backgroundBlendMode: dynamicDecoration.backgroundBlendMode,
shape: dynamicDecoration.shape,
);
}
return Container(
width: _width,
height: _height,
alignment: _alignment,
padding: _padding,
margin: _margin,
constraints: constraints,
transform: _transform,
transformAlignment: _transformAlignment,
decoration: dynamicDecoration ?? (dynamicBorder != null ? BoxDecoration(border: dynamicBorder) : null),
foregroundDecoration: _foregroundDecoration,
child: child,
);
},
);
} else {
container = Container(
width: _width,
height: _height,
alignment: _alignment,
padding: _padding,
margin: _margin,
constraints: constraints,
transform: _transform,
transformAlignment: _transformAlignment,
decoration: decoration,
foregroundDecoration: _foregroundDecoration,
child: child,
);
}
// 如果设置了 aspect ratio,使用 AspectRatio widget 包裹
if (_aspectRatio != null) {
container = AspectRatio(
aspectRatio: _aspectRatio!,
child: container,
);
}
// 如果设置了 z-index,使用 Transform 来模拟层级
if (_zIndex != null) {
container = Transform(
transform: Matrix4.identity()..setTranslation(vm.Vector3(0.0, 0.0, _zIndex!.toDouble())),
child: container,
);
}
// 如果需要定位,包装成Positioned或PositionedDirectional
if (_isPositioned) {
// 如果使用了 start/end,使用 PositionedDirectional 以支持 RTL
if (_positionStart != null || _positionEnd != null) {
return PositionedDirectional(
top: _positionTop,
start: _positionStart,
end: _positionEnd,
bottom: _positionBottom,
width: _positionWidth,
height: _positionHeight,
child: container,
);
} else {
// 否则使用普通的 Positioned
return Positioned(
top: _positionTop,
right: _positionRight,
bottom: _positionBottom,
left: _positionLeft,
width: _positionWidth,
height: _positionHeight,
child: container,
);
}
}
return container;
}