flutter_webview_plugin.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. class FlutterWebviewPlugin {
  4. static FlutterWebviewPlugin _instance;
  5. FlutterWebviewPlugin._() {
  6. _init();
  7. }
  8. factory FlutterWebviewPlugin() => _instance ??= new FlutterWebviewPlugin._();
  9. StreamController<Null> _onDestroy = new StreamController.broadcast();
  10. Stream<Null> get onDestroy => _onDestroy.stream;
  11. StreamController<Null> _onBackPressed = new StreamController.broadcast();
  12. Stream<Null> get onBackPressed => _onBackPressed.stream;
  13. final MethodChannel _channel = const MethodChannel('flutter_webview_plugin');
  14. Future<Null> launch(String url,
  15. {bool withJavascript: true,
  16. bool clearCache: false,
  17. bool clearCookies: false}) =>
  18. _channel.invokeMethod('launch', {
  19. "url": url,
  20. "withJavascript": withJavascript,
  21. "clearCache": clearCache,
  22. "clearCookies": clearCookies
  23. });
  24. Future<Null> close() => _channel.invokeMethod("close");
  25. _init() {
  26. _channel.setMethodCallHandler(_handleMessages);
  27. }
  28. Future<Null> _handleMessages(MethodCall call) async {
  29. switch (call.method) {
  30. case "onDestroy":
  31. _onDestroy.add(null);
  32. break;
  33. case "onBackPressed":
  34. _onBackPressed.add(null);
  35. break;
  36. }
  37. }
  38. }