showToastWidget function
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,
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;
}