|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:math'; |
| 6 | + |
| 7 | +// TODO(a14n): remove this import once Flutter 3.1 or later reaches stable (including flutter/flutter#104231) |
| 8 | +// ignore: unnecessary_import |
| 9 | +import 'dart:typed_data'; |
| 10 | + |
| 11 | +import 'package:flutter/material.dart'; |
| 12 | +import 'package:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart'; |
| 13 | + |
| 14 | +/// Controls a WebView provided by the host platform. |
| 15 | +/// |
| 16 | +/// Pass this to a [WebViewWidget] to display the WebView. |
| 17 | +class WebViewController { |
| 18 | + /// Constructs a [WebViewController]. |
| 19 | + WebViewController() |
| 20 | + : this.fromPlatformCreationParams( |
| 21 | + const PlatformWebViewControllerCreationParams(), |
| 22 | + ); |
| 23 | + |
| 24 | + /// Constructs a [WebViewController] from creation params for a specific |
| 25 | + /// platform. |
| 26 | + WebViewController.fromPlatformCreationParams( |
| 27 | + PlatformWebViewControllerCreationParams params, |
| 28 | + ) : this.fromPlatform(PlatformWebViewController(params)); |
| 29 | + |
| 30 | + /// Constructs a [WebViewController] from a specific platform implementation. |
| 31 | + WebViewController.fromPlatform(this.platform); |
| 32 | + |
| 33 | + /// Implementation of [PlatformWebViewController] for the current platform. |
| 34 | + final PlatformWebViewController platform; |
| 35 | + |
| 36 | + /// Loads the file located on the specified [absoluteFilePath]. |
| 37 | + /// |
| 38 | + /// The [absoluteFilePath] parameter should contain the absolute path to the |
| 39 | + /// file as it is stored on the device. For example: |
| 40 | + /// `/Users/username/Documents/www/index.html`. |
| 41 | + /// |
| 42 | + /// Throws a `PlatformException` if the [absoluteFilePath] does not exist. |
| 43 | + Future<void> loadFile(String absoluteFilePath) { |
| 44 | + return platform.loadFile(absoluteFilePath); |
| 45 | + } |
| 46 | + |
| 47 | + /// Loads the Flutter asset specified in the pubspec.yaml file. |
| 48 | + /// |
| 49 | + /// Throws a `PlatformException` if [key] is not part of the specified assets |
| 50 | + /// in the pubspec.yaml file. |
| 51 | + Future<void> loadFlutterAsset(String key) { |
| 52 | + assert(key.isNotEmpty); |
| 53 | + return platform.loadFlutterAsset(key); |
| 54 | + } |
| 55 | + |
| 56 | + /// Loads the supplied HTML string. |
| 57 | + /// |
| 58 | + /// The [baseUrl] parameter is used when resolving relative URLs within the |
| 59 | + /// HTML string. |
| 60 | + Future<void> loadHtmlString(String html, {String? baseUrl}) { |
| 61 | + assert(html.isNotEmpty); |
| 62 | + return platform.loadHtmlString(html, baseUrl: baseUrl); |
| 63 | + } |
| 64 | + |
| 65 | + /// Makes a specific HTTP request ands loads the response in the webview. |
| 66 | + /// |
| 67 | + /// [method] must be one of the supported HTTP methods in [LoadRequestMethod]. |
| 68 | + /// |
| 69 | + /// If [headers] is not empty, its key-value pairs will be added as the |
| 70 | + /// headers for the request. |
| 71 | + /// |
| 72 | + /// If [body] is not null, it will be added as the body for the request. |
| 73 | + /// |
| 74 | + /// Throws an ArgumentError if [uri] has an empty scheme. |
| 75 | + Future<void> loadRequest( |
| 76 | + Uri uri, { |
| 77 | + LoadRequestMethod method = LoadRequestMethod.get, |
| 78 | + Map<String, String> headers = const <String, String>{}, |
| 79 | + Uint8List? body, |
| 80 | + }) { |
| 81 | + if (uri.scheme.isEmpty) { |
| 82 | + throw ArgumentError('Missing scheme in uri: $uri'); |
| 83 | + } |
| 84 | + return platform.loadRequest(LoadRequestParams( |
| 85 | + uri: uri, |
| 86 | + method: method, |
| 87 | + headers: headers, |
| 88 | + body: body, |
| 89 | + )); |
| 90 | + } |
| 91 | + |
| 92 | + /// Returns the current URL that the WebView is displaying. |
| 93 | + /// |
| 94 | + /// If no URL was ever loaded, returns `null`. |
| 95 | + Future<String?> currentUrl() { |
| 96 | + return platform.currentUrl(); |
| 97 | + } |
| 98 | + |
| 99 | + /// Checks whether there's a back history item. |
| 100 | + Future<bool> canGoBack() { |
| 101 | + return platform.canGoBack(); |
| 102 | + } |
| 103 | + |
| 104 | + /// Checks whether there's a forward history item. |
| 105 | + Future<bool> canGoForward() { |
| 106 | + return platform.canGoForward(); |
| 107 | + } |
| 108 | + |
| 109 | + /// Goes back in the history of this WebView. |
| 110 | + /// |
| 111 | + /// If there is no back history item this is a no-op. |
| 112 | + Future<void> goBack() { |
| 113 | + return platform.goBack(); |
| 114 | + } |
| 115 | + |
| 116 | + /// Goes forward in the history of this WebView. |
| 117 | + /// |
| 118 | + /// If there is no forward history item this is a no-op. |
| 119 | + Future<void> goForward() { |
| 120 | + return platform.goForward(); |
| 121 | + } |
| 122 | + |
| 123 | + /// Reloads the current URL. |
| 124 | + Future<void> reload() { |
| 125 | + return platform.reload(); |
| 126 | + } |
| 127 | + |
| 128 | + /// Clears all caches used by the WebView. |
| 129 | + /// |
| 130 | + /// The following caches are cleared: |
| 131 | + /// 1. Browser HTTP Cache. |
| 132 | + /// 2. [Cache API](https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/cache-api) |
| 133 | + /// caches. Service workers tend to use this cache. |
| 134 | + /// 3. Application cache. |
| 135 | + Future<void> clearCache() { |
| 136 | + return platform.clearCache(); |
| 137 | + } |
| 138 | + |
| 139 | + /// Clears the local storage used by the WebView. |
| 140 | + Future<void> clearLocalStorage() { |
| 141 | + return platform.clearLocalStorage(); |
| 142 | + } |
| 143 | + |
| 144 | + /// Runs the given JavaScript in the context of the current page. |
| 145 | + /// |
| 146 | + /// The Future completes with an error if a JavaScript error occurred. |
| 147 | + Future<void> runJavaScript(String javaScript) { |
| 148 | + return platform.runJavaScript(javaScript); |
| 149 | + } |
| 150 | + |
| 151 | + /// Runs the given JavaScript in the context of the current page, and returns |
| 152 | + /// the result. |
| 153 | + /// |
| 154 | + /// The Future completes with an error if a JavaScript error occurred, or if |
| 155 | + /// the type the given expression evaluates to is unsupported. Unsupported |
| 156 | + /// values include certain non-primitive types on iOS, as well as `undefined` |
| 157 | + /// or `null` on iOS 14+. |
| 158 | + Future<Object> runJavaScriptReturningResult(String javaScript) { |
| 159 | + return platform.runJavaScriptReturningResult(javaScript); |
| 160 | + } |
| 161 | + |
| 162 | + /// Adds a new JavaScript channel to the set of enabled channels. |
| 163 | + /// |
| 164 | + /// The JavaScript code can then call `postMessage` on that object to send a |
| 165 | + /// message that will be passed to [onMessageReceived]. |
| 166 | + /// |
| 167 | + /// For example, after adding the following JavaScript channel: |
| 168 | + /// |
| 169 | + /// ```dart |
| 170 | + /// final WebViewController controller = WebViewController(); |
| 171 | + /// controller.addJavaScriptChannel( |
| 172 | + /// name: 'Print', |
| 173 | + /// onMessageReceived: (JavascriptMessage message) { |
| 174 | + /// print(message.message); |
| 175 | + /// }, |
| 176 | + /// ); |
| 177 | + /// ``` |
| 178 | + /// |
| 179 | + /// JavaScript code can call: |
| 180 | + /// |
| 181 | + /// ```javascript |
| 182 | + /// Print.postMessage('Hello'); |
| 183 | + /// ``` |
| 184 | + /// |
| 185 | + /// to asynchronously invoke the message handler which will print the message |
| 186 | + /// to standard output. |
| 187 | + /// |
| 188 | + /// Adding a new JavaScript channel only takes affect after the next page is |
| 189 | + /// loaded. |
| 190 | + /// |
| 191 | + /// A channel [name] cannot be the same for multiple channels. |
| 192 | + Future<void> addJavaScriptChannel( |
| 193 | + String name, { |
| 194 | + required void Function(JavaScriptMessage) onMessageReceived, |
| 195 | + }) { |
| 196 | + assert(name.isNotEmpty); |
| 197 | + return platform.addJavaScriptChannel(JavaScriptChannelParams( |
| 198 | + name: name, |
| 199 | + onMessageReceived: onMessageReceived, |
| 200 | + )); |
| 201 | + } |
| 202 | + |
| 203 | + /// Removes the JavaScript channel with the matching name from the set of |
| 204 | + /// enabled channels. |
| 205 | + /// |
| 206 | + /// This disables the channel with the matching name if it was previously |
| 207 | + /// enabled through the [addJavaScriptChannel]. |
| 208 | + Future<void> removeJavaScriptChannel(String javaScriptChannelName) { |
| 209 | + return platform.removeJavaScriptChannel(javaScriptChannelName); |
| 210 | + } |
| 211 | + |
| 212 | + /// The title of the currently loaded page. |
| 213 | + Future<String?> getTitle() { |
| 214 | + return platform.getTitle(); |
| 215 | + } |
| 216 | + |
| 217 | + /// Sets the scrolled position of this view. |
| 218 | + /// |
| 219 | + /// The parameters `x` and `y` specify the position to scroll to in WebView |
| 220 | + /// pixels. |
| 221 | + Future<void> scrollTo(int x, int y) { |
| 222 | + return platform.scrollTo(x, y); |
| 223 | + } |
| 224 | + |
| 225 | + /// Moves the scrolled position of this view. |
| 226 | + /// |
| 227 | + /// The parameters `x` and `y` specify the amount of WebView pixels to scroll |
| 228 | + /// by. |
| 229 | + Future<void> scrollBy(int x, int y) { |
| 230 | + return platform.scrollBy(x, y); |
| 231 | + } |
| 232 | + |
| 233 | + /// Returns the current scroll position of this view. |
| 234 | + /// |
| 235 | + /// Scroll position is measured from the top left. |
| 236 | + Future<Offset> getScrollPosition() async { |
| 237 | + final Point<int> position = await platform.getScrollPosition(); |
| 238 | + return Offset(position.x.toDouble(), position.y.toDouble()); |
| 239 | + } |
| 240 | + |
| 241 | + /// Whether to support zooming using the on-screen zoom controls and gestures. |
| 242 | + Future<void> enableZoom(bool enabled) { |
| 243 | + return platform.enableZoom(enabled); |
| 244 | + } |
| 245 | + |
| 246 | + /// Sets the current background color of this view. |
| 247 | + Future<void> setBackgroundColor(Color color) { |
| 248 | + return platform.setBackgroundColor(color); |
| 249 | + } |
| 250 | + |
| 251 | + /// Sets the JavaScript execution mode to be used by the WebView. |
| 252 | + Future<void> setJavaScriptMode(JavaScriptMode javaScriptMode) { |
| 253 | + return platform.setJavaScriptMode(javaScriptMode); |
| 254 | + } |
| 255 | + |
| 256 | + /// Sets the value used for the HTTP `User-Agent:` request header. |
| 257 | + Future<void> setUserAgent(String? userAgent) { |
| 258 | + return platform.setUserAgent(userAgent); |
| 259 | + } |
| 260 | +} |
0 commit comments