1
1
library oktoast;
2
2
3
3
import 'dart:collection' ;
4
+ import 'dart:ui' as ui;
4
5
5
6
import 'package:flutter/material.dart' ;
6
7
import 'package:oktoast/src/toast_manager.dart' ;
@@ -25,13 +26,16 @@ class OKToast extends StatefulWidget {
25
26
26
27
final bool dismissOtherOnShow;
27
28
29
+ final bool movingOnWindowChange;
30
+
28
31
const OKToast ({
29
32
Key key,
30
33
@required this .child,
31
34
this .textStyle,
32
35
this .radius = 10.0 ,
33
36
this .position = ToastPosition .center,
34
37
this .dismissOtherOnShow = false ,
38
+ this .movingOnWindowChange = true ,
35
39
Color backgroundColor,
36
40
}) : this .backgroundColor = backgroundColor ?? const Color (0xDD000000 ),
37
41
super (key: key);
@@ -100,6 +104,7 @@ class _OKToastState extends State<OKToast> {
100
104
textStyle: textStyle,
101
105
position: widget.position,
102
106
dismissOtherOnShow: widget.dismissOtherOnShow,
107
+ movingOnWindowChange: widget.movingOnWindowChange,
103
108
);
104
109
}
105
110
}
@@ -164,11 +169,15 @@ ToastFuture showToastWidget(
164
169
context ?? = _contextMap.values.first;
165
170
OverlayEntry entry;
166
171
ToastFuture future;
172
+
173
+ var movingOnWindowChange =
174
+ _ToastTheme .of (context)? .movingOnWindowChange ?? false ;
167
175
entry = OverlayEntry (builder: (ctx) {
168
176
return IgnorePointer (
169
177
child: _ToastContainer (
170
178
duration: duration,
171
179
child: widget,
180
+ movingOnWindowChange: movingOnWindowChange,
172
181
),
173
182
);
174
183
});
@@ -194,15 +203,24 @@ ToastFuture showToastWidget(
194
203
class _ToastContainer extends StatefulWidget {
195
204
final Duration duration;
196
205
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);
198
213
199
214
@override
200
215
__ToastContainerState createState () => __ToastContainerState ();
201
216
}
202
217
203
- class __ToastContainerState extends State <_ToastContainer > {
218
+ class __ToastContainerState extends State <_ToastContainer >
219
+ with WidgetsBindingObserver {
204
220
double opacity = 0.0 ;
205
221
222
+ bool get movingOnWindowChange => widget.movingOnWindowChange;
223
+
206
224
@override
207
225
void initState () {
208
226
super .initState ();
@@ -223,21 +241,41 @@ class __ToastContainerState extends State<_ToastContainer> {
223
241
opacity = 0.0 ;
224
242
});
225
243
});
244
+
245
+ WidgetsBinding .instance.addObserver (this );
246
+ }
247
+
248
+ @override
249
+ void didChangeMetrics () {
250
+ super .didChangeMetrics ();
251
+ setState (() {});
226
252
}
227
253
228
254
@override
229
255
void dispose () {
256
+ WidgetsBinding .instance.removeObserver (this );
230
257
super .dispose ();
231
258
}
232
259
233
260
@override
234
261
Widget build (BuildContext context) {
235
- // print("mq bottom = ${MediaQuery.of(context).viewInsets.bottom}");
236
- return AnimatedOpacity (
262
+ Widget w = AnimatedOpacity (
237
263
duration: _opacityDuration,
238
264
child: widget.child,
239
265
opacity: opacity,
240
266
);
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
+ );
241
279
}
242
280
}
243
281
@@ -268,6 +306,8 @@ class _ToastTheme extends InheritedWidget {
268
306
269
307
final bool dismissOtherOnShow;
270
308
309
+ final bool movingOnWindowChange;
310
+
271
311
@override
272
312
bool updateShouldNotify (InheritedWidget oldWidget) => true ;
273
313
@@ -278,6 +318,7 @@ class _ToastTheme extends InheritedWidget {
278
318
this .position,
279
319
Widget child,
280
320
this .dismissOtherOnShow,
321
+ this .movingOnWindowChange,
281
322
}) : super (child: child);
282
323
283
324
static _ToastTheme of (BuildContext context) =>
0 commit comments