show static method

Future<CoachResult> show(
  1. BuildContext context, {
  2. required List<CoachStep> steps,
  3. CoachLabels labels = const CoachLabels(),
  4. CoachStyle style = const CoachStyle(),
  5. ValueChanged<int>? onStep,
})

Shows steps one after another and completes when the tour closes.

Steps whose widget is not laid out are left out; when none is, CoachResult.completed is returned without showing anything. onStep receives the index (in steps) of each step as it appears.

Implementation

static Future<CoachResult> show(
  BuildContext context, {
  required List<CoachStep> steps,
  CoachLabels labels = const CoachLabels(),
  CoachStyle style = const CoachStyle(),
  ValueChanged<int>? onStep,
}) async {
  // Indices (into [steps]) of the steps whose widget is on screen right now.
  final shown = <int>[
    for (var i = 0; i < steps.length; i++)
      if (_boxOf(steps[i]) != null) i,
  ];
  // Nothing to point at: finish at once instead of showing an empty overlay.
  if (shown.isEmpty) return CoachResult.completed;
  // The route only knows the visible steps, so the callback maps its index
  // back to the caller's original index.
  final route = _CoachRoute(
    steps: [for (final i in shown) steps[i]],
    onStep: onStep == null ? null : (i) => onStep(shown[i]),
    labels: labels,
    style: style,
    reduceMotion: MediaQuery.maybeOf(context)?.disableAnimations ?? false,
  );
  // Completes however the tour ends, even if the navigator itself goes away.
  unawaited(Navigator.of(context).push<CoachResult>(route).then(route.finish));
  return route.result.future;
}