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 config = VThemeConfig.of(context);
  final resolvedHeight = height ?? config.buttonHeight ?? 44;
  final resolvedFontSize = fontSize ?? config.buttonFontSize ?? 14;
  final resolvedPadding = padding ??
      EdgeInsets.symmetric(horizontal: config.buttonPaddingH ?? 16);

  final effectiveOnPressed = (disabled || loading) ? null : onPressed;
  final resolvedRadius = _resolvedRadius(context);
  final fgColor = _resolvedFgColor(context);

  // 边框:显式 border > hasBorder/plainType 自动边框
  BoxBorder? resolvedBorder = border;
  if (resolvedBorder == null &&
      (hasBorder || type == VButtonType.plainType)) {
    resolvedBorder = Border.all(color: _resolvedBorderColor(context), width: 1);
  }

  final bgColorResolved = _resolvedBgColor(context);

  // 子组件内容
  Widget content;
  if (child != null) {
    content = child!;
  } else {
    // 构造 icon widget(loading 优先)
    Widget? iconWidget;
    if (loading) {
      final loadingSize = resolvedFontSize + 2;
      final effectiveLoadingColor = loadingColor ?? fgColor;
      if (loadingType == VLoadingType.cupertino) {
        // iOS 菊花转风格,支持自定义颜色
        iconWidget = SizedBox(
          width: loadingSize,
          height: loadingSize,
          child: CupertinoActivityIndicator(
            radius: loadingSize / 2,
            color: effectiveLoadingColor,
          ),
        );
      } else {
        iconWidget = SizedBox(
          width: loadingSize,
          height: loadingSize,
          child: CircularProgressIndicator(
            strokeWidth: 2,
            valueColor: AlwaysStoppedAnimation<Color>(effectiveLoadingColor),
          ),
        );
      }
    } else if (icon != null) {
      iconWidget = IconTheme(
        data: IconThemeData(color: fgColor, size: resolvedFontSize + 2),
        child: icon!,
      );
    }

    final textWidget = Text(
      text,
      style: textStyle ??
          TextStyle(color: fgColor, fontSize: resolvedFontSize, height: 1),
    );

    if (iconWidget == null) {
      content = textWidget;
    } else {
      final isHorizontal = iconPosition == VIconPosition.left ||
          iconPosition == VIconPosition.right;
      final isIconFirst = iconPosition == VIconPosition.left ||
          iconPosition == VIconPosition.top;
      final spacer = isHorizontal
          ? const SizedBox(width: 4)
          : const SizedBox(height: 2);
      final children = isIconFirst
          ? <Widget>[iconWidget, spacer, textWidget]
          : <Widget>[textWidget, spacer, iconWidget];

      if (isHorizontal) {
        content = Row(
          mainAxisSize: block ? MainAxisSize.max : MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: children,
        );
      } else {
        content = Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: children,
        );
      }
    }
  }

  Widget btn = Container(
    margin: margin,
    width: block ? double.infinity : width,
    height: resolvedHeight,
    decoration: BoxDecoration(
      color: gradient != null ? null : bgColorResolved,
      gradient: gradient,
      borderRadius: resolvedRadius,
      border: resolvedBorder,
      image: imageBg != null
          ? DecorationImage(
              image: imageBg!.startsWith('http')
                  ? NetworkImage(imageBg!) as ImageProvider
                  : AssetImage(imageBg!),
              fit: BoxFit.cover,
            )
          : null,
    ),
    child: Material(
      color: Colors.transparent,
      child: InkWell(
        borderRadius: resolvedRadius,
        highlightColor: highlightColor ?? Colors.transparent,
        splashColor: splashColor ?? Colors.transparent,
        onTap: effectiveOnPressed,
        child: Padding(
          padding: resolvedPadding,
          child: Center(
            widthFactor: block ? null : 1.0,
            child: content,
          ),
        ),
      ),
    ),
  );

  // 禁用态视觉:传了 disabledBgColor 就用颜色表达,否则用 Opacity 兜底
  if (disabled && !_useDisabledBgColor) {
    btn = Opacity(opacity: 0.4, child: btn);
  }

  return btn;
}