showToastWidget function

ToastFuture showToastWidget(
  1. Widget widget, {
  2. BuildContext? context,
  3. Duration? duration,
  4. Duration? animDuration,
  5. VoidCallback? onDismiss,
  6. bool? dismissOtherToast,
  7. TextDirection? textDirection,
  8. Alignment? alignment,
  9. Axis? axis,
  10. Offset? startOffset,
  11. Offset? endOffset,
  12. Offset? reverseStartOffset,
  13. Offset? reverseEndOffset,
  14. StyledToastPosition? position,
  15. StyledToastAnimation? animation,
  16. StyledToastAnimation? reverseAnimation,
  17. Curve? curve,
  18. Curve? reverseCurve,
  19. bool? isHideKeyboard,
  20. CustomAnimationBuilder? animationBuilder,
  21. CustomAnimationBuilder? reverseAnimBuilder,
  22. bool? isIgnoring,
  23. OnInitStateCallback? onInitState,
})

Show custom content widget for toasting.

Implementation

ToastFuture showToastWidget(
  Widget widget, {
  BuildContext? context,
  Duration? duration,
  Duration? animDuration,
  VoidCallback? onDismiss,
  bool? dismissOtherToast,
  TextDirection? textDirection,
  Alignment? alignment,
  Axis? axis,
  Offset? startOffset,
  Offset? endOffset,
  Offset? reverseStartOffset,
  Offset? reverseEndOffset,
  StyledToastPosition? position,
  StyledToastAnimation? animation,
  StyledToastAnimation? reverseAnimation,
  Curve? curve,
  Curve? reverseCurve,
  bool? isHideKeyboard,
  CustomAnimationBuilder? animationBuilder,
  CustomAnimationBuilder? reverseAnimBuilder,
  bool? isIgnoring,
  OnInitStateCallback? onInitState,
}) {
  OverlayEntry entry;
  ToastFuture future;

  context ??= currentContext;
  assert(context != null);

  final toastTheme = StyledToastTheme.maybeOf(context!);

  isHideKeyboard ??= toastTheme?.isHideKeyboard ?? false;

  duration ??= toastTheme?.duration ?? _defaultDuration;

  animDuration ??= toastTheme?.animDuration ?? animationDuration;

  dismissOtherToast ??= toastTheme?.dismissOtherOnShow ?? true;

  textDirection ??=
      textDirection ?? toastTheme?.textDirection ?? TextDirection.ltr;

  position ??= toastTheme?.toastPositions ?? StyledToastPosition.bottom;

  alignment ??= toastTheme?.alignment ?? Alignment.center;

  axis ??= toastTheme?.axis ?? Axis.vertical;

  startOffset ??= toastTheme?.startOffset;

  endOffset ??= toastTheme?.endOffset;

  reverseStartOffset ??= toastTheme?.reverseStartOffset;

  reverseEndOffset ??= toastTheme?.reverseEndOffset;

  curve ??= curve ?? toastTheme?.curve ?? Curves.linear;

  reverseCurve ??= reverseCurve ?? toastTheme?.reverseCurve ?? Curves.linear;

  animation ??=
      animation ?? toastTheme?.toastAnimation ?? StyledToastAnimation.size;

  reverseAnimation ??= reverseAnimation ??
      toastTheme?.reverseAnimation ??
      StyledToastAnimation.size;

  animationBuilder ??= animationBuilder ?? toastTheme?.animationBuilder;

  reverseAnimBuilder ??= reverseAnimBuilder ?? toastTheme?.reverseAnimBuilder;

  onInitState ??= onInitState ?? toastTheme?.onInitState;

  onDismiss ??= onDismiss ?? toastTheme?.onDismiss;

  isIgnoring ??= toastTheme?.isIgnoring ?? true;

  if (isHideKeyboard) {
    /// Hide keyboard.
    FocusScope.of(context).requestFocus(FocusNode());
  }

  GlobalKey<StyledToastWidgetState> key = GlobalKey();

  entry = OverlayEntry(builder: (ctx) {
    return IgnorePointer(
      ignoring: isIgnoring!,
      child: _StyledToastWidget(
        duration: duration!,
        animDuration: animDuration!,
        position: position,
        animation: animation,
        reverseAnimation: reverseAnimation,
        alignment: alignment,
        axis: axis,
        startOffset: startOffset,
        endOffset: endOffset,
        reverseStartOffset: reverseStartOffset,
        reverseEndOffset: reverseEndOffset,
        curve: curve!,
        reverseCurve: reverseCurve!,
        key: key,
        animationBuilder: animationBuilder,
        reverseAnimBuilder: reverseAnimBuilder,
        onInitState: onInitState,
        child: Directionality(
          textDirection: textDirection!,
          child: Material(
            child: widget,
            color: Colors.transparent,
          ),
        ),
      ),
    );
  });

  if (dismissOtherToast) {
    dismissAllToast();
  }

  future = ToastFuture.create(duration, entry, onDismiss, key);

  Overlay.of(context).insert(entry);
  ToastManager().addFuture(future);

  return future;
}