build method
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:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
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) {
final theme = Theme.of(context);
final cooling = state == SafaehSheetActionState.cooldown;
final working = state == SafaehSheetActionState.working;
final failed = state == SafaehSheetActionState.failed;
final accent = color ?? theme.colorScheme.primary;
final foreground = foregroundColor ?? theme.colorScheme.onPrimary;
final active = cooling || working;
final reducedMotion = MediaQuery.disableAnimationsOf(context);
final warning =
warningColor ??
(theme.brightness == Brightness.dark
? Colors.amber.shade300
: Colors.amber.shade700);
final tint = accent;
return TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: failed ? 1 : 0),
duration: reducedMotion
? Duration.zero
: const Duration(milliseconds: 1100),
builder: (context, value, child) {
final glow = failed && !reducedMotion ? math.sin(value * math.pi) : 0.0;
return DecoratedBox(
// Keep this identity stable so error/recovery transitions animate
// the existing button instead of mounting a fresh subtree.
key: glowKey,
decoration: ShapeDecoration(
shape: shape,
shadows: [
if (glow > 0)
BoxShadow(
color: warning.withValues(alpha: 0.36 * glow),
blurRadius: 12 + 12 * glow,
spreadRadius: 2 * glow,
),
],
),
child: child,
);
},
child: AnimatedBuilder(
animation: activity,
builder: (context, child) => Stack(
children: [
if (active)
Positioned.fill(
child: IgnorePointer(
child: ClipPath(
clipper: ShapeBorderClipper(shape: shape),
child: ColoredBox(
color: theme.colorScheme.surfaceContainerHighest,
child: CustomPaint(
key: activityKey,
painter: _ActivityPainter(
progress: reducedMotion
? (working
? 0.55
: cooldownProgress.clamp(0.0, 1.0))
: activity.value,
working: working,
rtl: Directionality.of(context) == TextDirection.rtl,
color: tint,
),
),
),
),
),
),
child!,
for (final slot in [(true, leading), (false, trailing)])
PositionedDirectional(
start: slot.$1 ? 8 : null,
end: slot.$1 ? null : 8,
top: 0,
bottom: 0,
width: 48,
child: IgnorePointer(
ignoring: slot.$2 == null || active,
child: ExcludeFocus(
excluding: slot.$2 == null || active,
child: ExcludeSemantics(
excluding: slot.$2 == null || active,
child: AnimatedOpacity(
opacity: slot.$2 == null ? 0 : 1,
duration: reducedMotion
? Duration.zero
: const Duration(milliseconds: 140),
child: Center(child: slot.$2),
),
),
),
),
),
],
),
child: SizedBox(
width: double.infinity,
child: FilledButton(
key: buttonKey,
style: FilledButton.styleFrom(
backgroundColor: failed ? Colors.amber.shade200 : accent,
foregroundColor: failed ? Colors.black87 : foreground,
disabledBackgroundColor: Colors.transparent,
disabledForegroundColor: theme.colorScheme.onSurface,
minimumSize: const Size(0, 48),
shape: shape,
side: BorderSide(
width: borderWidth,
color: failed
? warning
: active
? tint.withValues(alpha: 0.75)
: Colors.transparent,
),
animationDuration: reducedMotion
? Duration.zero
: const Duration(milliseconds: 360),
),
onPressed: active ? null : onPressed,
child: Semantics(
liveRegion: true,
excludeSemantics: cooling,
label: announcement,
child: Row(
// Reserve the error icon slot so labels stay centered in every state.
children: [
SizedBox(
width:
reserveActionSpace ||
leading != null ||
trailing != null
? 48
: 8,
),
Expanded(
child: LayoutBuilder(
builder: (context, constraints) {
// Measure all labels once per layout, including localized
// large text, so changing state cannot resize the button.
var height = 0.0;
for (final candidate in {...stateLabels, label}) {
final painter = TextPainter(
text: TextSpan(
text: candidate,
style: DefaultTextStyle.of(context).style,
),
textDirection: Directionality.of(context),
textScaler: MediaQuery.textScalerOf(context),
)..layout(maxWidth: constraints.maxWidth);
height = math.max(height, painter.height);
painter.dispose();
}
return SizedBox(
height: height,
child: AnimatedSwitcher(
duration: reducedMotion
? Duration.zero
: const Duration(milliseconds: 360),
switchInCurve: const Interval(
0.35,
1,
curve: Curves.easeOutCubic,
),
switchOutCurve: const Interval(
0.6,
1,
curve: Curves.easeOut,
),
transitionBuilder: (child, animation) =>
FadeTransition(
opacity: animation,
child: SlideTransition(
position: Tween(
begin: const Offset(0, 0.12),
end: Offset.zero,
).animate(animation),
child: child,
),
),
child: Text(
label,
key: ValueKey(label),
textAlign: TextAlign.center,
),
),
);
},
),
),
SizedBox(
width:
reserveActionSpace ||
leading != null ||
trailing != null
? 48
: 8,
),
],
),
),
),
),
),
);
}