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'],
      );
    }
  }

  // 构建约束条件
  BoxConstraints? finalConstraints = constraints;
  if (parsedStyles['minWidth'] != null || parsedStyles['maxWidth'] != null ||
      parsedStyles['minHeight'] != null || parsedStyles['maxHeight'] != null) {

    // 确保约束值在合理范围内,避免溢出
    double minWidth = parsedStyles['minWidth'] ?? 0.0;
    double maxWidth = parsedStyles['maxWidth'] ?? double.infinity;
    double minHeight = parsedStyles['minHeight'] ?? 0.0;
    double maxHeight = parsedStyles['maxHeight'] ?? double.infinity;

    // 限制最大值以防止溢出
    if (maxWidth != double.infinity && maxWidth > 2000) {
      maxWidth = 2000;
    }
    if (maxHeight != double.infinity && maxHeight > 2000) {
      maxHeight = 2000;
    }

    finalConstraints = BoxConstraints(
      minWidth: minWidth,
      maxWidth: maxWidth,
      minHeight: minHeight,
      maxHeight: maxHeight,
    );

    // 如果已有constraints,则合并
    if (constraints != null) {
      finalConstraints = finalConstraints.enforce(constraints!);
    }
  }

  // 使用智能的 LayoutBuilder,带递归检测
  return Builder(
    builder: (context) {
      // 更可靠的递归检测 - 检查调用栈深度
      int layoutBuilderDepth = 0;
      context.visitAncestorElements((element) {
        if (element.widget is LayoutBuilder) {
          layoutBuilderDepth++;
          if (layoutBuilderDepth > 2) { // 允许最多2层嵌套
            return false;
          }
        }
        return true;
      });

      if (layoutBuilderDepth > 2) {
        // 如果已经在 LayoutBuilder 中,使用简化的约束处理
        BoxConstraints? safeConstraints = finalConstraints;

        if (safeConstraints != null) {
          double minWidth = safeConstraints.minWidth;
          double maxWidth = safeConstraints.maxWidth;
          double minHeight = safeConstraints.minHeight;
          double maxHeight = safeConstraints.maxHeight;

          // 处理负数约束
          if (minWidth < 0) minWidth = 0;
          if (minHeight < 0) minHeight = 0;

          // 处理极大值约束
          if (maxWidth > 10000) maxWidth = 10000;
          if (maxHeight > 10000) maxHeight = 10000;

          // 确保 min <= max
          if (minWidth > maxWidth && maxWidth != double.infinity) {
            minWidth = maxWidth;
          }
          if (minHeight > maxHeight && maxHeight != double.infinity) {
            minHeight = maxHeight;
          }

          safeConstraints = BoxConstraints(
            minWidth: minWidth,
            maxWidth: maxWidth,
            minHeight: minHeight,
            maxHeight: maxHeight,
          );
        }

        return Container(
          width: width ?? parsedStyles['width'],
          height: height ?? parsedStyles['height'],
          padding: padding ?? parsedStyles['padding'],
          margin: margin ?? parsedStyles['margin'],
          alignment: alignment ?? parsedStyles['alignment'],
          clipBehavior: clipBehavior,
          constraints: safeConstraints,
          transform: transform,
          transformAlignment: transformAlignment,
          foregroundDecoration: foregroundDecoration,
          decoration: finalDecoration,
          child: containerChild,
        );
      } else {
        // 使用 LayoutBuilder 来安全处理约束
        return LayoutBuilder(
          builder: (context, parentConstraints) {
            // 确保约束在合理范围内
            BoxConstraints safeConstraints = finalConstraints ?? const BoxConstraints();

            // 严格验证约束值,防止无效约束
            double minWidth = safeConstraints.minWidth;
            double maxWidth = safeConstraints.maxWidth;
            double minHeight = safeConstraints.minHeight;
            double maxHeight = safeConstraints.maxHeight;

            // 处理负数约束
            if (minWidth < 0) minWidth = 0;
            if (minHeight < 0) minHeight = 0;

            // 处理无限约束
            if (maxWidth == double.infinity && parentConstraints.maxWidth.isFinite) {
              maxWidth = parentConstraints.maxWidth;
            }
            if (maxHeight == double.infinity && parentConstraints.maxHeight.isFinite) {
              maxHeight = parentConstraints.maxHeight;
            }

            // 处理极大值约束(防止内存溢出)
            if (maxWidth > 10000) maxWidth = 10000;
            if (maxHeight > 10000) maxHeight = 10000;

            // 确保 min <= max
            if (minWidth > maxWidth && maxWidth != double.infinity) {
              minWidth = maxWidth;
            }
            if (minHeight > maxHeight && maxHeight != double.infinity) {
              minHeight = maxHeight;
            }

            // 创建安全的约束
            safeConstraints = BoxConstraints(
              minWidth: minWidth,
              maxWidth: maxWidth,
              minHeight: minHeight,
              maxHeight: maxHeight,
            );

            // 获取安全的宽高值
            double? safeWidth = width ?? parsedStyles['width'];
            double? safeHeight = height ?? parsedStyles['height'];

            // 验证宽高值
            if (safeWidth != null) {
              if (safeWidth < 0) safeWidth = 0;
              if (safeWidth > 10000) safeWidth = 10000;
            }
            if (safeHeight != null) {
              if (safeHeight < 0) safeHeight = 0;
              if (safeHeight > 10000) safeHeight = 10000;
            }

            return Container(
              width: safeWidth,
              height: safeHeight,
              padding: padding ?? parsedStyles['padding'],
              margin: margin ?? parsedStyles['margin'],
              alignment: alignment ?? parsedStyles['alignment'],
              clipBehavior: clipBehavior,
              constraints: safeConstraints,
              transform: transform,
              transformAlignment: transformAlignment,
              foregroundDecoration: foregroundDecoration,
              decoration: finalDecoration,
              child: containerChild,
            );
          },
        );
      }
    },
  );
}