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) {
  if (text.isEmpty || animations.isEmpty) return const SizedBox.shrink();

  // Use LayoutBuilder to get the available width, then measure all
  // character bounding boxes with TextPainter before laying them out.
  return LayoutBuilder(
    builder: (context, constraints) {
      final maxWidth = constraints.hasBoundedWidth
          ? constraints.maxWidth
          : double.infinity;

      // Measure the full text once to get each character's position.
      final tp = TextPainter(
        text: TextSpan(text: text, style: textStyle),
        textDirection: textDirection,
        textAlign: textAlign,
        strutStyle: strutStyle,
        textHeightBehavior: textHeightBehavior,
        textWidthBasis: textWidthBasis,
      )..layout(maxWidth: maxWidth);

      // Extract per-character bounding rectangles from the laid-out text.
      final charRects = <int, Rect>{};
      for (int i = 0; i < text.length; i++) {
        final boxes = tp.getBoxesForSelection(
          TextSelection(baseOffset: i, extentOffset: i + 1),
        );
        if (boxes.isNotEmpty) {
          charRects[i] = boxes.first.toRect();
        }
      }

      return SizedBox(
        width: tp.width,
        height: tp.height,
        child: Stack(
          clipBehavior: Clip.none,
          children: List.generate(text.length, (i) {
            final char = animations[i].character ?? text[i];
            // Whitespace characters are skipped — they contribute no visual.
            if (char == ' ' || char == '\n' || char == '\t') {
              return const SizedBox.shrink();
            }

            final anim = animations[i];
            final rect = charRects[i];
            if (rect == null) return const SizedBox.shrink();

            // Base character widget with optional color override.
            Widget charWidget = Text(
              char,
              style: textStyle.copyWith(
                color: anim.color ?? textStyle.color,
              ),
              textDirection: textDirection,
            );

            // Wrap in background color container if set.
            if (anim.backgroundColor != null) {
              charWidget = Container(
                color: anim.backgroundColor,
                child: charWidget,
              );
            }

            // Transform pipeline: blur → opacity → translate → scale → rotate → clip.
            // Each stage wraps the previous so the operations compose correctly.
            Widget clipContent = charWidget;

            // 1. Gaussian blur (applied first so blur doesn't affect other transforms).
            if (anim.blurSigma > 0) {
              clipContent = ImageFiltered(
                imageFilter: ui.ImageFilter.blur(
                  sigmaX: anim.blurSigma,
                  sigmaY: anim.blurSigma,
                ),
                child: clipContent,
              );
            }

            // 2. Opacity fade.
            if (anim.opacity < 1.0) {
              clipContent = Opacity(
                opacity: anim.opacity,
                child: clipContent,
              );
            }

            // 3. Translation (offset position).
            if (anim.translation != Offset.zero) {
              clipContent = Transform.translate(
                offset: anim.translation,
                child: clipContent,
              );
            }

            // 4. Scale (uniform × per-axis).
            final sx = anim.scale * anim.scaleX;
            final sy = anim.scale * anim.scaleY;
            if (sx != 1.0 || sy != 1.0) {
              clipContent = Transform(
                alignment: Alignment.topLeft,
                transform: Matrix4.diagonal3Values(sx, sy, 1),
                child: clipContent,
              );
            }

            // 5. 2D rotation (Z-axis).
            if (anim.rotation != 0.0) {
              clipContent = Transform.rotate(
                angle: anim.rotation,
                child: clipContent,
              );
            }

            // 6. 3D rotation around X axis (setEntry prevents z-fighting).
            if (anim.rotationX != 0.0) {
              clipContent = Transform(
                alignment: Alignment.topLeft,
                transform: Matrix4.identity()
                  ..setEntry(3, 2, 0.001)
                  ..rotateX(anim.rotationX),
                child: clipContent,
              );
            }

            // 7. 3D rotation around Y axis.
            if (anim.rotationY != 0.0) {
              clipContent = Transform(
                alignment: Alignment.topLeft,
                transform: Matrix4.identity()
                  ..setEntry(3, 2, 0.001)
                  ..rotateY(anim.rotationY),
                child: clipContent,
              );
            }

            // 8. Clip reveal (horizontal wipe from left).
            if (anim.clipProgress < 1.0) {
              clipContent = ClipRect(
                child: Align(
                  alignment: Alignment.centerLeft,
                  widthFactor: anim.clipProgress.clamp(0.0, 1.0),
                  child: clipContent,
                ),
              );
            }

            return Positioned(
              left: rect.left,
              top: rect.top,
              child: Stack(
                clipBehavior: Clip.none,
                children: [
                  clipContent,
                  if (anim.underlineProgress > 0)
                    Positioned(
                      left: 0,
                      top: rect.height,
                      child: Container(
                        width: rect.width *
                            anim.underlineProgress.clamp(0.0, 1.0),
                        height: 2,
                        color: anim.color ?? textStyle.color,
                      ),
                    ),
                ],
              ),
            );
          }),
        ),
      );
    },
  );
}