Skip to content

Commit 297d6c7

Browse files
authored
Merge pull request #30 from OpenFlutter/dev
Merge dev to master
2 parents 50338b7 + c1da1a6 commit 297d6c7

File tree

12 files changed

+479
-436
lines changed

12 files changed

+479
-436
lines changed

CHANGELOG.md

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

3+
## 2.2.0
4+
5+
New feature:
6+
7+
- handleTouch: Caller can use this property to respond to click events.
8+
9+
Change:
10+
11+
- This is a **breaking change** : The default value of dismissOtherToast of showToast is changed to better match the OKToast overall settings. .
12+
313
## 2.1.9
414

515
Fix:

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ There are two reasons why you need to wrap MaterialApp
7878

7979
### OKToast params
8080

81-
oktoast have default style, and you also can custom style.
81+
oktoast have default style, and you also can custom style or other behavior.
8282

8383
| name | type | need | desc |
8484
| :------------------: | :----------------: | :------: | :-----------------------------------------------------------: |
@@ -92,6 +92,7 @@ oktoast have default style, and you also can custom style.
9292
| textDirection | TextDirection | optional | |
9393
| textPadding | EdgeInsetsGeometry | optional | Outer margin of text |
9494
| textAlign | TextAlign | optional | When the text wraps, the align of the text. |
95+
| handleTouch | bool | optional | Default is false, if it's true, can responed use touch event. |
9596

9697
### showToast
9798

@@ -118,6 +119,17 @@ Display custom widgets on toast
118119

119120
param see showToast
120121

122+
| name | type | need | desc |
123+
| :---------------: | :-----------: | :------: | :-----------------------------------------------------------: |
124+
| widget | Widget | required | The widget you want to display. |
125+
| context | BuildContext | optional | |
126+
| duration | Duration | optional | |
127+
| position | ToastPosition | optional | |
128+
| onDismiss | Function | optional | |
129+
| dismissOtherToast | bool | optional | |
130+
| textDirection | TextDirection | optional | |
131+
| handleTouch | bool | optional | Default is false, if it's true, can responed use touch event. |
132+
121133
### dismissAllToast
122134

123135
dismiss all toast

lib/oktoast.dart

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

3-
export 'src/toast.dart'
4-
show showToast, showToastWidget, OKToast, ToastPosition, ToastFuture;
5-
export 'src/toast_manager.dart' show dismissAllToast;
3+
export 'src/core/toast.dart'
4+
show showToast, showToastWidget, OKToast, ToastFuture, dismissAllToast;
5+
6+
export 'src/core/position.dart';

lib/src/core/position.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:flutter/widgets.dart';
2+
3+
class ToastPosition {
4+
final AlignmentGeometry align;
5+
final double offset;
6+
7+
const ToastPosition({this.align = Alignment.center, this.offset = 0.0});
8+
9+
static const center =
10+
const ToastPosition(align: Alignment.center, offset: 0.0);
11+
12+
static const bottom =
13+
const ToastPosition(align: Alignment.bottomCenter, offset: -30.0);
14+
15+
static const top =
16+
const ToastPosition(align: Alignment.topCenter, offset: 75.0);
17+
18+
@override
19+
String toString() {
20+
return "ToastPosition [ align = $align, offset = $offset ] ";
21+
}
22+
}

