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) {
  loadingSize ??= 20.bw;
  loadingMarginRight ??= 8.bw;

  BetterThemeExtension themeExtension = BetterUtil.getThemeExtension(
    context,
  )!;
  BetterButtonTheme buttonTheme = themeExtension.buttonTheme;

  Color? defaultColor = buttonTheme.defaultBackgroundColor;
  Color? finalBackgroundColor = backgroundColor ?? defaultColor;
  Color finalBorderColor = borderColor ?? buttonTheme.borderColor;
  Color? finalTextColor = buttonTheme.defaultTextColor;
  BorderRadiusGeometry finalBorderRadius =
      borderRadius ??
      buttonTheme.borderRadius ??
      BorderRadius.all(Radius.circular(6.bw));

  Color finalLoadingColor = buttonTheme.loadingColor;

  TextStyle? finalTextStyle = textStyle ?? TextStyle();

  double finalBorderWidth = borderWidth ?? 1;
  if (borderWidth == null && BetterUtil.shouldUseHairlineBorder(context)) {
    finalBorderWidth = 1.bw;
  }

  // 获取主题色
  Color primaryColor = themeExtension.primaryColor;
  Color successColor = themeExtension.successColor;
  Color warningColor = themeExtension.warningColor;
  Color dangerColor = themeExtension.dangerColor;
  Color infoColor = themeExtension.infoColor;

  // 获取按钮类型
  if (backgroundColor == null) {
    if (plain == null || plain == false) {
      if (type == BetterButtonType.defaultType) {
        finalBackgroundColor = defaultColor;
      } else if (type == BetterButtonType.primary) {
        finalBackgroundColor = primaryColor;
      } else if (type == BetterButtonType.success) {
        finalBackgroundColor = successColor;
      } else if (type == BetterButtonType.warning) {
        finalBackgroundColor = warningColor;
      } else if (type == BetterButtonType.danger) {
        finalBackgroundColor = dangerColor;
      } else if (type == BetterButtonType.info) {
        finalBackgroundColor = infoColor;
      }
    }
  }

  // 获取边框颜色
  if (borderColor == null) {
    if (plain != null && plain == true) {
      if (type == BetterButtonType.primary) {
        finalBorderColor = primaryColor;
      } else if (type == BetterButtonType.success) {
        finalBorderColor = successColor;
      } else if (type == BetterButtonType.warning) {
        finalBorderColor = warningColor;
      } else if (type == BetterButtonType.danger) {
        finalBorderColor = dangerColor;
      } else if (type == BetterButtonType.info) {
        finalBorderColor = infoColor;
      }
    }
    if (plain == null || plain == false) {
      if (type == BetterButtonType.defaultType) {
        finalBorderColor = finalBorderColor;
      } else {
        finalBorderColor = Colors.transparent;
      }
    }
  }

  if (plain != null && plain == true) {
    if (type == BetterButtonType.primary) {
      finalTextColor = primaryColor;
      finalLoadingColor = primaryColor;
    } else if (type == BetterButtonType.success) {
      finalTextColor = successColor;
      finalLoadingColor = successColor;
    } else if (type == BetterButtonType.warning) {
      finalTextColor = warningColor;
      finalLoadingColor = warningColor;
    } else if (type == BetterButtonType.danger) {
      finalTextColor = dangerColor;
      finalLoadingColor = dangerColor;
    } else if (type == BetterButtonType.info) {
      finalTextColor = infoColor;
      finalLoadingColor = infoColor;
    }
  } else {
    if (type == BetterButtonType.primary) {
      finalTextColor = buttonTheme.primaryTextColor;
      finalLoadingColor = buttonTheme.primaryTextColor;
    } else if (type == BetterButtonType.success) {
      finalTextColor = buttonTheme.successTextColor;
      finalLoadingColor = Colors.white;
    } else if (type == BetterButtonType.warning) {
      finalTextColor = buttonTheme.warningTextColor;
      finalLoadingColor = Colors.white;
    } else if (type == BetterButtonType.danger) {
      finalTextColor = buttonTheme.dangerTextColor;
      finalLoadingColor = Colors.white;
    } else if (type == BetterButtonType.info) {
      finalTextColor = buttonTheme.infoTextColor;
      finalLoadingColor = Colors.white;
    }
  }

  if (loadingColor != null) {
    finalLoadingColor = loadingColor!;
  }

  // 获取按钮内容
  final children = <Widget>[];
  if (loading) {
    if (loadingWidget != null) {
      children.add(loadingWidget!);
    } else {
      if (loadingType == BetterButtonLoadingType.circular) {
        children.add(
          SizedBox(
            width: loadingSize,
            height: loadingSize,
            child: CircularProgressIndicator(
              strokeWidth:
                  loadingStrokeWidth ?? buttonTheme.loadingStrokeWidth ?? 1,
              valueColor: AlwaysStoppedAnimation(finalLoadingColor),
            ),
          ),
        );
      }
      if (loadingType == BetterButtonLoadingType.spinner) {
        children.add(Spinner(size: loadingSize, color: finalLoadingColor));
      }
      // 获取loading间距
      if ((loadingMarginRight != null && loadingMarginRight! > 0) &&
          ((loading == false || hideContentWhenLoading == false) ||
              (loading == true && loadingText != null))) {
        children.add(SizedBox(width: loadingMarginRight));
      }
    }
  }
  //获取前缀
  if (prefix != null &&
      (loading == false || hideContentWhenLoading == false)) {
    children.add(prefix!);
  }

  //获取文本样式
  if (finalTextStyle.color == null) {
    finalTextStyle = finalTextStyle.copyWith(color: finalTextColor);
  }
  if (finalTextStyle.fontSize == null) {
    finalTextStyle = finalTextStyle.copyWith(fontSize: buttonTheme.fontSize);
  }

  //获取文本
  if (text != null &&
      child == null &&
      (loading == false ||
          (loading == true && hideContentWhenLoading == false))) {
    children.add(Text(text!, style: finalTextStyle));
  }
  //获取自定义内容
  if (child != null) {
    children.add(child!);
  }

  //获取loading文本
  if (loading == true && loadingText != null) {
    children.add(
      Text(
        loadingText!,
        style: finalTextStyle,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }

  //获取后缀
  if (suffix != null &&
      (loading == false || hideContentWhenLoading == false)) {
    children.add(suffix!);
  }

  return ElevatedButton(
    style: ButtonStyle(
      padding: WidgetStateProperty.all(EdgeInsets.zero),
      minimumSize: WidgetStateProperty.all(Size.zero),
      tapTargetSize: MaterialTapTargetSize.shrinkWrap,
      backgroundColor: WidgetStateProperty.all(Colors.transparent),
      shadowColor: WidgetStateProperty.all(Colors.transparent),
      overlayColor: WidgetStateProperty.all(
        loading == true
            ? Colors.transparent
            : overlayColor ?? buttonTheme.overlayColor,
      ),
      splashFactory:
          disabled == true || disableSplash == true || loading == true
          ? NoSplash.splashFactory
          : InkSparkle.splashFactory,
      shape: WidgetStateProperty.all(
        RoundedRectangleBorder(borderRadius: finalBorderRadius),
      ),
      side: isShowBorder == true
          ? WidgetStateProperty.all(
              BorderSide(color: finalBorderColor, width: finalBorderWidth),
            )
          : WidgetStateProperty.all(BorderSide.none),
    ),
    onPressed: disabled == true ? null : onClick ?? () {},
    child: Ink(
      width: width,
      height: height,
      padding: padding ?? buttonTheme.padding,
      decoration: BoxDecoration(
        color: disabled == true
            ? finalBackgroundColor.withAlpha(128)
            : finalBackgroundColor,
        gradient: gradient,
        borderRadius: finalBorderRadius,
      ),
      child: Wrap(runAlignment: WrapAlignment.center, children: children),
    ),
  );
}