Skip to content

Commit 446bc7a

Browse files
authored
🔖 ## 3.1.3 (#86)
1 parent 3c7e79c commit 446bc7a

File tree

5 files changed

+79
-24
lines changed

5 files changed

+79
-24
lines changed

‎CHANGELOG.md

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

3+
## 3.1.3
4+
5+
- Throw a more precise error if the `OKToast` widget is not wrapped correctly.
6+
- Bind `ToastFuture` with `OverlayEntry` mounted state.
7+
38
## 3.1.2
49

510
- Deliver overlay insertion into microtask.

‎README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ You can completely customize the style of toast.
2323

2424
### 3.x.x
2525

26-
Starting from the 3.x version, OkToast provides a null-safety version,
26+
Starting from the 3.x version, OKToast provides a null-safety version,
2727
the specific introduction of null-safety can be viewed in [dart][dart-safe] or [flutter][flutter-safe].
2828

2929
[flutter-safe]: https://flutter.dev/docs/null-safety
@@ -33,7 +33,7 @@ The 2.3.2 version is the last version that does not support null-safety.
3333

3434
### About version 1.x
3535

36-
if you use OkToast 1.x, Please use the 1.x branch, and read version readme.
36+
if you use OKToast 1.x, Please use the 1.x branch, and read version readme.
3737

3838
Proposed migration to 2.x version. The new version does not require buildContext.
3939

@@ -47,7 +47,7 @@ latest version: [![pub package](https://img.shields.io/pub/v/oktoast.svg)](https
4747

4848
```yaml
4949
dependencies:
50-
oktoast: ^3.0.0 # such as version, you need use the latest version of pub.
50+
oktoast: ^latest_version
5151
```
5252
5353
### 2. Import library in dart file
@@ -65,23 +65,23 @@ OKToast(
6565
);
6666
```
6767

68-
tips:
69-
If you happened error like: `No MediaQuery widget found`
70-
71-
you can try to use next [code](https://github.com/OpenFlutter/flutter_oktoast/issues/53#issuecomment-628431625) to include `oktoast` to your App.
68+
Tips:
69+
If you happened error like: `No MediaQuery widget found`,
70+
you can try to use this [code](https://github.com/OpenFlutter/flutter_oktoast/issues/53#issuecomment-628431625)
71+
to include `OKToast` to your App.
7272

7373
```dart
74-
MaterialApp(builder: (context, widget) {
75-
return OKToast(
76-
child: widget,
77-
);
78-
});
74+
MaterialApp(
75+
builder: (BuildContext context, Widget? widget) {
76+
return OKToast(child: widget);
77+
},
78+
);
7979
```
8080

8181
### 4. Call method `showToast`
8282

8383
```dart
84-
showToast("content");
84+
showToast('content');
8585
8686
// position and second have default value, is optional
8787
@@ -99,7 +99,7 @@ There are two reasons why you need to wrap MaterialApp
9999

100100
### OKToast params
101101

102-
oktoast have default style, and you also can custom style or other behavior.
102+
OKToast have default style, and you also can custom style or other behavior.
103103

104104
| name | type | need | desc |
105105
| :------------------: | :---------------------: | :------: | :-----------------------------------------------------------: |
@@ -191,7 +191,7 @@ class MyApp extends StatelessWidget {
191191
@override
192192
Widget build(BuildContext context) {
193193
return OKToast(
194-
//2. wrap your app with OKToast
194+
// 2. wrap your app with OKToast
195195
child: MaterialApp(
196196
title: 'Flutter Demo',
197197
theme: ThemeData(

‎lib/src/core/toast.dart

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ ToastFuture showToast(
4444
BoxConstraints? constraints,
4545
EdgeInsetsGeometry? margin = const EdgeInsets.all(50),
4646
}) {
47+
if (context == null) {
48+
_throwIfNoContext(_contextMap.values, 'showToast');
49+
}
4750
context ??= _contextMap.values.first;
4851

4952
final _ToastTheme theme = _ToastTheme.of(context);
@@ -94,6 +97,9 @@ ToastFuture showToastWidget(
9497
Duration? animationDuration,
9598
Curve? animationCurve,
9699
}) {
100+
if (context == null) {
101+
_throwIfNoContext(_contextMap.values, 'showToastWidget');
102+
}
97103
context ??= _contextMap.values.first;
98104
final _ToastTheme theme = _ToastTheme.of(context);
99105

@@ -150,14 +156,40 @@ ToastFuture showToastWidget(
150156
});
151157
}
152158

153-
Future<void>.microtask(() {
159+
ToastManager().addFuture(future);
160+
161+
void _insertOverlayEntry() {
154162
Overlay.of(context!)?.insert(entry);
155-
ToastManager().addFuture(future);
156-
});
163+
}
164+
165+
if (!context.debugDoingBuild && context.owner?.debugBuilding != true) {
166+
_insertOverlayEntry();
167+
} else {
168+
WidgetsBinding.instance?.addPostFrameCallback((_) {
169+
if (!future.dismissed) {
170+
_insertOverlayEntry();
171+
}
172+
});
173+
}
157174

158175
return future;
159176
}
160177

161178
void dismissAllToast({bool showAnim = false}) {
162179
ToastManager().dismissAll(showAnim: showAnim);
163180
}
181+
182+
void _throwIfNoContext(Iterable<BuildContext> contexts, String methodName) {
183+
final List<DiagnosticsNode> information = <DiagnosticsNode>[
184+
ErrorSummary('No OKToast widget found.'),
185+
ErrorDescription(
186+
'$methodName requires an OKToast widget ancestor '
187+
'for correct operation.',
188+
),
189+
ErrorHint(
190+
'The most common way to add an OKToast to an application '
191+
'is to wrap a OKToast upon a WidgetsApp(MaterialApp/CupertinoApp).',
192+
),
193+
];
194+
throw FlutterError.fromParts(information);
195+
}

‎lib/src/core/toast_future.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,35 @@ class ToastFuture {
77
this._onDismiss,
88
this._containerKey,
99
this.animationDuration,
10-
);
10+
) {
11+
_entry.addListener(_mountedListener);
12+
}
1113

1214
final OverlayEntry _entry;
1315
final VoidCallback? _onDismiss;
1416
final GlobalKey<__ToastContainerState> _containerKey;
1517
final Duration animationDuration;
1618

1719
Timer? timer;
18-
bool _isShow = true;
20+
bool _isShow = false;
21+
bool _dismissed = false;
22+
23+
bool get mounted => _isShow;
24+
25+
bool get dismissed => _dismissed;
26+
27+
void _mountedListener() {
28+
_isShow = _entry.mounted;
29+
}
30+
31+
void _removeEntry() {
32+
_entry.removeListener(_mountedListener);
33+
_entry.remove();
34+
}
1935

2036
void dismiss({bool showAnim = false}) {
2137
if (!_isShow) {
38+
_dismissed = true;
2239
return;
2340
}
2441
_isShow = false;
@@ -27,11 +44,12 @@ class ToastFuture {
2744

2845
if (showAnim) {
2946
_containerKey.currentState?.showDismissAnim();
30-
Future<void>.delayed(animationDuration, _entry.remove);
47+
Future<void>.delayed(animationDuration, _removeEntry);
3148
} else {
32-
_entry.remove();
49+
_removeEntry();
3350
}
3451

3552
timer?.cancel();
53+
_dismissed = true;
3654
}
3755
}

‎pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: oktoast
2-
description: A pure flutter toast library Support custom style/widget. Easy to use. You can use this library to achieve the same effect as Android toast.
2+
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.1.2
5+
version: 3.1.3
66

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

0 commit comments

Comments
 (0)