build method

  1. @override
dynamic build(
  1. dynamic context
)

Implementation

@override
Widget build(BuildContext context) {
  TextStyle style = (this.style ?? context.typography.b1).copyWith(
    color: color,
    fontWeight: fontWeight,
  );

  return LayoutBuilder(
    builder: (context, constraints) {
      final maxWidth = constraints.maxWidth;
      final maxHeight = constraints.maxHeight;
      double fontSize = max(style.fontSize ?? 0, minFontSize);

      if (fitHeight != null) {
        if (constraints.maxHeight == double.infinity) {
          throw Exception("Cant use fitHeight with infinite height");
        }
        final (size, lines) = findStyleForFitHeight(
          maxWidth: maxWidth,
          maxHeight: maxHeight,
          text: text,
          style: style,
        );
        style = style.copyWith(fontSize: size);
        return Text(
          text,
          style: style,
          maxLines: lines,
          overflow: overflow,
          textAlign: textAlign,
          textDirection: textDirection,
        );
      }

      style = style.copyWith(fontSize: fontSize);

      final textPainter = TextPainter(
        text: TextSpan(
          text: text,
          style: style,
        ),
        textDirection: textDirection ?? TextDirection.ltr,
        maxLines: maxLines,
      )..layout(maxWidth: maxWidth);

      var lines = textPainter.computeLineMetrics();
      var totalHeight = lines.fold(0.0, (prev, line) => prev + line.height);

      while ((totalHeight > maxHeight || textPainter.didExceedMaxLines) &&
          fontSize > minFontSize) {
        fontSize = decreaseFontSize(fontSize);

        style = style.copyWith(fontSize: fontSize);
        textPainter.text = TextSpan(
          text: text,
          style: style,
        );
        textPainter.layout(maxWidth: maxWidth);
        lines = textPainter.computeLineMetrics();
        totalHeight = lines.fold(0.0, (prev, line) => prev + line.height);
      }

      return Text(
        text,
        style: style,
        maxLines: maxLines,
        overflow: overflow,
        textAlign: textAlign,
        textDirection: textDirection,
      );
    },
  );
}