diff --git a/CHANGELOG.md b/CHANGELOG.md index 36465f8..6ee81d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Change log +## 2.1.1 + +use manager to manage ToastFuture + +add a method `dismissAllToast` to dismiss all toast. + +add a param with showToast to dismiss other toast. + ## 2.1.0 add new params to helper user listen toast dismiss @@ -22,7 +30,7 @@ support flutter sdk 0.10 ,fix bug ## 1.0.3 -update the textAlign : TextAlign.center +update the textAlign : TextAlign.center update the overflow: TextOverflow.ellipsis, ## 1.0.2 diff --git a/README.md b/README.md index 4eb1150..6a83485 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # oktoast -[![pub package](https://img.shields.io/pub/v/oktoast.svg)](https://pub.dartlang.org/packages/oktoast) -![Hex.pm](https://img.shields.io/hexpm/l/plug.svg) +[![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) A library for flutter. @@ -35,7 +34,7 @@ And you can completely customize the style of toast, because now you can use `sh ```yaml dependencies: - oktoast: ^2.1.0 + oktoast: ^2.1.1 ``` 2. import library in dart file @@ -223,3 +222,7 @@ class _MyHomePageState extends State { ``` + +--- + +Since version `2.1.1`, you can use `dismissAllToast()` to dismiss all toast. diff --git a/lib/oktoast.dart b/lib/oktoast.dart index fe75863..377bd34 100644 --- a/lib/oktoast.dart +++ b/lib/oktoast.dart @@ -1,3 +1,4 @@ library oktoast; export 'src/toast.dart' show showToast, showToastWidget, OKToast, ToastPosition, ToastFuture; +export 'src/toast_manager.dart' show dismissAllToast; diff --git a/lib/src/toast.dart b/lib/src/toast.dart index a26b668..2e0d458 100644 --- a/lib/src/toast.dart +++ b/lib/src/toast.dart @@ -3,6 +3,7 @@ library oktoast; import 'dart:collection'; import 'package:flutter/material.dart'; +import 'package:oktoast/src/toast_manager.dart'; LinkedHashMap<_OKToastState, BuildContext> _contextMap = LinkedHashMap(); const _defaultDuration = Duration( @@ -99,6 +100,7 @@ class _OKToastState extends State { } } +/// show toast with [msg], ToastFuture showToast( String msg, { BuildContext context, @@ -108,6 +110,7 @@ ToastFuture showToast( Color backgroundColor, double radius, VoidCallback onDismiss, + bool dismissOtherToast = false, }) { context ??= _contextMap.values.first; @@ -142,14 +145,17 @@ ToastFuture showToast( context: context, duration: duration, onDismiss: onDismiss, + dismissOtherToast: dismissOtherToast, ); } +/// show [widget] with oktoast ToastFuture showToastWidget( Widget widget, { BuildContext context, Duration duration = _defaultDuration, VoidCallback onDismiss, + bool dismissOtherToast = false, }) { context ??= _contextMap.values.first; OverlayEntry entry; @@ -163,6 +169,10 @@ ToastFuture showToastWidget( ); }); + if (dismissOtherToast == true) { + ToastManager().dismissAll(); + } + future = ToastFuture._(entry, onDismiss); Future.delayed(duration, () { @@ -170,6 +180,7 @@ ToastFuture showToastWidget( }); Overlay.of(context).insert(entry); + ToastManager().addFuture(future); return future; } @@ -259,6 +270,7 @@ class _ToastTheme extends InheritedWidget { static _ToastTheme of(BuildContext context) => context.inheritFromWidgetOfExactType(_ToastTheme); } +/// use the [dismiss] to dismiss toast. class ToastFuture { final OverlayEntry _entry; final VoidCallback _onDismiss; @@ -274,5 +286,6 @@ class ToastFuture { _isShow = false; _entry.remove(); _onDismiss?.call(); + ToastManager().removeFuture(this); } } diff --git a/lib/src/toast_manager.dart b/lib/src/toast_manager.dart new file mode 100644 index 0000000..1d4754c --- /dev/null +++ b/lib/src/toast_manager.dart @@ -0,0 +1,33 @@ +import 'toast.dart'; + +class ToastManager { + ToastManager._(); + + static ToastManager _instance; + + factory ToastManager() { + _instance ??= ToastManager._(); + return _instance; + } + + Set toastSet = Set(); + + void dismissAll() { + toastSet.toList().forEach((v) { + v.dismiss(); + }); + } + + void removeFuture(ToastFuture future) { + toastSet.remove(future); + } + + void addFuture(ToastFuture future) { + toastSet.add(future); + } +} + +/// use the method to dismiss all toast. +void dismissAllToast() { + ToastManager().dismissAll(); +} diff --git a/pubspec.yaml b/pubspec.yaml index 3fcff22..2a89168 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: oktoast 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. -version: 2.1.0 +version: 2.1.1 author: Caijinglong homepage: https://github.com/OpenFlutter/flutter_oktoast email: cjl_spy@163.com