Repository: OpenFlutter/flutter_oktoast

Issue:
position offset do not work when movingOnWindowChange is false



Discussion: ![image](https://user-images.githubusercontent.com/21259873/92456694-d8838180-f1f5-11ea-8280-c13b0929f949.png)
![image](https://user-images.githubusercontent.com/21259873/92456718-e0432600-f1f5-11ea-8895-72f0340e0467.png)
直接报错  根本不现实

════════ Exception caught by gesture ═══════════════════════════════════════════
Bad state: No element

不显示 崩

Are you still encounting this issue?
Hi @AlexV525  I'm still encountering this issue and it could because of these lines of code.

When `movingOnWindowChange` is `false`, it take the early return.

I'm very passionate to fix this issue.

```dart
    if (movingOnWindowChange != true) {
      return w;
    }

    final EdgeInsets? offsetPadding;
    if (offset > 0) {
      offsetPadding = EdgeInsets.only(top: offset);
    } else if (offset < 0) {
      offsetPadding = EdgeInsets.only(bottom: offset.abs());
    } else {
      offsetPadding = null;
    }
    final EdgeInsets edgeInsets = EdgeInsets.only(
      bottom: MediaQueryData.fromWindow(ui.window).viewInsets.bottom,
    );

    return AnimatedPadding(
      padding: (offsetPadding ?? EdgeInsets.zero) + edgeInsets,
      duration: animationDuration,
      child: w,
    );
  }
```

https://github.com/OpenFlutter/flutter_oktoast/blob/7475ad2cfecee7d795c6a899e635c17bf626876a/lib/src/widget/container.dart#LL85C1-L106C4
> I'm very passionate to fix this issue.

Contributions are welcomed!
> > I'm very passionate to fix this issue.
> 
> Contributions are welcomed!

The PR will be done later this day.
Thanks!

Fix patch:
diff --git a/example/lib/main.dart b/example/lib/main.dart
index 56b7603..e1fd5b0 100644
--- a/example/lib/main.dart
+++ b/example/lib/main.dart
@@ -139,7 +139,7 @@ class _MyHomePageState extends State<MyHomePage> {
             Center(
               child: Text(
                 '$_counter',
-                style: Theme.of(context).textTheme.headline4,
+                style: Theme.of(context).textTheme.headlineMedium,
               ),
             ),
             Padding(
diff --git a/lib/src/core/toast.dart b/lib/src/core/toast.dart
index fcb5237..480a1e0 100644
--- a/lib/src/core/toast.dart
+++ b/lib/src/core/toast.dart
@@ -2,7 +2,6 @@ library oktoast;
 
 import 'dart:async';
 import 'dart:collection';
-import 'dart:ui' as ui;
 
 import 'package:flutter/material.dart' hide Overlay, OverlayEntry, OverlayState;
 import 'package:flutter/scheduler.dart';
@@ -190,7 +189,8 @@ ToastFuture showToastWidget(
     }
   }
 
-  if (SchedulerBinding.instance.schedulerPhase != SchedulerPhase.persistentCallbacks) {
+  if (SchedulerBinding.instance.schedulerPhase !=
+      SchedulerPhase.persistentCallbacks) {
     insertOverlayEntry();
   } else {
     WidgetsBinding.instance.addPostFrameCallback((_) {
diff --git a/lib/src/widget/container.dart b/lib/src/widget/container.dart
index 6f7707a..d6d5ac7 100644
--- a/lib/src/widget/container.dart
+++ b/lib/src/widget/container.dart
@@ -82,24 +82,25 @@ class _ToastContainerState extends State<ToastContainer>
       child: widget.child,
     );
 
-    if (movingOnWindowChange != true) {
-      return w;
-    }
-
-    final EdgeInsets? offsetPadding;
+    final EdgeInsets offsetPadding;
     if (offset > 0) {
       offsetPadding = EdgeInsets.only(top: offset);
-    } else if (offset < 0) {
+    } else {
       offsetPadding = EdgeInsets.only(bottom: offset.abs());
+    }
+
+    final EdgeInsets windowInsets;
+    if (movingOnWindowChange) {
+      final currentView = View.of(context);
+      windowInsets = EdgeInsets.only(
+        bottom: MediaQueryData.fromView(currentView).viewInsets.bottom,
+      );
     } else {
-      offsetPadding = null;
+      windowInsets = EdgeInsets.zero;
     }
-    final EdgeInsets edgeInsets = EdgeInsets.only(
-      bottom: MediaQueryData.fromWindow(ui.window).viewInsets.bottom,
-    );
 
     return AnimatedPadding(
-      padding: (offsetPadding ?? EdgeInsets.zero) + edgeInsets,
+      padding: offsetPadding + windowInsets,
       duration: animationDuration,
       child: w,
     );
diff --git a/lib/src/widget/oktoast.dart b/lib/src/widget/oktoast.dart
index 1664729..b6cff7a 100644
--- a/lib/src/widget/oktoast.dart
+++ b/lib/src/widget/oktoast.dart
@@ -98,7 +98,7 @@ class _OKToastState extends State<OKToast> {
     final TextTheme defaultTextTheme = typography.white;
 
     final TextStyle textStyle = widget.textStyle ??
-        defaultTextTheme.bodyText2?.copyWith(
+        defaultTextTheme.bodyMedium?.copyWith(
           fontSize: 15.0,
           fontWeight: FontWeight.normal,
           color: Colors.white,
diff --git a/lib/src/widget/overlay.dart b/lib/src/widget/overlay.dart
index ec9f4c5..8a88805 100644
--- a/lib/src/widget/overlay.dart
+++ b/lib/src/widget/overlay.dart
@@ -579,7 +579,7 @@ class OverlayState extends State<Overlay> with TickerProviderStateMixin {
 ///
 /// The first [skipCount] children are considered "offstage".
 class Theatre extends MultiChildRenderObjectWidget {
-  Theatre({
+  const Theatre({
     super.key,
     this.skipCount = 0,
     this.clipBehavior = Clip.hardEdge,


Test patch:
diff --git a/test/toast_test.dart b/test/toast_test.dart
index 89d1039..ff59c23 100644
--- a/test/toast_test.dart
+++ b/test/toast_test.dart
@@ -4,6 +4,7 @@
 ///
 import 'package:flutter/material.dart' hide Overlay, OverlayEntry, OverlayState;
 import 'package:flutter_test/flutter_test.dart';
+import 'package:oktoast/src/core/position.dart';
 import 'package:oktoast/src/core/toast.dart';
 
 void main() {
@@ -36,6 +37,58 @@ void main() {
     await tester.pumpAndSettle(_defaultAnimDuration);
     expect(find.byType(ToastContainer), findsNothing);
   });
+
+  testWidgets('Offset test when movingOnWindowChange is true',
+      (WidgetTester tester) async {
+    const double verticalOffset = 400.0;
+
+    await _pumpWidget(
+      tester,
+      onPressed: () {
+        showToast(
+          'Test toast',
+          position: const ToastPosition(offset: verticalOffset),
+        );
+      },
+    );
+
+    await tester.tap(find.byKey(_wButtonKey));
+    await tester.pumpAndSettle();
+    final AnimatedPadding widget =
+        tester.firstWidget(find.byType(AnimatedPadding)) as AnimatedPadding;
+    final view = tester.viewOf(find.byType(AnimatedPadding));
+    final windowInsets = EdgeInsets.only(bottom: view.viewInsets.bottom);
+    expect(
+      const EdgeInsets.only(top: verticalOffset) + windowInsets,
+      widget.padding,
+    );
+    await tester.pumpAndSettle(_defaultDuration);
+    expect(find.byType(ToastContainer), findsNothing);
+  });
+
+  testWidgets('Offset test when movingOnWindowChange is false',
+      (WidgetTester tester) async {
+    const double verticalOffset = 400.0;
+
+    await _pumpWidget(
+      tester,
+      movingOnWindowChange: false,
+      onPressed: () {
+        showToast(
+          'Test toast',
+          position: const ToastPosition(offset: verticalOffset),
+        );
+      },
+    );
+
+    await tester.tap(find.byKey(_wButtonKey));
+    await tester.pumpAndSettle();
+    final AnimatedPadding widget =
+        tester.firstWidget(find.byType(AnimatedPadding)) as AnimatedPadding;
+    expect(const EdgeInsets.only(top: verticalOffset), widget.padding);
+    await tester.pumpAndSettle(_defaultDuration);
+    expect(find.byType(ToastContainer), findsNothing);
+  });
 }
 
 const Duration _defaultDuration = Duration(milliseconds: 2300);
@@ -46,10 +99,12 @@ final GlobalKey _wButtonKey = GlobalKey();
 
 Future<void> _pumpWidget(
   WidgetTester tester, {
+  bool movingOnWindowChange = true,
   VoidCallback? onPressed,
 }) async {
   await tester.pumpWidget(
     OKToast(
+      movingOnWindowChange: movingOnWindowChange,
       child: MaterialApp(
         home: Scaffold(
           appBar: AppBar(title: const Text(_wTitle)),