@@ -24,10 +24,19 @@ part '../widget/container.dart';
24
24
final LinkedHashMap <_OKToastState , BuildContext > _contextMap =
25
25
LinkedHashMap <_OKToastState , BuildContext >();
26
26
27
+ typedef BuildContextPredicate = BuildContext Function (
28
+ Iterable <BuildContext > list,
29
+ );
30
+
31
+ BuildContext _defaultContextPredicate (Iterable <BuildContext > list) {
32
+ return list.first;
33
+ }
34
+
27
35
/// Show toast with [msg] ,
28
36
ToastFuture showToast (
29
37
String msg, {
30
38
BuildContext ? context,
39
+ BuildContextPredicate buildContextPredicate = _defaultContextPredicate,
31
40
Duration ? duration,
32
41
ToastPosition ? position,
33
42
TextStyle ? textStyle,
@@ -47,7 +56,7 @@ ToastFuture showToast(
47
56
if (context == null ) {
48
57
_throwIfNoContext (_contextMap.values, 'showToast' );
49
58
}
50
- context ?? = _contextMap.values.first ;
59
+ context ?? = buildContextPredicate ( _contextMap.values) ;
51
60
52
61
final ToastTheme theme = ToastTheme .of (context);
53
62
textStyle ?? = theme.textStyle;
@@ -71,14 +80,15 @@ ToastFuture showToast(
71
80
72
81
return showToastWidget (
73
82
widget,
74
- animationBuilder: animationBuilder,
75
- animationDuration: animationDuration,
76
83
context: context,
84
+ buildContextPredicate: buildContextPredicate,
77
85
duration: duration,
78
86
onDismiss: onDismiss,
79
87
position: position,
80
88
dismissOtherToast: dismissOtherToast,
81
89
textDirection: textDirection,
90
+ animationBuilder: animationBuilder,
91
+ animationDuration: animationDuration,
82
92
animationCurve: animationCurve,
83
93
);
84
94
}
@@ -87,6 +97,7 @@ ToastFuture showToast(
87
97
ToastFuture showToastWidget (
88
98
Widget widget, {
89
99
BuildContext ? context,
100
+ BuildContextPredicate buildContextPredicate = _defaultContextPredicate,
90
101
Duration ? duration,
91
102
ToastPosition ? position,
92
103
VoidCallback ? onDismiss,
@@ -100,9 +111,9 @@ ToastFuture showToastWidget(
100
111
if (context == null ) {
101
112
_throwIfNoContext (_contextMap.values, 'showToastWidget' );
102
113
}
103
- context ?? = _contextMap.values.first;
104
- final ToastTheme theme = ToastTheme .of (context);
114
+ context ?? = buildContextPredicate (_contextMap.values);
105
115
116
+ final ToastTheme theme = ToastTheme .of (context);
106
117
position ?? = theme.position;
107
118
handleTouch ?? = theme.handleTouch;
108
119
animationBuilder ?? = theme.animationBuilder;
0 commit comments