瀏覽代碼

Merge pull request #255 from florisvdg/loading-progress

Support for webview’s estimated loading progress
Rafal Wachol 6 年之前
父節點
當前提交
f0ef28ddb2

+ 7 - 0
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -236,6 +236,13 @@ class WebviewManager {
                 activity.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
                 return true;
             }
+
+            @Override
+            public void onProgressChanged(WebView view, int progress) {
+                Map<String, Object> args = new HashMap<>();
+                args.put("progress", progress / 100.0);
+                FlutterWebviewPlugin.channel.invokeMethod("onProgressChanged", args);
+            }
         });
     }
 

+ 11 - 0
example/lib/main.dart

@@ -93,6 +93,8 @@ class _MyHomePageState extends State<MyHomePage> {
 
   StreamSubscription<WebViewHttpError> _onHttpError;
 
+  StreamSubscription<double> _onProgressChanged;
+
   StreamSubscription<double> _onScrollYChanged;
 
   StreamSubscription<double> _onScrollXChanged;
@@ -132,6 +134,14 @@ class _MyHomePageState extends State<MyHomePage> {
       }
     });
 
+    _onProgressChanged = flutterWebViewPlugin.onProgressChanged.listen((double progress) {
+      if (mounted) {
+        setState(() {
+          _history.add("onProgressChanged: $progress");
+        });
+      }
+    });
+    
     _onScrollYChanged = flutterWebViewPlugin.onScrollYChanged.listen((double y) {
       if (mounted) {
         setState(() {
@@ -172,6 +182,7 @@ class _MyHomePageState extends State<MyHomePage> {
     _onUrlChanged.cancel();
     _onStateChanged.cancel();
     _onHttpError.cancel();
+    _onProgressChanged.cancel();
     _onScrollXChanged.cancel();
     _onScrollYChanged.cancel();
 

+ 11 - 0
ios/Classes/FlutterWebviewPlugin.m

@@ -114,6 +114,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     self.webview.hidden = [hidden boolValue];
     self.webview.scrollView.showsHorizontalScrollIndicator = [scrollBar boolValue];
     self.webview.scrollView.showsVerticalScrollIndicator = [scrollBar boolValue];
+    
+    [self.webview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
 
     WKPreferences* preferences = [[self.webview configuration] preferences];
     if ([withJavascript boolValue]) {
@@ -191,11 +193,20 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     }
 }
 
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+    if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webview) {
+        [channel invokeMethod:@"onProgressChanged" arguments:@{@"progress": @(self.webview.estimatedProgress)}];
+    } else {
+        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
+    }
+}
+
 - (void)closeWebView {
     if (self.webview != nil) {
         [self.webview stopLoading];
         [self.webview removeFromSuperview];
         self.webview.navigationDelegate = nil;
+        [self.webview removeObserver:self forKeyPath:@"estimatedProgress"];
         self.webview = nil;
 
         // manually trigger onDestroy

+ 8 - 0
lib/src/base.dart

@@ -28,6 +28,7 @@ class FlutterWebviewPlugin {
   final _onStateChanged = StreamController<WebViewStateChanged>.broadcast();
   final _onScrollXChanged = StreamController<double>.broadcast();
   final _onScrollYChanged = StreamController<double>.broadcast();
+  final _onProgressChanged = new StreamController<double>.broadcast();
   final _onHttpError = StreamController<WebViewHttpError>.broadcast();
 
   Future<Null> _handleMessages(MethodCall call) async {
@@ -44,6 +45,9 @@ class FlutterWebviewPlugin {
       case 'onScrollYChanged':
         _onScrollYChanged.add(call.arguments['yDirection']);
         break;
+      case "onProgressChanged":
+        _onProgressChanged.add(call.arguments["progress"]);
+        break;
       case 'onState':
         _onStateChanged.add(
           WebViewStateChanged.fromMap(
@@ -68,6 +72,9 @@ class FlutterWebviewPlugin {
   /// more detail than other events
   Stream<WebViewStateChanged> get onStateChanged => _onStateChanged.stream;
 
+  /// Listening web view loading progress estimation, value between 0.0 and 1.0
+  Stream<double> get onProgressChanged => _onProgressChanged.stream;
+
   /// Listening web view y position scroll change
   Stream<double> get onScrollYChanged => _onScrollYChanged.stream;
 
@@ -194,6 +201,7 @@ class FlutterWebviewPlugin {
     _onDestroy.close();
     _onUrlChanged.close();
     _onStateChanged.close();
+    _onProgressChanged.close();
     _onScrollXChanged.close();
     _onScrollYChanged.close();
     _onHttpError.close();