build method

Widget build()

最终构建方法 - 一次性创建Container

Implementation

Widget build() {
  // 构建 BoxDecoration
  BoxDecoration? decoration;

  if (_backgroundColor != null ||
      _border != null ||
      _borderRadius != null ||
      _boxShadow != null ||
      _gradient != null) {
    decoration = BoxDecoration(
      color: _gradient == null ? _backgroundColor : null, // 如果有渐变就不设置color
      border: _border,
      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),
    );
  }

  Widget container = Container(
    width: _width,
    height: _height,
    alignment: _alignment,
    padding: _padding,
    margin: _margin,
    constraints: constraints,
    transform: _transform,
    transformAlignment: _transformAlignment,
    decoration: decoration,
    foregroundDecoration: _foregroundDecoration,
    child: child,
  );

  // 如果设置了 z-index,使用 Transform 来模拟层级
  if (_zIndex != null) {
    container = Transform(
      transform: Matrix4.identity()..setTranslation(vm.Vector3(0.0, 0.0, _zIndex!.toDouble())),
      child: container,
    );
  }

  // 如果需要定位,包装成Positioned
  if (_isPositioned) {
    return Positioned(
      top: _positionTop,
      right: _positionRight,
      bottom: _positionBottom,
      left: _positionLeft,
      width: _positionWidth,
      height: _positionHeight,
      child: container,
    );
  }

  return container;
}