showToastWidget function

ToastFuture showToastWidget(
  1. Widget widget, {
  2. BuildContext? context,
  3. BuildContextPredicate buildContextPredicate = _defaultContextPredicate,
  4. Duration? duration,
  5. ToastPosition? position,
  6. VoidCallback? onDismiss,
  7. bool? dismissOtherToast,
  8. TextDirection? textDirection,
  9. bool? handleTouch,
  10. OKToastAnimationBuilder? animationBuilder,
  11. Duration? animationDuration,
  12. Curve? animationCurve,
})

Show a widget and returns a ToastFuture.

Implementation

ToastFuture showToastWidget(
  Widget widget, {
  BuildContext? context,
  BuildContextPredicate buildContextPredicate = _defaultContextPredicate,
  Duration? duration,
  ToastPosition? position,
  VoidCallback? onDismiss,
  bool? dismissOtherToast,
  TextDirection? textDirection,
  bool? handleTouch,
  OKToastAnimationBuilder? animationBuilder,
  Duration? animationDuration,
  Curve? animationCurve,
}) {
  if (context == null) {
    _throwIfNoContext(_contextMap.values, 'showToastWidget');
  }
  context ??= buildContextPredicate(_contextMap.values);

  final ToastTheme theme = ToastTheme.of(context);
  position ??= theme.position;
  handleTouch ??= theme.handleTouch;
  animationBuilder ??= theme.animationBuilder;
  animationDuration ??= theme.animationDuration;
  animationCurve ??= theme.animationCurve;
  duration ??= theme.duration;

  final bool movingOnWindowChange = theme.movingOnWindowChange;

  final TextDirection direction = textDirection ?? theme.textDirection;

  final GlobalKey<_ToastContainerState> key = GlobalKey();

  widget = Align(alignment: position.align, child: widget);

  final OverlayEntry entry = OverlayEntry(
    builder: (BuildContext ctx) {
      return IgnorePointer(
        ignoring: !handleTouch!,
        child: Directionality(
          textDirection: direction,
          child: ToastContainer(
            key: key,
            duration: duration!,
            position: position!,
            movingOnWindowChange: movingOnWindowChange,
            animationBuilder: animationBuilder!,
            animationDuration: animationDuration!,
            animationCurve: animationCurve!,
            child: widget,
          ),
        ),
      );
    },
  );

  dismissOtherToast ??= theme.dismissOtherOnShow;

  if (dismissOtherToast == true) {
    ToastManager().dismissAll();
  }

  final ToastFuture future = ToastFuture._(
    entry,
    onDismiss,
    key,
    animationDuration,
  );

  if (duration != Duration.zero) {
    future.timer = Timer(duration, () {
      future.dismiss();
    });
  }

  ToastManager().addFuture(future);

  void insertOverlayEntry() {
    if (!future.dismissed) {
      future._insertEntry(context!);
    }
  }

  if (SchedulerBinding.instance.schedulerPhase !=
      SchedulerPhase.persistentCallbacks) {
    insertOverlayEntry();
  } else {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      insertOverlayEntry();
    });
  }

  return future;
}