Skip to content

Commit 2b230c5

Browse files
authored
Merge pull request #10 from OpenFlutter/size-change-patch
fix #8
2 parents a87b2d8 + dbb61f9 commit 2b230c5

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

CHANGELOG.md

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

3+
## 2.1.3
4+
5+
When ui.window's size changes, toast is moved.
6+
37
## 2.1.2
48

59
Now, `OKToast` add a params `dismissOtherOnShow` to dismiss other toast.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ And you can completely customize the style of toast, because now you can use `sh
3636

3737
```yaml
3838
dependencies:
39-
oktoast: ^2.1.2
39+
oktoast: ^2.1.3
4040
```
4141
4242
2. import library in dart file
@@ -80,6 +80,7 @@ position: the toast align and padding
8080
child: the application
8181
onDismiss: on toast dismiss will be called.
8282
dismissOtherOnShow: If true, other toasts will be dismissed. Default false.
83+
movingOnWindowChange: If true, when the size changes, toast is moved. Default true.
8384
```
8485

8586
about return type:

lib/src/toast.dart

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

33
import 'dart:collection';
4+
import 'dart:ui' as ui;
45

56
import 'package:flutter/material.dart';
67
import 'package:oktoast/src/toast_manager.dart';
@@ -25,13 +26,16 @@ class OKToast extends StatefulWidget {
2526

2627
final bool dismissOtherOnShow;
2728

29+
final bool movingOnWindowChange;
30+
2831
const OKToast({
2932
Key key,
3033
@required this.child,
3134
this.textStyle,
3235
this.radius = 10.0,
3336
this.position = ToastPosition.center,
3437
this.dismissOtherOnShow = false,
38+
this.movingOnWindowChange = true,
3539
Color backgroundColor,
3640
}) : this.backgroundColor = backgroundColor ?? const Color(0xDD000000),
3741
super(key: key);
@@ -100,6 +104,7 @@ class _OKToastState extends State<OKToast> {
100104
textStyle: textStyle,
101105
position: widget.position,
102106
dismissOtherOnShow: widget.dismissOtherOnShow,
107+
movingOnWindowChange: widget.movingOnWindowChange,
103108
);
104109
}
105110
}
@@ -164,11 +169,15 @@ ToastFuture showToastWidget(
164169
context ??= _contextMap.values.first;
165170
OverlayEntry entry;
166171
ToastFuture future;
172+
173+
var movingOnWindowChange =
174+
_ToastTheme.of(context)?.movingOnWindowChange ?? false;
167175
entry = OverlayEntry(builder: (ctx) {
168176
return IgnorePointer(
169177
child: _ToastContainer(
170178
duration: duration,
171179
child: widget,
180+
movingOnWindowChange: movingOnWindowChange,
172181
),
173182
);
174183
});
@@ -194,15 +203,24 @@ ToastFuture showToastWidget(
194203
class _ToastContainer extends StatefulWidget {
195204
final Duration duration;
196205
final Widget child;
197-
const _ToastContainer({Key key, this.duration, this.child}) : super(key: key);
206+
final bool movingOnWindowChange;
207+
const _ToastContainer({
208+
Key key,
209+
this.duration,
210+
this.child,
211+
this.movingOnWindowChange = false,
212+
}) : super(key: key);
198213

199214
@override
200215
__ToastContainerState createState() => __ToastContainerState();
201216
}
202217

203-
class __ToastContainerState extends State<_ToastContainer> {
218+
class __ToastContainerState extends State<_ToastContainer>
219+
with WidgetsBindingObserver {
204220
double opacity = 0.0;
205221

222+
bool get movingOnWindowChange => widget.movingOnWindowChange;
223+
206224
@override
207225
void initState() {
208226
super.initState();
@@ -223,21 +241,41 @@ class __ToastContainerState extends State<_ToastContainer> {
223241
opacity = 0.0;
224242
});
225243
});
244+
245+
WidgetsBinding.instance.addObserver(this);
246+
}
247+
248+
@override
249+
void didChangeMetrics() {
250+
super.didChangeMetrics();
251+
setState(() {});
226252
}
227253

228254
@override
229255
void dispose() {
256+
WidgetsBinding.instance.removeObserver(this);
230257
super.dispose();
231258
}
232259

233260
@override
234261
Widget build(BuildContext context) {
235-
// print("mq bottom = ${MediaQuery.of(context).viewInsets.bottom}");
236-
return AnimatedOpacity(
262+
Widget w = AnimatedOpacity(
237263
duration: _opacityDuration,
238264
child: widget.child,
239265
opacity: opacity,
240266
);
267+
268+
if (movingOnWindowChange != true) {
269+
return w;
270+
}
271+
272+
var mediaQueryData = MediaQueryData.fromWindow(ui.window);
273+
274+
return AnimatedContainer(
275+
padding: EdgeInsets.only(bottom: mediaQueryData.viewInsets.bottom),
276+
duration: _opacityDuration,
277+
child: w,
278+
);
241279
}
242280
}
243281

@@ -268,6 +306,8 @@ class _ToastTheme extends InheritedWidget {
268306

269307
final bool dismissOtherOnShow;
270308

309+
final bool movingOnWindowChange;
310+
271311
@override
272312
bool updateShouldNotify(InheritedWidget oldWidget) => true;
273313

@@ -278,6 +318,7 @@ class _ToastTheme extends InheritedWidget {
278318
this.position,
279319
Widget child,
280320
this.dismissOtherOnShow,
321+
this.movingOnWindowChange,
281322
}) : super(child: child);
282323

283324
static _ToastTheme of(BuildContext context) =>

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.2
3+
version: 2.1.3
44
author: Caijinglong<[email protected]>
55
homepage: https://github.com/OpenFlutter/flutter_oktoast
66

0 commit comments

Comments
 (0)