base.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import 'dart:async';
  2. import 'dart:ui';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. const _kChannel = 'flutter_webview_plugin';
  6. // TODO: more general state for iOS/android
  7. enum WebViewState { shouldStart, startLoad, finishLoad }
  8. // TODO: use an id by webview to be able to manage multiple webview
  9. /// Singleton Class that communicate with a Webview Instance
  10. /// Have to be instanciate after `runApp` called.
  11. class FlutterWebviewPlugin {
  12. final _channel = const MethodChannel(_kChannel);
  13. final _onDestroy = new StreamController<Null>.broadcast();
  14. final _onUrlChanged = new StreamController<String>.broadcast();
  15. final _onStateChanged = new StreamController<WebViewStateChanged>.broadcast();
  16. final _onError = new StreamController<String>.broadcast();
  17. static FlutterWebviewPlugin _instance;
  18. factory FlutterWebviewPlugin() => _instance ??= new FlutterWebviewPlugin._();
  19. FlutterWebviewPlugin._() {
  20. _channel.setMethodCallHandler(_handleMessages);
  21. }
  22. Future<Null> _handleMessages(MethodCall call) async {
  23. switch (call.method) {
  24. case "onDestroy":
  25. _onDestroy.add(null);
  26. break;
  27. case "onUrlChanged":
  28. _onUrlChanged.add(call.arguments["url"]);
  29. break;
  30. case "onState":
  31. _onStateChanged.add(
  32. new WebViewStateChanged.fromMap(
  33. new Map<String, dynamic>.from(call.arguments)),
  34. );
  35. break;
  36. case "onError":
  37. _onError.add(call.arguments);
  38. break;
  39. }
  40. }
  41. /// Listening the OnDestroy LifeCycle Event for Android
  42. Stream<Null> get onDestroy => _onDestroy.stream;
  43. /// Listening url changed
  44. Stream<String> get onUrlChanged => _onUrlChanged.stream;
  45. /// Listening the onState Event for iOS WebView and Android
  46. /// content is Map for type: {shouldStart(iOS)|startLoad|finishLoad}
  47. /// more detail than other events
  48. Stream<WebViewStateChanged> get onStateChanged => _onStateChanged.stream;
  49. /// Start the Webview with [url]
  50. /// - [withJavascript] enable Javascript or not for the Webview
  51. /// iOS WebView: Not implemented yet
  52. /// - [clearCache] clear the cache of the Webview
  53. /// - [clearCookies] clear all cookies of the Webview
  54. /// - [hidden] not show
  55. /// - [rect]: show in rect, fullscreen if null
  56. /// - [enableAppScheme]: false will enable all schemes, true only for httt/https/about
  57. /// android: Not implemented yet
  58. /// - [userAgent]: set the User-Agent of WebView
  59. /// - [withZoom]: enable zoom on webview
  60. /// - [withLocalStorage] enable localStorage API on Webview
  61. /// Currently Android only.
  62. /// It is always enabled in UIWebView of iOS and can not be disabled.
  63. /// - [withLocalUrl]: allow url as a local path
  64. /// Allow local files on iOs > 9.0
  65. /// - [scrollBar]: enable or disable scrollbar
  66. Future<Null> launch(String url,
  67. {bool withJavascript,
  68. bool clearCache,
  69. bool clearCookies,
  70. bool hidden,
  71. bool enableAppScheme,
  72. Rect rect,
  73. String userAgent,
  74. bool withZoom,
  75. bool withLocalStorage,
  76. bool withLocalUrl,
  77. bool scrollBar}) async {
  78. Map<String, dynamic> args = {
  79. "url": url,
  80. "withJavascript": withJavascript ?? true,
  81. "clearCache": clearCache ?? false,
  82. "hidden": hidden ?? false,
  83. "clearCookies": clearCookies ?? false,
  84. "enableAppScheme": enableAppScheme ?? true,
  85. "userAgent": userAgent,
  86. "withZoom": withZoom ?? false,
  87. "withLocalStorage": withLocalStorage ?? true,
  88. "withLocalUrl": withLocalUrl ?? false,
  89. "scrollBar": scrollBar ?? true
  90. };
  91. if (rect != null) {
  92. args["rect"] = {
  93. "left": rect.left,
  94. "top": rect.top,
  95. "width": rect.width,
  96. "height": rect.height
  97. };
  98. }
  99. await _channel.invokeMethod('launch', args);
  100. }
  101. /// Execute Javascript inside webview
  102. Future<String> evalJavascript(String code) async {
  103. final res = await _channel.invokeMethod('eval', {"code": code});
  104. return res;
  105. }
  106. /// Close the Webview
  107. /// Will trigger the [onDestroy] event
  108. Future close() => _channel.invokeMethod("close");
  109. /// Reloads the WebView.
  110. /// This is only available on Android for now.
  111. Future reload() => _channel.invokeMethod("reload");
  112. /// Navigates back on the Webview.
  113. /// This is only available on Android for now.
  114. Future goBack() => _channel.invokeMethod("back");
  115. /// Navigates forward on the Webview.
  116. /// This is only available on Android for now.
  117. Future goForward() => _channel.invokeMethod("forward");
  118. /// Close all Streams
  119. void dispose() {
  120. _onDestroy.close();
  121. _onUrlChanged.close();
  122. _onStateChanged.close();
  123. _onError.close();
  124. _instance = null;
  125. }
  126. Future<Map<String, dynamic>> getCookies() async {
  127. final cookiesString = await evalJavascript("document.cookie");
  128. final cookies = {};
  129. if (cookiesString?.isNotEmpty == true) {
  130. cookiesString.split(";").forEach((String cookie) {
  131. final splited = cookie.split("=");
  132. cookies[splited[0]] = splited[1];
  133. });
  134. }
  135. return cookies;
  136. }
  137. /// resize webview
  138. Future<Null> resize(Rect rect) async {
  139. final args = {};
  140. args["rect"] = {
  141. "left": rect.left,
  142. "top": rect.top,
  143. "width": rect.width,
  144. "height": rect.height
  145. };
  146. await _channel.invokeMethod('resize', args);
  147. }
  148. }
  149. class WebViewStateChanged {
  150. final WebViewState type;
  151. final String url;
  152. final int navigationType;
  153. WebViewStateChanged(this.type, this.url, this.navigationType);
  154. factory WebViewStateChanged.fromMap(Map<String, dynamic> map) {
  155. WebViewState t;
  156. switch (map["type"]) {
  157. case "shouldStart":
  158. t = WebViewState.shouldStart;
  159. break;
  160. case "startLoad":
  161. t = WebViewState.startLoad;
  162. break;
  163. case "finishLoad":
  164. t = WebViewState.finishLoad;
  165. break;
  166. }
  167. return new WebViewStateChanged(t, map["url"], map["navigationType"]);
  168. }
  169. }