build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  final parsedStyles = SmartStyleParser.parseClassName(
    className ?? '',
    'Container',
    context,
  );

  Widget? containerChild = child;

  // 如果设置了 flex 相关样式,需要用 Flex 包裹子组件
  if (parsedStyles.containsKey('display') && parsedStyles['display'] == 'flex') {
    containerChild = Flex(
      direction: parsedStyles['direction'] ?? Axis.horizontal,
      mainAxisAlignment: parsedStyles['mainAxisAlignment'] ?? MainAxisAlignment.start,
      crossAxisAlignment: parsedStyles['crossAxisAlignment'] ?? CrossAxisAlignment.center,
      children: child != null ? [child!] : [],
    );
  }

  // 构建装饰
  Decoration? finalDecoration = decoration;
  if (finalDecoration == null) {
    if (parsedStyles.containsKey('gradient')) {
      // 使用渐变背景
      finalDecoration = BoxDecoration(
        gradient: parsedStyles['gradient'],
        borderRadius: borderRadius ?? parsedStyles['borderRadius'],
        boxShadow: boxShadow ?? parsedStyles['boxShadow'],
        border: border ?? parsedStyles['border'],
      );
    } else {
      // 使用纯色背景
      finalDecoration = BoxDecoration(
        color: backgroundColor ?? parsedStyles['backgroundColor'],
        borderRadius: borderRadius ?? parsedStyles['borderRadius'],
        boxShadow: boxShadow ?? parsedStyles['boxShadow'],
        border: border ?? parsedStyles['border'],
      );
    }
  }

  // 处理尺寸约束 - 使用 ConstrainedBox 包装而不是直接设置 constraints
  Widget? constrainedChild = containerChild;

  // 安全的约束处理 - 只在真正需要时创建约束
  bool hasConstraints = false;
  double? minW, maxW, minH, maxH;

  // 检查并验证宽度约束
  if (parsedStyles.containsKey('minWidth')) {
    final value = parsedStyles['minWidth'];
    if (value is double && value.isFinite && value >= 0) {
      minW = value;
      hasConstraints = true;
    }
  }

  if (parsedStyles.containsKey('maxWidth')) {
    final value = parsedStyles['maxWidth'];
    if (value is double && value.isFinite && value > 0) {
      maxW = value;
      hasConstraints = true;
    }
  }

  // 检查并验证高度约束
  if (parsedStyles.containsKey('minHeight')) {
    final value = parsedStyles['minHeight'];
    if (value is double && value.isFinite && value >= 0) {
      minH = value;
      hasConstraints = true;
    }
  }

  if (parsedStyles.containsKey('maxHeight')) {
    final value = parsedStyles['maxHeight'];
    if (value is double && value.isFinite && value > 0) {
      maxH = value;
      hasConstraints = true;
    }
  }

  // 确保最小值不大于最大值
  if (minW != null && maxW != null && minW > maxW) {
    maxW = minW;
  }
  if (minH != null && maxH != null && minH > maxH) {
    maxH = minH;
  }

  // 只在有有效约束时才创建 ConstrainedBox
  if (hasConstraints && constrainedChild != null) {
    try {
      constrainedChild = ConstrainedBox(
        constraints: BoxConstraints(
          minWidth: minW ?? 0.0,
          maxWidth: maxW ?? double.infinity,
          minHeight: minH ?? 0.0,
          maxHeight: maxH ?? double.infinity,
        ),
        child: constrainedChild,
      );
    } catch (e) {
      // 如果约束创建失败,使用原始子组件
      constrainedChild = containerChild;
    }
  }

  Widget finalContainer = Container(
    width: width ?? parsedStyles['width'],
    height: height ?? parsedStyles['height'],
    padding: padding ?? parsedStyles['padding'],
    margin: margin ?? parsedStyles['margin'],
    alignment: alignment ?? parsedStyles['alignment'],
    clipBehavior: clipBehavior,
    transform: transform,
    transformAlignment: transformAlignment,
    foregroundDecoration: foregroundDecoration,
    decoration: finalDecoration,
    child: constrainedChild,
  );

  // 检查是否需要处理溢出
  if (parsedStyles.containsKey('overflow') || className?.contains('overflow-') == true) {
    if (parsedStyles['overflow'] == 'scroll' || className?.contains('overflow-scroll') == true) {
      // 将子组件包装在 SingleChildScrollView 中
      Widget scrollableChild = containerChild ?? const SizedBox.shrink();
      finalContainer = Container(
        width: width ?? parsedStyles['width'],
        height: height ?? parsedStyles['height'],
        padding: padding ?? parsedStyles['padding'],
        margin: margin ?? parsedStyles['margin'],
        alignment: alignment ?? parsedStyles['alignment'],
        clipBehavior: clipBehavior,
        constraints: constraints,
        transform: transform,
        transformAlignment: transformAlignment,
        foregroundDecoration: foregroundDecoration,
        decoration: finalDecoration,
        child: SingleChildScrollView(
          child: scrollableChild,
        ),
      );
    } else if (parsedStyles['overflow'] == 'hidden' || className?.contains('overflow-hidden') == true) {
      // 对于 overflow-hidden,使用 ClipRect 裁剪内容
      Widget clippedChild = containerChild ?? const SizedBox.shrink();
      finalContainer = Container(
        width: width ?? parsedStyles['width'],
        height: height ?? parsedStyles['height'],
        padding: padding ?? parsedStyles['padding'],
        margin: margin ?? parsedStyles['margin'],
        alignment: alignment ?? parsedStyles['alignment'],
        clipBehavior: Clip.hardEdge,
        constraints: constraints,
        transform: transform,
        transformAlignment: transformAlignment,
        foregroundDecoration: foregroundDecoration,
        decoration: finalDecoration,
        child: ClipRect(
          child: clippedChild,
        ),
      );
    }
  }

  return finalContainer;
}