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) {
  // Calculate expansion rectangle based on thickness
  final rect = RelativeRect.lerp(
    RelativeRect.fill,
    RelativeRect.fromLTRB(
      -expansion,
      -expansion,
      -expansion,
      -expansion,
    ),
    thickness,
  );

  // 1. Background Indicator (Resting state)
  // Fade out as the drag spring thickness increases toward 0.15.
  final backgroundOpacity = (1.0 - (thickness / 0.15)).clamp(0.0, 1.0);
  final backgroundIndicator = IgnorePointer(
    child: Opacity(
      opacity: backgroundOpacity,
      child: DecoratedBox(
        decoration: BoxDecoration(
          color: indicatorColor,
          borderRadius: BorderRadius.circular(borderRadius),
        ),
        child: const SizedBox.expand(),
      ),
    ),
  );

  // 2. Glass Indicator (Active/Dragging state)
  // We fade the glass in/out by setting `visibility` on the settings rather
  // than wrapping the widget in `Opacity`.
  final fade = thickness.clamp(0.0, 1.0);
  final base = glassSettings ?? _baseGlassSettings;
  final effectiveSettings = base.copyWith(visibility: fade);

  final shape = useSuperellipse
      ? LiquidRoundedSuperellipse(borderRadius: borderRadius * 2)
      : LiquidRoundedRectangle(borderRadius: borderRadius);

  final glassWidget = GlassEffect(
    shape: shape,
    settings: effectiveSettings,
    quality: quality,
    interactionIntensity: thickness,
    backgroundKey: backgroundKey,
    clipExpansion: _jellyClipExpansion,
    child: const GlassGlow(
      glowColor: Colors
          .transparent, // caused grey rectangle flicker if clicking multiple times
      child: SizedBox.expand(),
    ),
  );

  final bool isMinimal = quality == GlassQuality.minimal;
  // Mount early (0.01) so geometry is built before the indicator is visible,
  // preventing a 1-frame flicker at the edges on fast drags.
  final interactiveIndicator = thickness > 0.01
      ? (isMinimal ? glassWidget : RepaintBoundary(child: glassWidget))
      : const SizedBox.expand();

  // Unified indicator child
  final indicatorChild = Stack(
    children: [
      if (paintBackground && backgroundOpacity > 0) backgroundIndicator,
      if (paintGlass && fade > 0.05) interactiveIndicator,
    ],
  );

  final indicatorBody = Stack(
    clipBehavior: Clip.none,
    children: [
      Positioned.fromRelativeRect(
        rect: rect!,
        child: RepaintBoundary(
          child: Transform(
            alignment: Alignment.center,
            transform: DraggableIndicatorPhysics.buildJellyTransform(
              velocity: Offset(velocity, 0),
              maxDistortion: 0.8,
              velocityScale: 10,
            ),
            child: indicatorChild,
          ),
        ),
      ),
    ],
  );

  Widget positioning;
  if (exactWidth != null && exactOffset != null) {
    // Exact pixel positioning for scrollable mode with variable-width tabs
    positioning = Positioned(
      left: exactOffset,
      top: 0,
      bottom: 0,
      width: exactWidth,
      child: indicatorBody,
    );
  } else {
    // Fractional positioning for fixed-width tabs
    positioning = Positioned.fill(
      child: FractionallySizedBox(
        widthFactor: 1 / itemCount,
        alignment: alignment,
        child: indicatorBody,
      ),
    );
  }

  return Positioned.fill(
    child: Padding(
      padding: padding,
      child: Stack(
        clipBehavior: Clip.none,
        children: [positioning],
      ),
    ),
  );
}