Skip to content

Commit b7dd446

Browse files
authored
♻️ Abstract BuildContextPredicate (#95)
1 parent 397d54d commit b7dd446

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change log
22

3+
## 3.3.0
4+
5+
- Abstract `BuildContextPredicate`. (#95)
6+
37
## 3.2.0
48

59
Migrate to Flutter 3, drop supports for previous Flutter versions.

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: example_for_oktoast
22
description: A new Flutter project.
3-
version: 3.2.0+2
3+
version: 3.3.0+3
44
publish_to: none
55

66
environment:

lib/src/core/toast.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ part '../widget/container.dart';
2424
final LinkedHashMap<_OKToastState, BuildContext> _contextMap =
2525
LinkedHashMap<_OKToastState, BuildContext>();
2626

27+
typedef BuildContextPredicate = BuildContext Function(
28+
Iterable<BuildContext> list,
29+
);
30+
31+
BuildContext _defaultContextPredicate(Iterable<BuildContext> list) {
32+
return list.first;
33+
}
34+
2735
/// Show toast with [msg],
2836
ToastFuture showToast(
2937
String msg, {
3038
BuildContext? context,
39+
BuildContextPredicate buildContextPredicate = _defaultContextPredicate,
3140
Duration? duration,
3241
ToastPosition? position,
3342
TextStyle? textStyle,
@@ -47,7 +56,7 @@ ToastFuture showToast(
4756
if (context == null) {
4857
_throwIfNoContext(_contextMap.values, 'showToast');
4958
}
50-
context ??= _contextMap.values.first;
59+
context ??= buildContextPredicate(_contextMap.values);
5160

5261
final ToastTheme theme = ToastTheme.of(context);
5362
textStyle ??= theme.textStyle;
@@ -71,14 +80,15 @@ ToastFuture showToast(
7180

7281
return showToastWidget(
7382
widget,
74-
animationBuilder: animationBuilder,
75-
animationDuration: animationDuration,
7683
context: context,
84+
buildContextPredicate: buildContextPredicate,
7785
duration: duration,
7886
onDismiss: onDismiss,
7987
position: position,
8088
dismissOtherToast: dismissOtherToast,
8189
textDirection: textDirection,
90+
animationBuilder: animationBuilder,
91+
animationDuration: animationDuration,
8292
animationCurve: animationCurve,
8393
);
8494
}
@@ -87,6 +97,7 @@ ToastFuture showToast(
8797
ToastFuture showToastWidget(
8898
Widget widget, {
8999
BuildContext? context,
100+
BuildContextPredicate buildContextPredicate = _defaultContextPredicate,
90101
Duration? duration,
91102
ToastPosition? position,
92103
VoidCallback? onDismiss,
@@ -100,9 +111,9 @@ ToastFuture showToastWidget(
100111
if (context == null) {
101112
_throwIfNoContext(_contextMap.values, 'showToastWidget');
102113
}
103-
context ??= _contextMap.values.first;
104-
final ToastTheme theme = ToastTheme.of(context);
114+
context ??= buildContextPredicate(_contextMap.values);
105115

116+
final ToastTheme theme = ToastTheme.of(context);
106117
position ??= theme.position;
107118
handleTouch ??= theme.handleTouch;
108119
animationBuilder ??= theme.animationBuilder;

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: oktoast
22
description: A pure flutter toast library, support custom style/widget, easy achieve the same effect with native toasts.
33
44
homepage: https://github.com/OpenFlutter/flutter_oktoast
5-
version: 3.2.0
5+
version: 3.3.0
66

77
environment:
88
sdk: '>=2.17.0 <3.0.0'

0 commit comments

Comments
 (0)