diff --git a/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md b/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md index e708df3f64b7..4c41f54060f8 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.9.0 + +* Adds support to show JavaScript dialog. See `PlatformWebViewController.setOnJavaScriptAlertDialog`, `PlatformWebViewController.setOnJavaScriptConfirmDialog` and `PlatformWebViewController.setOnJavaScriptTextInputDialog`. + ## 2.8.0 * Adds support to track scroll position changes. See `PlatformWebViewController.setOnScrollPositionChange`. diff --git a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart index a6cc2c03f3a0..2aaabb519ccc 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart +++ b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart @@ -294,6 +294,36 @@ abstract class PlatformWebViewController extends PlatformInterface { throw UnimplementedError( 'setOnScrollPositionChange is not implemented on the current platform'); } + + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript alert() dialog. + Future setOnJavaScriptAlertDialog( + Future Function(JavaScriptAlertDialogRequest request) + onJavaScriptAlertDialog) async { + throw UnimplementedError( + 'setOnJavaScriptAlertDialog is not implemented on the current platform', + ); + } + + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript confirm() dialog. + Future setOnJavaScriptConfirmDialog( + Future Function(JavaScriptConfirmDialogRequest request) + onJavaScriptConfirmDialog) async { + throw UnimplementedError( + 'setOnJavaScriptConfirmDialog is not implemented on the current platform', + ); + } + + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript prompt() dialog. + Future setOnJavaScriptTextInputDialog( + Future Function(JavaScriptTextInputDialogRequest request) + onJavaScriptTextInputDialog) async { + throw UnimplementedError( + 'setOnJavaScriptTextInputDialog is not implemented on the current platform', + ); + } } /// Describes the parameters necessary for registering a JavaScript channel. diff --git a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/javascript_dialog_request.dart b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/javascript_dialog_request.dart new file mode 100644 index 000000000000..db367bd7e62a --- /dev/null +++ b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/javascript_dialog_request.dart @@ -0,0 +1,57 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/foundation.dart'; + +/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptAlertDialog`. +@immutable +class JavaScriptAlertDialogRequest { + /// Creates a [JavaScriptAlertDialogRequest]. + const JavaScriptAlertDialogRequest({ + required this.message, + required this.url, + }); + + /// The message to be displayed in the window. + final String message; + + /// The URL of the page requesting the dialog. + final String url; +} + +/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptConfirmDialog`. +@immutable +class JavaScriptConfirmDialogRequest { + /// Creates a [JavaScriptConfirmDialogRequest]. + const JavaScriptConfirmDialogRequest({ + required this.message, + required this.url, + }); + + /// The message to be displayed in the window. + final String message; + + /// The URL of the page requesting the dialog. + final String url; +} + +/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptTextInputDialog`. +@immutable +class JavaScriptTextInputDialogRequest { + /// Creates a [JavaScriptAlertDialogRequest]. + const JavaScriptTextInputDialogRequest({ + required this.message, + required this.url, + required this.defaultText, + }); + + /// The message to be displayed in the window. + final String message; + + /// The URL of the page requesting the dialog. + final String url; + + /// The initial text to display in the text entry field. + final String? defaultText; +} diff --git a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart index 84b20f3257e0..c89f32a238fb 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart +++ b/packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart @@ -5,6 +5,7 @@ export 'http_auth_request.dart'; export 'http_response_error.dart'; export 'javascript_console_message.dart'; +export 'javascript_dialog_request.dart'; export 'javascript_log_level.dart'; export 'javascript_message.dart'; export 'javascript_mode.dart'; diff --git a/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml b/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml index 774da3ce458c..1a900c2b4d42 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22 # NOTE: We strongly prefer non-breaking changes, even at the expense of a # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes -version: 2.8.0 +version: 2.9.0 environment: sdk: ">=3.0.0 <4.0.0" diff --git a/packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart b/packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart index ba83cc119922..db3cf1dd5d6f 100644 --- a/packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart +++ b/packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart @@ -414,6 +414,49 @@ void main() { throwsUnimplementedError, ); }); + + test( + 'Default implementation of setOnJavaScriptAlertDialog should throw unimplemented error', + () { + final PlatformWebViewController controller = + ExtendsPlatformWebViewController( + const PlatformWebViewControllerCreationParams()); + + expect( + () => controller.setOnJavaScriptAlertDialog((_) async {}), + throwsUnimplementedError, + ); + }); + + test( + 'Default implementation of setOnJavaScriptConfirmDialog should throw unimplemented error', + () { + final PlatformWebViewController controller = + ExtendsPlatformWebViewController( + const PlatformWebViewControllerCreationParams()); + + expect( + () => controller.setOnJavaScriptConfirmDialog((_) async { + return false; + }), + throwsUnimplementedError, + ); + }); + + test( + 'Default implementation of setOnJavaScriptTextInputDialog should throw unimplemented error', + () { + final PlatformWebViewController controller = + ExtendsPlatformWebViewController( + const PlatformWebViewControllerCreationParams()); + + expect( + () => controller.setOnJavaScriptTextInputDialog((_) async { + return ''; + }), + throwsUnimplementedError, + ); + }); } class MockWebViewPlatformWithMixin extends MockWebViewPlatform