showFluent2Toast function

Fluent2Raised showFluent2Toast(
  1. BuildContext context,
  2. String message, {
  3. String? title,
  4. Fluent2ToastIntent tone = Fluent2ToastIntent.success,
  5. String? actionLabel,
  6. VoidCallback? onAction,
  7. Duration? duration,
})

Raises a short notice over the page, Fluent 2's Toast. Under a Fluent2Toaster it queues there, after any already showing; with no toaster it stands alone: a new one replaces the one still up, gutter to gutter 16 over the tab bar on a phone, 292 wide at the window's bottom end on the desktop. It stays for duration (three seconds, six with its one inline action) and a tap on the action takes it down.

showFluent2Toast(context, 'File moved to Archive', actionLabel: 'Undo', onAction: undo)

Implementation

Fluent2Raised showFluent2Toast(
  BuildContext context,
  String message, {
  String? title,
  Fluent2ToastIntent tone = Fluent2ToastIntent.success,
  String? actionLabel,
  VoidCallback? onAction,
  Duration? duration,
}) {
  final stays = duration ?? Duration(seconds: actionLabel == null ? 3 : 6);
  final toaster = DesignToaster.maybeOf(context);
  if (toaster != null) {
    return _Queued(
      toaster.show(
        DesignToast(
          message: message,
          title: title,
          actionLabel: actionLabel,
          onAction: onAction,
          duration: stays,
          data: tone,
        ),
      ),
    );
  }
  final moves = Fluent2Motion.on(context);
  final wide = Fluent2Layout.isWide(context);
  final overlay = Fluent2Host.navigatorOf(context).overlay!;
  final double bottom;
  if (wide) {
    bottom = Fluent2ChromeMetrics.toastGap;
  } else {
    final stands = math.max(
      ShellInsets.of(context),
      Fluent2BottomStand.reachOf(
        context,
        overlay.context.findRenderObject()! as RenderBox,
      ),
    );
    bottom = stands > 0
        ? stands + Fluent2ChromeMetrics.toastGap
        : MediaQuery.paddingOf(overlay.context).bottom +
              Fluent2ChromeMetrics.toastGap;
  }
  _up?.dismiss();
  final handle = _Overlaid();
  _up = handle;
  final timer = Timer(stays, handle.dismiss);
  final themes = InheritedTheme.capture(from: context, to: overlay.context);
  final card = Semantics(
    container: true,
    liveRegion: true,
    child: Fluent2Toast(
      message,
      title: title,
      tone: tone,
      actionLabel: actionLabel,
      onAction: actionLabel == null
          ? null
          : () {
              handle.dismiss();
              onAction?.call();
            },
    ),
  );
  final entry = OverlayEntry(
    // Torn down with its overlay (the app gone while it is up), it goes
    // too, so its time never runs on past the app.
    builder: (_) => _Standing(
      onGone: () {
        timer.cancel();
        scheduleMicrotask(handle.dismiss);
      },
      child: PositionedDirectional(
        bottom: bottom,
        start: wide ? null : Fluent2ChromeMetrics.gutter,
        end: wide ? Fluent2ChromeMetrics.toastGap : Fluent2ChromeMetrics.gutter,
        width: wide ? Fluent2ChromeMetrics.toastWidth : null,
        child: themes.wrap(
          IgnorePointer(
            ignoring: actionLabel == null,
            child: !moves
                ? card
                : TweenAnimationBuilder<double>(
                    tween: Tween(begin: 0, end: 1),
                    duration: Fluent2Motion.normal,
                    curve: Fluent2Motion.decelerateMid,
                    builder: (_, v, child) => Opacity(
                      opacity: v,
                      child: Transform.translate(
                        offset: Offset(0, (1 - v) * 16),
                        child: child,
                      ),
                    ),
                    child: card,
                  ),
          ),
        ),
      ),
    ),
  );
  handle.remove = () {
    timer.cancel();
    entry.remove();
    entry.dispose();
    if (identical(_up, handle)) _up = null;
  };
  overlay.insert(entry);
  return handle;
}