Skip to content

Commit f6f0708

Browse files
authored
Merge pull request #7 from OpenFlutter/dismiss-all
update dismissAll params and method
2 parents 08e657c + e631417 commit f6f0708

6 files changed

Lines changed: 63 additions & 5 deletions

File tree

CHANGELOG.md

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

3+
## 2.1.1
4+
5+
use manager to manage ToastFuture
6+
7+
add a method `dismissAllToast` to dismiss all toast.
8+
9+
add a param with showToast to dismiss other toast.
10+
311
## 2.1.0
412

513
add new params to helper user listen toast dismiss
@@ -22,7 +30,7 @@ support flutter sdk 0.10 ,fix bug
2230

2331
## 1.0.3
2432

25-
update the textAlign : TextAlign.center
33+
update the textAlign : TextAlign.center
2634
update the overflow: TextOverflow.ellipsis,
2735

2836
## 1.0.2

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# oktoast
22

3-
[![pub package](https://img.shields.io/pub/v/oktoast.svg)](https://pub.dartlang.org/packages/oktoast)
4-
![Hex.pm](https://img.shields.io/hexpm/l/plug.svg)
3+
[![pub package](https://img.shields.io/pub/v/oktoast.svg)](https://pub.dartlang.org/packages/oktoast)![GitHub](https://img.shields.io/github/license/OpenFlutter/flutter_oktoast.svg)[![GitHub stars](https://img.shields.io/github/stars/OpenFlutter/flutter_oktoast.svg?style=social&label=Stars)](https://github.com/OpenFlutter/flutter_oktoast)
54

65
A library for flutter.
76

@@ -35,7 +34,7 @@ And you can completely customize the style of toast, because now you can use `sh
3534

3635
```yaml
3736
dependencies:
38-
oktoast: ^2.1.0
37+
oktoast: ^2.1.1
3938
```
4039
4140
2. import library in dart file
@@ -223,3 +222,7 @@ class _MyHomePageState extends State<MyHomePage> {
223222
224223
225224
```
225+
226+
---
227+
228+
Since version `2.1.1`, you can use `dismissAllToast()` to dismiss all toast.

lib/oktoast.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
library oktoast;
22

33
export 'src/toast.dart' show showToast, showToastWidget, OKToast, ToastPosition, ToastFuture;
4+
export 'src/toast_manager.dart' show dismissAllToast;

lib/src/toast.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ library oktoast;
33
import 'dart:collection';
44

55
import 'package:flutter/material.dart';
6+
import 'package:oktoast/src/toast_manager.dart';
67

78
LinkedHashMap<_OKToastState, BuildContext> _contextMap = LinkedHashMap();
89
const _defaultDuration = Duration(
@@ -99,6 +100,7 @@ class _OKToastState extends State<OKToast> {
99100
}
100101
}
101102

103+
/// show toast with [msg],
102104
ToastFuture showToast(
103105
String msg, {
104106
BuildContext context,
@@ -108,6 +110,7 @@ ToastFuture showToast(
108110
Color backgroundColor,
109111
double radius,
110112
VoidCallback onDismiss,
113+
bool dismissOtherToast = false,
111114
}) {
112115
context ??= _contextMap.values.first;
113116

@@ -142,14 +145,17 @@ ToastFuture showToast(
142145
context: context,
143146
duration: duration,
144147
onDismiss: onDismiss,
148+
dismissOtherToast: dismissOtherToast,
145149
);
146150
}
147151

152+
/// show [widget] with oktoast
148153
ToastFuture showToastWidget(
149154
Widget widget, {
150155
BuildContext context,
151156
Duration duration = _defaultDuration,
152157
VoidCallback onDismiss,
158+
bool dismissOtherToast = false,
153159
}) {
154160
context ??= _contextMap.values.first;
155161
OverlayEntry entry;
@@ -163,13 +169,18 @@ ToastFuture showToastWidget(
163169
);
164170
});
165171

172+
if (dismissOtherToast == true) {
173+
ToastManager().dismissAll();
174+
}
175+
166176
future = ToastFuture._(entry, onDismiss);
167177

168178
Future.delayed(duration, () {
169179
future.dismiss();
170180
});
171181

172182
Overlay.of(context).insert(entry);
183+
ToastManager().addFuture(future);
173184

174185
return future;
175186
}
@@ -259,6 +270,7 @@ class _ToastTheme extends InheritedWidget {
259270
static _ToastTheme of(BuildContext context) => context.inheritFromWidgetOfExactType(_ToastTheme);
260271
}
261272

273+
/// use the [dismiss] to dismiss toast.
262274
class ToastFuture {
263275
final OverlayEntry _entry;
264276
final VoidCallback _onDismiss;
@@ -274,5 +286,6 @@ class ToastFuture {
274286
_isShow = false;
275287
_entry.remove();
276288
_onDismiss?.call();
289+
ToastManager().removeFuture(this);
277290
}
278291
}

lib/src/toast_manager.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'toast.dart';
2+
3+
class ToastManager {
4+
ToastManager._();
5+
6+
static ToastManager _instance;
7+
8+
factory ToastManager() {
9+
_instance ??= ToastManager._();
10+
return _instance;
11+
}
12+
13+
Set<ToastFuture> toastSet = Set();
14+
15+
void dismissAll() {
16+
toastSet.toList().forEach((v) {
17+
v.dismiss();
18+
});
19+
}
20+
21+
void removeFuture(ToastFuture future) {
22+
toastSet.remove(future);
23+
}
24+
25+
void addFuture(ToastFuture future) {
26+
toastSet.add(future);
27+
}
28+
}
29+
30+
/// use the method to dismiss all toast.
31+
void dismissAllToast() {
32+
ToastManager().dismissAll();
33+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: oktoast
22
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.
3-
version: 2.1.0
3+
version: 2.1.1
44
author: Caijinglong<cjl_spy@163.com>
55
homepage: https://github.com/OpenFlutter/flutter_oktoast
66
email: cjl_spy@163.com

0 commit comments

Comments
 (0)