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) {
  String text = this.text ?? "";
  TextStyle textStyle = this.textStyle ??
      TextStyle(
        color: textColor.enable(enable),
        fontSize: textSize,
      );
  Widget child = Text(
    text,
    style: textStyle,
    textAlign: textAlign,
    overflow: overflow,
    maxLines: maxLines,
  );

  // 解决Android与iOS平台下字体对齐问题
  // https://docs.google.com/spreadsheets/d/1864hy_1r4vxf1I864gIlj8m3tgZeijG-BwhghSm5OJQ/edit?gid=0#gid=0
  bool shrinkWrap = this.shrinkWrap;
  Size size = renderSize(
    text,
    textStyle: textStyle,
    textScaler: MediaQuery.of(context).textScaler,
  );
  if (Platform.isAndroid) {
    Offset? offset;
    double textHeight = size.height;
    // 如果是英文或数字,增加高度(由于计算出来的高度比实际高度小)
    if (text.isEnglishOrDigits) {
      textHeight += 4;
    }
    TextScaler textScaler = MediaQuery.of(context).textScaler;
    String scaleText = textScaler.toString();
    double scaleFactor = scaleText.getScaleFactor;
    if (scaleFactor == 0) scaleFactor = 1;
    EdgeInsetsGeometry padding = scaledPadding(context);
    // 如果文本高度大于按钮设定的高度,向上偏移文本
    if (height != null && textHeight > height!) {
      shrinkWrap = true;
      offset = Offset(0, (height! - textHeight) * 0.8);
    } else if (height != null && textHeight > height! - padding.vertical) {
      shrinkWrap = true;
      if (!text.isChinese) {
        offset = Offset(0, (0.4 * log(pow(scaleFactor, 2) * 12)));
        if (text.isContainChinese) {
          offset = Offset(0, (0.1 * log(pow(scaleFactor, 2) * 10)));
        }
      } else {
        offset = Offset(0, -(0.1 * log(pow(scaleFactor, 2) * 10)));
      }
    } else if (text.isContainChinese) {
      // 如果是中文,向下偏移文本(随着字体大小的增加,增速变快)
      offset = Offset(0, -(0.1 + 0.2 * pow(scaleFactor, 5)));
      if (text.isChinese) {
        offset = offset.translate(0, -(0.7 * log(pow(scaleFactor, 2) * 4)));
      }
    } else if (text.isEnglishOrDigits) {
      // 如果是中文,向上偏移文本(随着字体大小的增加,增速变慢)
      offset = Offset(0, (0.1 * log(pow(scaleFactor, 2) * 12)));
    }
    if (offset != null) {
      child = Transform.translate(offset: offset, child: child);
    }
  } else if (Platform.isIOS) {
    if (height != null && size.height > height!) {
      double percent = 0.1;
      if (text.isEnglishOrDigits) percent = 0.3;
      child = Transform.translate(
        offset: Offset(0, (height! - size.height) * percent),
        child: SizedBox(
          width: size.width + 6,
          child: OverflowBox(
            maxHeight: size.height,
            child: child,
          ),
        ),
      );
    }
  }
  if (this.child != null) {
    child = this.child!;
  }

  // 设置图标及位置
  if (icon != null) {
    if (iconPosition == IconPosition.left) {
      child = Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          icon!,
          SizedBox(width: gap),
          Flexible(child: child, fit: flexFit),
        ],
      );
    } else if (iconPosition == IconPosition.right) {
      child = Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Flexible(child: child, fit: flexFit),
          SizedBox(width: gap),
          icon!
        ],
      );
    } else if (iconPosition == IconPosition.top) {
      child = Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          icon!,
          SizedBox(height: gap),
          Flexible(child: child, fit: flexFit),
        ],
      );
    } else if (iconPosition == IconPosition.bottom) {
      child = Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Flexible(child: child, fit: flexFit),
          SizedBox(height: gap),
          icon!
        ],
      );
    }
  }

  Size? minimumSize = this.minimumSize;
  EdgeInsetsGeometry? padding = this.padding;
  MaterialTapTargetSize? tapTargetSize = this.tapTargetSize;
  if (shrinkWrap) {
    if (padding == null) padding = EdgeInsets.zero;
    minimumSize = Size.zero;
    tapTargetSize = MaterialTapTargetSize.shrinkWrap;
  }
  child = TextButton(
    onPressed: () {
      if (enable) onPressed.call();
    },
    onLongPress: () {
      if (enable) onLongPress?.call();
    },
    onHover: onHover,
    onFocusChange: onFocusChange,
    clipBehavior: clipBehavior,
    focusNode: focusNode,
    autofocus: autofocus,
    statesController: statesController,
    isSemanticButton: isSemanticButton,
    style: style ??
        ButtonStyle(
          backgroundColor: resolve<Color?>(backgroundColor?.enable(enable)),
          foregroundColor: resolve<Color?>(foregroundColor?.enable(enable)),
          overlayColor: resolve<Color?>(overlayColor),
          shadowColor: resolve<Color?>(shadowColor),
          surfaceTintColor: resolve<Color?>(surfaceTintColor),
          shape: resolve<OutlinedBorder?>(shape ??
              RoundedRectangleBorder(
                borderRadius:
                    borderRadius ?? BorderRadius.all(Radius.circular(radius)),
                side: borderSide ??
                    BorderSide(
                      color: borderColor ?? const Color(0xFF000000),
                      width: borderWidth ?? 1,
                      style: borderStyle ?? BorderStyle.solid,
                    ),
              )),
          side: resolve<BorderSide?>(borderSide ??
              BorderSide(
                color: borderColor ?? const Color(0xFF000000),
                width: borderWidth ?? 1,
                style: borderStyle ?? BorderStyle.solid,
              )),
          padding: resolve<EdgeInsetsGeometry?>(padding),
          elevation: resolve<double?>(elevation),
          iconColor: resolve<Color?>(iconColor?.enable(enable)),
          iconSize: resolve<double?>(iconSize),
          alignment: alignment,
          minimumSize: resolve<Size?>(minimumSize),
          tapTargetSize: tapTargetSize,
        ),
    child: child,
  );
  if (width != null || height != null) {
    child = SizedBox(
      width: width,
      height: height,
      child: child,
    );
  }
  return child;
}