瀏覽代碼

use a singleton instead of static function

Hadrien Lejard 8 年之前
父節點
當前提交
1d54977566

+ 2 - 2
android/src/main/java/com/flutter_webview_plugin/WebviewActivity.java

@@ -70,13 +70,13 @@ public class WebviewActivity extends Activity {
 
     @Override
     protected void onDestroy() {
-        super.onDestroy();
         FlutterWebviewPlugin.channel.invokeMethod("onDestroy", null);
+        super.onDestroy();
     }
 
     @Override
     public void onBackPressed() {
-        super.onBackPressed();
         FlutterWebviewPlugin.channel.invokeMethod("onBackPressed", null);
+        super.onBackPressed();
     }
 }

+ 7 - 5
example/lib/main.dart

@@ -1,6 +1,5 @@
 import 'dart:async';
 import 'package:flutter/material.dart';
-import 'package:flutter/services.dart';
 import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
 
 void main() {
@@ -8,6 +7,7 @@ void main() {
 }
 
 class MyApp extends StatelessWidget {
+
   // This widget is the root of your application.
   @override
   Widget build(BuildContext context) {
@@ -49,6 +49,9 @@ class MyHomePage extends StatefulWidget {
 }
 
 class _MyHomePageState extends State<MyHomePage> {
+
+  final FlutterWebviewPlugin flutterWebviewPlugin = new FlutterWebviewPlugin();
+
   TextEditingController _ctrl =
       new TextEditingController(text: "https://flutter.io");
   StreamSubscription _onDestroy;
@@ -57,8 +60,7 @@ class _MyHomePageState extends State<MyHomePage> {
   @override
   initState() {
     super.initState();
-    FlutterWebviewPlugin.init();
-    _onDestroy = FlutterWebviewPlugin.onDestroy.listen((_) {
+    _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
       if (mounted) {
         _scaffoldKey.currentState
             .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
@@ -90,10 +92,10 @@ class _MyHomePageState extends State<MyHomePage> {
 
   void _onPressed() {
     try {
-      FlutterWebviewPlugin.launch(_ctrl.text);
+      flutterWebviewPlugin.launch(_ctrl.text);
 
       new Timer(const Duration(seconds: 3), () {
-        FlutterWebviewPlugin.close();
+        flutterWebviewPlugin.close();
       });
     } catch (e) {
       print(e);

+ 24 - 20
lib/flutter_webview_plugin.dart

@@ -4,21 +4,28 @@ import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 
 class FlutterWebviewPlugin {
-  static bool _init = false;
-  static StreamController<Null> _onDestroy = new StreamController.broadcast();
-  static Stream<Null> get onDestroy => _onDestroy.stream;
 
-  static StreamController<Null> _onBackPressed =
-      new StreamController.broadcast();
-  static Stream<Null> get onBackPressed => _onDestroy.stream;
+  static FlutterWebviewPlugin _instance;
+  FlutterWebviewPlugin._() {
+    _init();
+  }
+  factory FlutterWebviewPlugin() => _instance ??= new FlutterWebviewPlugin._();
+
+
+  StreamController<Null> _onDestroy = new StreamController.broadcast();
+  Stream<Null> get onDestroy => _onDestroy.stream;
 
-  static const MethodChannel _channel =
-      const MethodChannel('flutter_webview_plugin');
+  StreamController<Null> _onBackPressed =
+  new StreamController.broadcast();
 
-  static Future<Null> launch(String url,
-          {bool withJavascript: true,
-          bool clearCache: false,
-          bool clearCookies: false}) =>
+  Stream<Null> get onBackPressed => _onDestroy.stream;
+
+  final MethodChannel _channel = const MethodChannel('flutter_webview_plugin');
+
+  Future<Null> launch(String url,
+      {bool withJavascript: true,
+        bool clearCache: false,
+        bool clearCookies: false}) =>
       _channel.invokeMethod('launch', {
         "url": url,
         "withJavascript": withJavascript,
@@ -26,16 +33,13 @@ class FlutterWebviewPlugin {
         "clearCookies": clearCookies
       });
 
-  static Future<Null> close() => _channel.invokeMethod("close");
+  Future<Null> close() => _channel.invokeMethod("close");
 
-  static init() {
-    if (!_init) {
-      _init = true;
-      _channel.setMethodCallHandler(_handleMessages);
-    }
+  _init() {
+    _channel.setMethodCallHandler(_handleMessages);
   }
 
-  static Future<Null> _handleMessages(MethodCall call) async {
+  Future<Null> _handleMessages(MethodCall call) async {
     switch (call.method) {
       case "onDestroy":
         _onDestroy.add(null);
@@ -45,4 +49,4 @@ class FlutterWebviewPlugin {
         break;
     }
   }
-}
+}