소스 검색

Merge pull request #160 from js1972/progress

Enable progress indicator for page load with WebviewScaffold
Rafal Wachol 7 년 전
부모
커밋
65799973c3
5개의 변경된 파일63개의 추가작업 그리고 8개의 파일을 삭제
  1. 1 0
      .gitignore
  2. 33 1
      README.md
  3. 1 1
      example/ios/Runner.xcodeproj/project.pbxproj
  4. 8 0
      example/lib/main.dart
  5. 20 6
      lib/src/webview_scaffold.dart

+ 1 - 0
.gitignore

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

+ 33 - 1
README.md

@@ -3,7 +3,7 @@
 
 
 # flutter_webview_plugin
 # flutter_webview_plugin
 
 
-Plugin that allow Flutter to communicate with a native WebView.
+Plugin that allows Flutter to communicate with a native WebView.
 
 
 ***Warning:***
 ***Warning:***
 The webview is not integrated in the widget tree, it is a native view on top of the flutter view.
 The webview is not integrated in the widget tree, it is a native view on top of the flutter view.
@@ -30,6 +30,38 @@ new MaterialApp(
     );
     );
 ```
 ```
 
 
+Optional parameters `hidden` and `initialChild` are available so that you can show something else while waiting for the page to load.
+If you set `hidden` to true it will show a default CircularProgressIndicator. If you additionally specify a Widget for initialChild
+you can have it display whatever you like till page-load.
+
+e.g. The following will show a read screen with the text 'waiting.....'.
+```dart
+return new MaterialApp(
+  title: 'Flutter WebView Demo',
+  theme: new ThemeData(
+    primarySwatch: Colors.blue,
+  ),
+  routes: {
+    '/': (_) => const MyHomePage(title: 'Flutter WebView Demo'),
+    '/widget': (_) => new WebviewScaffold(
+          url: selectedUrl,
+          appBar: new AppBar(
+            title: const Text('Widget webview'),
+          ),
+          withZoom: true,
+          withLocalStorage: true,
+          hidden: true,
+          initialChild: Container(
+            color: Colors.redAccent,
+            child: const Center(
+              child: Text('Waiting.....'),
+            ),
+          ),
+        )
+  },
+);
+```
+
 `FlutterWebviewPlugin` provide a singleton instance linked to one unique webview,
 `FlutterWebviewPlugin` provide a singleton instance linked to one unique webview,
 so you can take control of the webview from anywhere in the app
 so you can take control of the webview from anywhere in the app
 
 

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

@@ -242,7 +242,7 @@
 			);
 			);
 			inputPaths = (
 			inputPaths = (
 				"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
 				"${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",
 				"${BUILT_PRODUCTS_DIR}/flutter_webview_plugin/flutter_webview_plugin.framework",
 			);
 			);
 			name = "[CP] Embed Pods Frameworks";
 			name = "[CP] Embed Pods Frameworks";

+ 8 - 0
example/lib/main.dart

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

+ 20 - 6
lib/src/webview_scaffold.dart

@@ -21,6 +21,8 @@ class WebviewScaffold extends StatefulWidget {
   final bool withLocalStorage;
   final bool withLocalStorage;
   final bool withLocalUrl;
   final bool withLocalUrl;
   final bool scrollBar;
   final bool scrollBar;
+  final bool hidden;
+  final Widget initialChild;
 
 
   final Map<String, String> headers;
   final Map<String, String> headers;
 
 
@@ -40,7 +42,9 @@ class WebviewScaffold extends StatefulWidget {
       this.withZoom,
       this.withZoom,
       this.withLocalStorage,
       this.withLocalStorage,
       this.withLocalUrl,
       this.withLocalUrl,
-      this.scrollBar})
+      this.scrollBar,
+      this.hidden = false,
+      this.initialChild})
       : super(key: key);
       : super(key: key);
 
 
   @override
   @override
@@ -51,11 +55,20 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
   final webviewReference = new FlutterWebviewPlugin();
   final webviewReference = new FlutterWebviewPlugin();
   Rect _rect;
   Rect _rect;
   Timer _resizeTimer;
   Timer _resizeTimer;
+  StreamSubscription<WebViewStateChanged> _onStateChanged;
 
 
   @override
   @override
   void initState() {
   void initState() {
     super.initState();
     super.initState();
     webviewReference.close();
     webviewReference.close();
+
+    if (widget.hidden) {
+      _onStateChanged = webviewReference.onStateChanged.listen((WebViewStateChanged state) {
+        if (state.type == WebViewState.finishLoad) {
+          webviewReference.show();
+        }
+      });
+    }
   }
   }
 
 
   @override
   @override
@@ -63,6 +76,9 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
     super.dispose();
     super.dispose();
     _resizeTimer?.cancel();
     _resizeTimer?.cancel();
     webviewReference.close();
     webviewReference.close();
+    if (widget.hidden) {
+      _onStateChanged.cancel();
+    }
     webviewReference.dispose();
     webviewReference.dispose();
   }
   }
 
 
@@ -101,9 +117,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
             }
             }
           }
           }
         },
         },
-        child: const Center(
-          child: CircularProgressIndicator(),
-        ),
+        child: widget.initialChild ?? const Center(child: const CircularProgressIndicator()),
       ),
       ),
     );
     );
   }
   }
@@ -115,7 +129,7 @@ class _WebviewPlaceholder extends SingleChildRenderObjectWidget {
     @required this.onRectChanged,
     @required this.onRectChanged,
     Widget child,
     Widget child,
   }) : super(key: key, child: child);
   }) : super(key: key, child: child);
-
+  
   final ValueChanged<Rect> onRectChanged;
   final ValueChanged<Rect> onRectChanged;
 
 
   @override
   @override
@@ -124,7 +138,7 @@ class _WebviewPlaceholder extends SingleChildRenderObjectWidget {
       onRectChanged: onRectChanged,
       onRectChanged: onRectChanged,
     );
     );
   }
   }
-
+  
   @override
   @override
   void updateRenderObject(BuildContext context, _WebviewPlaceholderRender renderObject) {
   void updateRenderObject(BuildContext context, _WebviewPlaceholderRender renderObject) {
     renderObject..onRectChanged = onRectChanged;
     renderObject..onRectChanged = onRectChanged;