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).hintTheme;
final l = labels ?? theme.tooltipLabels;
final isLast = ctx.isLast;
// A single-step hint is informational, not a tour: no action row at all
// (tap-on-overlay/target and keyboard already dismiss it). No Done keeps
// a lone hint visually distinct from a tour step.
final isSingle = ctx.totalSteps <= 1;
final onSurface = theme.tooltipForeground;
final screenSize = MediaQuery.sizeOf(context);
final maxWidth = math.min(360.0, screenSize.width - 32).clamp(160.0, 360.0);
// Text-scale contract: at `textScaleFactor = 2.0` the tooltip still fits
// on screen — the height is capped and the content scrolls instead of
// overflowing (long text wraps within maxWidth and grows vertically until
// the cap). The cap also keeps the placement fallback corner on screen
// for degenerate content. The scroll machinery is not free (~18 KB of
// heap while mounted, measured against the engine baseline), so it is
// mounted only when the contract needs it: at scale 1.0 the content is
// width-capped (text wraps) and grows freely.
final textScale = MediaQuery.textScalerOf(context).scale(1.0);
final maxHeight = math.max(160.0, screenSize.height - 48);
final title = this.title ?? step.title;
final description = this.description ?? step.description;
// The buttons keep inheriting the ambient Material theme the way the
// old Material buttons did (their text style was `textTheme.labelLarge`
// — a product's fontFamily/letterSpacing/height must survive a theme
// swap; only size/weight/color are overridden here).
final buttonText = Theme.of(context).textTheme.labelLarge;
// Lightweight buttons — InkWell + Text instead of Material buttons. The
// tooltip's own Material is the ink ancestor (ripples paint on it); the
// accent button carries its own flat Material for the inverted pair.
// `Semantics(button: true)` keeps the button role + tap action for
// screen readers. Measured: three Material buttons cost ~45 KB more heap
// than this row while mounted (see benchmark/bench/
// memory_tooltip_decomp_test.dart); the mechanics and the look are the
// same — Skip at the far edge, small and quiet, Back/Next/Done grouped
// at the end (the primary action is always at the very corner).
Widget plainButton(String label, VoidCallback onPressed,
{bool compact = false}) =>
Semantics(
button: true,
child: InkWell(
onTap: onPressed,
borderRadius: BorderRadius.circular(20),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: compact ? 8 : 12,
vertical: compact ? 6 : 10,
),
child: Text(
label,
style: (buttonText ?? const TextStyle(fontSize: 14)).copyWith(
fontSize: compact ? 12 : 14,
fontWeight: FontWeight.w500,
color: onSurface,
),
),
),
),
);
final content = Padding(
padding: theme.tooltipPadding,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (title != null)
Text(
title,
style: theme.tooltipTitleStyle ??
TextStyle(
fontSize: 15,
fontWeight: FontWeight.w600,
color: onSurface,
),
),
if (description != null) ...[
if (title != null) const SizedBox(height: 4),
Text(
description,
style: theme.tooltipDescriptionStyle ??
TextStyle(fontSize: 13, color: onSurface),
),
],
// The button row. Skip is meaningless when the tour is about to
// end anyway ("Done" does the same) — shown on intermediate steps
// only, and only when the step opts in. This covers the last step
// of a multi-step tour. A single-step hint shows no row at all
// (see isSingle above). Hidden for informational slots
// (showActions: false).
if (showActions && !isSingle) ...[
const SizedBox(height: 12),
Row(
children: [
if (step.showSkip && !isLast)
plainButton(l.skip, ctx.actions.skip, compact: true),
const Spacer(),
if (ctx.stepIndex > 0) ...[
plainButton(l.back, ctx.actions.previous),
const SizedBox(width: 8),
],
// Inverted pair: the accent button contrasts with the
// tooltip background in any (light/dark) theme.
Semantics(
button: true,
child: Material(
color: onSurface,
borderRadius: BorderRadius.circular(20),
child: InkWell(
onTap: isLast ? ctx.actions.finish : ctx.actions.next,
borderRadius: BorderRadius.circular(20),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 10),
child: Text(
isLast ? l.done : l.next,
style: (buttonText ?? const TextStyle(fontSize: 14))
.copyWith(
fontSize: 14,
fontWeight: FontWeight.w500,
color: theme.tooltipBackground,
),
),
),
),
),
),
],
),
],
],
),
);
return Semantics(
container: true,
label: l.announce(
stepIndex: ctx.stepIndex,
totalSteps: ctx.totalSteps,
title: title ?? step.targetId,
),
child: Material(
color: theme.tooltipBackground,
borderRadius: theme.tooltipRadius,
elevation: 6,
child: textScale > 1.0
? ConstrainedBox(
constraints:
BoxConstraints(maxWidth: maxWidth, maxHeight: maxHeight),
child: SingleChildScrollView(child: content),
)
: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxWidth),
child: content,
),
),
);
}