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 isLast = ctx.isLast;
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.
final maxHeight = math.max(160.0, screenSize.height - 48);
final title = this.title ?? step.title;
final description = this.description ?? step.description;
return Semantics(
container: true,
label: 'Step ${ctx.stepIndex + 1} of ${ctx.totalSteps}: '
'${title ?? step.targetId}',
child: Material(
color: theme.tooltipBackground,
borderRadius: theme.tooltipRadius,
elevation: 6,
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxWidth, maxHeight: maxHeight),
child: SingleChildScrollView(
child: 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 sits at the far edge, small and
// quiet — a tap aimed at the primary action must not hit
// it. Back and Next/Done are grouped at the end (the
// primary action is always at the very corner). Hidden for
// informational slots (showActions: false).
if (showActions) ...[
const SizedBox(height: 12),
Row(
children: [
// 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 both a single-step tour/hint
// and the last step of a multi-step tour.
if (step.showSkip && !ctx.isLast)
TextButton(
onPressed: ctx.actions.skip,
style: TextButton.styleFrom(
foregroundColor: onSurface,
visualDensity: VisualDensity.compact,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
minimumSize: const Size(0, 30),
padding:
const EdgeInsets.symmetric(horizontal: 8),
textStyle: const TextStyle(fontSize: 12),
),
child: const Text('Skip'),
),
const Spacer(),
if (ctx.stepIndex > 0) ...[
TextButton(
onPressed: ctx.actions.previous,
style: TextButton.styleFrom(
foregroundColor: onSurface,
),
child: const Text('Back'),
),
const SizedBox(width: 8),
],
// Inverted pair: the accent button contrasts with the
// tooltip background in any (light/dark) theme.
FilledButton(
onPressed:
isLast ? ctx.actions.finish : ctx.actions.next,
style: FilledButton.styleFrom(
backgroundColor: onSurface,
foregroundColor: theme.tooltipBackground,
),
child: Text(isLast ? 'Done' : 'Next'),
),
],
),
],
],
),
),
),
),
),
);
}