flutter_webview_plugin.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter/material.dart';
  4. const _kChannel = 'flutter_webview_plugin';
  5. const _kEvent = 'flutter_webview_plugin_event';
  6. /// Singleton Class that communicate with a fullscreen Webview Instance
  7. /// Have to be instanciate after `runApp` called.
  8. class FlutterWebviewPlugin {
  9. final MethodChannel _channel;
  10. final EventChannel _event;
  11. Stream<String> _stateChanged;
  12. Stream<String> get stateChanged {
  13. if (_stateChanged == null) {
  14. _stateChanged = _event.receiveBroadcastStream();
  15. }
  16. return _stateChanged;
  17. }
  18. final StreamController<Null> _onDestroy = new StreamController.broadcast();
  19. final StreamController<Null> _onBackPressed =
  20. new StreamController.broadcast();
  21. final StreamController<String> _onUrlChanged =
  22. new StreamController.broadcast();
  23. FlutterWebviewPlugin() :
  24. _channel = const MethodChannel(_kChannel),
  25. _event = const EventChannel(_kEvent) {
  26. _channel.setMethodCallHandler(_handleMessages);
  27. }
  28. Future<Null> _handleMessages(MethodCall call) async {
  29. print("_handleMessages $call");
  30. switch (call.method) {
  31. case "onDestroy":
  32. _onDestroy.add(null);
  33. break;
  34. case "onBackPressed":
  35. _onBackPressed.add(null);
  36. break;
  37. case "onUrlChanged":
  38. _onUrlChanged.add(call.arguments["url"]);
  39. break;
  40. }
  41. }
  42. //////////////////////
  43. /// Listening the OnDestroy LifeCycle Event for Android
  44. ///
  45. Stream<Null> get onDestroy => _onDestroy.stream;
  46. /// Listening the onBackPressed Event for Android
  47. ///
  48. Stream<Null> get onBackPressed => _onBackPressed.stream;
  49. /// Start the Webview with [url]
  50. /// - [withJavascript] enable Javascript or not for the Webview
  51. /// - [clearCache] clear the cache of the Webview
  52. /// - clearCookies] clear all cookies of the Webview
  53. Future<Null> launch(String url,
  54. {bool withJavascript: true,
  55. bool clearCache: false,
  56. bool clearCookies: false,
  57. bool hidden: false,
  58. bool fullScreen: true,
  59. Rect rect: null}) async {
  60. Map<String, dynamic> args = {
  61. "url": url,
  62. "withJavascript": withJavascript,
  63. "clearCache": clearCache,
  64. "hidden": hidden,
  65. "clearCookies": clearCookies,
  66. "fullScreen": fullScreen
  67. };
  68. if (!fullScreen) assert(rect != null);
  69. if (rect != null) {
  70. args["rect"] = {
  71. "left": rect.left,
  72. "right": rect.right,
  73. "width": rect.width,
  74. "height": rect.height
  75. };
  76. }
  77. await _channel.invokeMethod('launch', args);
  78. }
  79. /// Close the Webview
  80. /// Will trigger the [onDestroy] event
  81. Future<Null> close() => _channel.invokeMethod("close");
  82. /// Listening url changed
  83. ///
  84. Stream<String> get onUrlChanged => _onUrlChanged.stream;
  85. }