flutter_webview_plugin.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. bool enableAppScheme: true,
  60. Rect rect: null}) async {
  61. Map<String, dynamic> args = {
  62. "url": url,
  63. "withJavascript": withJavascript,
  64. "clearCache": clearCache,
  65. "hidden": hidden,
  66. "clearCookies": clearCookies,
  67. "fullScreen": fullScreen,
  68. "enableAppScheme": enableAppScheme
  69. };
  70. if (!fullScreen) assert(rect != null);
  71. if (rect != null) {
  72. args["rect"] = {
  73. "left": rect.left,
  74. "right": rect.right,
  75. "width": rect.width,
  76. "height": rect.height
  77. };
  78. }
  79. await _channel.invokeMethod('launch', args);
  80. }
  81. Future<Null> evalJavascript(String code) {
  82. return _channel.invokeMethod('eval', {"code": code});
  83. }
  84. /// Close the Webview
  85. /// Will trigger the [onDestroy] event
  86. Future<Null> close() => _channel.invokeMethod("close");
  87. /// Listening url changed
  88. ///
  89. Stream<String> get onUrlChanged => _onUrlChanged.stream;
  90. }