lib/src/core/toast.dart

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
library oktoast;
2+
3+
import 'dart:async';
4+
import 'dart:collection';
5+
import 'dart:ui' as ui;
6+
7+
import 'package:flutter/material.dart';
8+
9+
import 'position.dart';
10+
import 'toast_manager.dart';
11+
12+
part 'toast_future.dart';
13+
part '../widget/theme.dart';
14+
part '../widget/oktoast.dart';
15+
part '../widget/container.dart';
16+
17+
LinkedHashMap<_OKToastState, BuildContext> _contextMap = LinkedHashMap();
18+
const _defaultDuration = Duration(
19+
milliseconds: 2300,
20+
);
21+
22+
const _opacityDuration = Duration(milliseconds: 250);
23+
24+
/// show toast with [msg],
25+
ToastFuture showToast(
26+
String msg, {
27+
BuildContext context,
28+
Duration duration = _defaultDuration,
29+
ToastPosition position,
30+
TextStyle textStyle,
31+
EdgeInsetsGeometry textPadding,
32+
Color backgroundColor,
33+
double radius,
34+
VoidCallback onDismiss,
35+
TextDirection textDirection,
36+
bool dismissOtherToast,
37+
TextAlign textAlign,
38+
}) {
39+
context ??= _contextMap.values.first;
40+
41+
textStyle ??= _ToastTheme.of(context).textStyle ?? TextStyle(fontSize: 15.0);
42+
43+
textAlign = _ToastTheme.of(context).textAlign;
44+
45+
textPadding ??= _ToastTheme.of(context).textPadding;
46+
47+
position ??= _ToastTheme.of(context).position;
48+
backgroundColor ??= _ToastTheme.of(context).backgroundColor;
49+
radius ??= _ToastTheme.of(context).radius;
50+
51+
var direction = textDirection ??
52+
_ToastTheme.of(context).textDirection ??
53+
TextDirection.ltr;
54+
55+
Widget widget = Container(
56+
margin: const EdgeInsets.all(50.0),
57+
decoration: BoxDecoration(
58+
color: backgroundColor,
59+
borderRadius: BorderRadius.circular(radius),
60+
),
61+
padding: textPadding,
62+
child: ClipRect(
63+
child: Text(
64+
msg,
65+
style: textStyle,
66+
textAlign: textAlign,
67+
),
68+
),
69+
);
70+
71+
return showToastWidget(
72+
widget,
73+
context: context,
74+
duration: duration,
75+
onDismiss: onDismiss,
76+
position: position,
77+
dismissOtherToast: dismissOtherToast,
78+
textDirection: direction,
79+
);
80+
}
81+
82+
/// show [widget] with oktoast
83+
ToastFuture showToastWidget(
84+
Widget widget, {
85+
BuildContext context,
86+
Duration duration = _defaultDuration,
87+
ToastPosition position,
88+
VoidCallback onDismiss,
89+
bool dismissOtherToast,
90+
TextDirection textDirection,
91+
bool handleTouch,
92+
}) {
93+
context ??= _contextMap.values.first;
94+
OverlayEntry entry;
95+
ToastFuture future;
96+
position ??= _ToastTheme.of(context).position;
97+
98+
handleTouch ??= _ToastTheme.of(context).handleTouch;
99+
100+
var movingOnWindowChange =
101+
_ToastTheme.of(context)?.movingOnWindowChange ?? false;
102+
103+
var direction = textDirection ??
104+
_ToastTheme.of(context).textDirection ??
105+
TextDirection.ltr;
106+
107+
GlobalKey<__ToastContainerState> key = GlobalKey();
108+
109+
widget = Align(
110+
child: widget,
111+
alignment: position.align,
112+
);
113+
114+
entry = OverlayEntry(builder: (ctx) {
115+
return IgnorePointer(
116+
ignoring: !handleTouch,
117+
child: _ToastContainer(
118+
duration: duration,
119+
position: position,
120+
movingOnWindowChange: movingOnWindowChange,
121+
key: key,
122+
child: Directionality(
123+
textDirection: direction,
124+
child: widget,
125+
),
126+
),
127+
);
128+
});
129+
130+
dismissOtherToast ??= _ToastTheme.of(context).dismissOtherOnShow ?? false;
131+
132+
if (dismissOtherToast == true) {
133+
ToastManager().dismissAll();
134+
}
135+
136+
future = ToastFuture._(entry, onDismiss, key);
137+
138+
Future.delayed(duration, () {
139+
future.dismiss();
140+
});
141+
142+
Overlay.of(context).insert(entry);
143+
ToastManager().addFuture(future);
144+
145+
return future;
146+
}
147+
148+
/// use the method to dismiss all toast.
149+
void dismissAllToast({bool showAnim = false}) {
150+
ToastManager().dismissAll(showAnim: showAnim);
151+
}

lib/src/toast_future.dart renamed to lib/src/core/toast_future.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of oktoast;
1+
part of 'toast.dart';
22

33
/// use the [dismiss] to dismiss toast.
44
class ToastFuture {

lib/src/toast_manager.dart renamed to lib/src/core/toast_manager.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,3 @@ class ToastManager {
2626
toastSet.add(future);
2727
}
2828
}
29-
30-
/// use the method to dismiss all toast.
31-
void dismissAllToast({bool showAnim = false}) {
32-
ToastManager().dismissAll(showAnim: showAnim);
33-
}

0 commit comments

Comments
 (0)