Jelajahi Sumber

Add hidden and initialChild parameters to WebviewScaffold to allow the underlying Scaffold to be shown while waiting for page load

Jason Scott 7 tahun lalu
induk
melakukan
ed7eb797ba

+ 1 - 0
.gitignore

@@ -1,6 +1,7 @@
 .DS_Store
 .atom/
 .idea
+.vscode
 .packages
 .pub/
 build/

+ 1 - 1
example/ios/Runner.xcodeproj/project.pbxproj

@@ -242,7 +242,7 @@
 			);
 			inputPaths = (
 				"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
-				"${PODS_ROOT}/../../../../../development/flutter/bin/cache/artifacts/engine/ios/Flutter.framework",
+				"${PODS_ROOT}/../../../../../flutter/bin/cache/artifacts/engine/ios/Flutter.framework",
 				"${BUILT_PRODUCTS_DIR}/flutter_webview_plugin/flutter_webview_plugin.framework",
 			);
 			name = "[CP] Embed Pods Frameworks";

+ 8 - 0
example/lib/main.dart

@@ -30,6 +30,13 @@ class MyApp extends StatelessWidget {
               ),
               withZoom: true,
               withLocalStorage: true,
+              hidden: true,
+              initialChild: Container(
+                color: Colors.redAccent,
+                child: const Center(
+                  child: Text('Waiting.....'),
+                ),
+              ),
             )
       },
     );
@@ -121,6 +128,7 @@ class _MyHomePageState extends State<MyHomePage> {
 
     _onStateChanged =
         flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
+
       if (mounted) {
         setState(() {
           _history.add('onStateChanged: ${state.type} ${state.url}');

+ 16 - 3
lib/src/webview_scaffold.dart

@@ -20,6 +20,8 @@ class WebviewScaffold extends StatefulWidget {
   final bool withLocalStorage;
   final bool withLocalUrl;
   final bool scrollBar;
+  final bool hidden;
+  final Widget initialChild;
 
   final Map<String, String> headers;
 
@@ -39,7 +41,9 @@ class WebviewScaffold extends StatefulWidget {
       this.withZoom,
       this.withLocalStorage,
       this.withLocalUrl,
-      this.scrollBar})
+      this.scrollBar,
+      this.hidden : false,
+      this.initialChild})
       : super(key: key);
 
   @override
@@ -50,17 +54,25 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
   final webviewReference = new FlutterWebviewPlugin();
   Rect _rect;
   Timer _resizeTimer;
+  StreamSubscription<WebViewStateChanged> _onStateChanged;
 
   @override
   void initState() {
     super.initState();
     webviewReference.close();
+
+    _onStateChanged = webviewReference.onStateChanged.listen((WebViewStateChanged state) {
+      if (state.type == WebViewState.finishLoad) {
+        webviewReference.show();
+      }
+    });
   }
 
   @override
   void dispose() {
     super.dispose();
     webviewReference.close();
+    _onStateChanged.cancel();
     webviewReference.dispose();
   }
 
@@ -79,7 +91,8 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
           withZoom: widget.withZoom,
           withLocalStorage: widget.withLocalStorage,
           withLocalUrl: widget.withLocalUrl,
-          scrollBar: widget.scrollBar);
+          scrollBar: widget.scrollBar,
+          hidden: widget.hidden);
     } else {
       final rect = _buildRect(context);
       if (_rect != rect) {
@@ -95,7 +108,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
         appBar: widget.appBar,
         persistentFooterButtons: widget.persistentFooterButtons,
         bottomNavigationBar: widget.bottomNavigationBar,
-        body: const Center(child: const CircularProgressIndicator()));
+        body: widget.initialChild ?? const Center(child: const CircularProgressIndicator()));
   }
 
   Rect _buildRect(BuildContext context) {