Skip to content

update dismissAll params and method #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -223,3 +222,7 @@ class _MyHomePageState extends State<MyHomePage> {


```

---

Since version `2.1.1`, you can use `dismissAllToast()` to dismiss all toast.
1 change: 1 addition & 0 deletions lib/oktoast.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
library oktoast;

export 'src/toast.dart' show showToast, showToastWidget, OKToast, ToastPosition, ToastFuture;
export 'src/toast_manager.dart' show dismissAllToast;
13 changes: 13 additions & 0 deletions lib/src/toast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -99,6 +100,7 @@ class _OKToastState extends State<OKToast> {
}
}

/// show toast with [msg],
ToastFuture showToast(
String msg, {
BuildContext context,
Expand All @@ -108,6 +110,7 @@ ToastFuture showToast(
Color backgroundColor,
double radius,
VoidCallback onDismiss,
bool dismissOtherToast = false,
}) {
context ??= _contextMap.values.first;

Expand Down Expand Up @@ -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;
Expand All @@ -163,13 +169,18 @@ ToastFuture showToastWidget(
);
});

if (dismissOtherToast == true) {
ToastManager().dismissAll();
}

future = ToastFuture._(entry, onDismiss);

Future.delayed(duration, () {
future.dismiss();
});

Overlay.of(context).insert(entry);
ToastManager().addFuture(future);

return future;
}
Expand Down Expand Up @@ -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;
Expand All @@ -274,5 +286,6 @@ class ToastFuture {
_isShow = false;
_entry.remove();
_onDismiss?.call();
ToastManager().removeFuture(this);
}
}
33 changes: 33 additions & 0 deletions lib/src/toast_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'toast.dart';

class ToastManager {
ToastManager._();

static ToastManager _instance;

factory ToastManager() {
_instance ??= ToastManager._();
return _instance;
}

Set<ToastFuture> 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();
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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<[email protected]>
homepage: https://github.com/OpenFlutter/flutter_oktoast
email: [email protected]
Expand Down