Skip to content

Commit 68bed91

Browse files
committed
[webview_flutter] Implement platform interface for Javascript Dialog
1 parent baae12e commit 68bed91

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.9.0
2+
3+
* Adds support to show JavaScript dialog. See `PlatformWebViewController.setOnJavaScriptAlertDialog`, `PlatformWebViewController.setOnJavaScriptConfirmDialog` and `PlatformWebViewController.setOnJavaScriptTextInputDialog`.
4+
15
## 2.8.0
26

37
* Adds support to track scroll position changes. See `PlatformWebViewController.setOnScrollPositionChange`.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,36 @@ abstract class PlatformWebViewController extends PlatformInterface {
294294
throw UnimplementedError(
295295
'setOnScrollPositionChange is not implemented on the current platform');
296296
}
297+
298+
/// Sets a callback that notifies the host application that the web page
299+
/// wants to display a JavaScript alert() dialog.
300+
Future<void> setOnJavaScriptAlertDialog(
301+
Future<void> Function(JavaScriptAlertDialogRequest request)
302+
onJavaScriptAlertDialog) async {
303+
throw UnimplementedError(
304+
'setOnJavaScriptAlertDialog is not implemented on the current platform',
305+
);
306+
}
307+
308+
/// Sets a callback that notifies the host application that the web page
309+
/// wants to display a JavaScript confirm() dialog.
310+
Future<void> setOnJavaScriptConfirmDialog(
311+
Future<bool> Function(JavaScriptConfirmDialogRequest request)
312+
onJavaScriptConfirmDialog) async {
313+
throw UnimplementedError(
314+
'setOnJavaScriptConfirmDialog is not implemented on the current platform',
315+
);
316+
}
317+
318+
/// Sets a callback that notifies the host application that the web page
319+
/// wants to display a JavaScript prompt() dialog.
320+
Future<void> setOnJavaScriptTextInputDialog(
321+
Future<String> Function(JavaScriptTextInputDialogRequest request)
322+
onJavaScriptTextInputDialog) async {
323+
throw UnimplementedError(
324+
'setOnJavaScriptTextInputDialog is not implemented on the current platform',
325+
);
326+
}
297327
}
298328

299329
/// Describes the parameters necessary for registering a JavaScript channel.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import 'package:flutter/foundation.dart';
5+
6+
/// Defines the parameters that support setOnJavaScriptAlertDialog.
7+
@immutable
8+
class JavaScriptAlertDialogRequest {
9+
/// Creates a [JavaScriptAlertDialogRequest].
10+
const JavaScriptAlertDialogRequest({
11+
required this.message,
12+
required this.url,
13+
});
14+
15+
/// The Message to be displayed in the window.
16+
final String message;
17+
18+
/// The URL of the page requesting the dialog.
19+
final String url;
20+
}
21+
22+
/// Defines the parameters that support setOnJavaScriptConfirmDialog.
23+
@immutable
24+
class JavaScriptConfirmDialogRequest {
25+
/// Creates a [JavaScriptConfirmDialogRequest].
26+
const JavaScriptConfirmDialogRequest({
27+
required this.message,
28+
required this.url,
29+
});
30+
31+
/// The Message to be displayed in the window.
32+
final String message;
33+
34+
/// The URL of the page requesting the dialog.
35+
final String url;
36+
}
37+
38+
/// Defines the parameters that support JavaScriptTextInputDialogRequest.
39+
@immutable
40+
class JavaScriptTextInputDialogRequest {
41+
/// Creates a [JavaScriptAlertDialogRequest].
42+
const JavaScriptTextInputDialogRequest({
43+
required this.message,
44+
required this.url,
45+
required this.defaultText,
46+
});
47+
48+
/// The Message to be displayed in the window.
49+
final String message;
50+
51+
/// The URL of the page requesting the dialog.
52+
final String url;
53+
54+
/// The initial text to display in the text entry field.
55+
final String? defaultText;
56+
}

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
export 'http_auth_request.dart';
66
export 'http_response_error.dart';
77
export 'javascript_console_message.dart';
8+
export 'javascript_dialog_request.dart';
89
export 'javascript_log_level.dart';
910
export 'javascript_message.dart';
1011
export 'javascript_mode.dart';

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.8.0
7+
version: 2.9.0
88

99
environment:
1010
sdk: ">=3.0.0 <4.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,49 @@ void main() {
414414
throwsUnimplementedError,
415415
);
416416
});
417+
418+
test(
419+
'Default implementation of setOnJavaScriptAlertDialog should throw unimplemented error',
420+
() {
421+
final PlatformWebViewController controller =
422+
ExtendsPlatformWebViewController(
423+
const PlatformWebViewControllerCreationParams());
424+
425+
expect(
426+
() => controller.setOnJavaScriptAlertDialog((_) async {}),
427+
throwsUnimplementedError,
428+
);
429+
});
430+
431+
test(
432+
'Default implementation of setOnJavaScriptConfirmDialog should throw unimplemented error',
433+
() {
434+
final PlatformWebViewController controller =
435+
ExtendsPlatformWebViewController(
436+
const PlatformWebViewControllerCreationParams());
437+
438+
expect(
439+
() => controller.setOnJavaScriptConfirmDialog((_) async {
440+
return false;
441+
}),
442+
throwsUnimplementedError,
443+
);
444+
});
445+
446+
test(
447+
'Default implementation of setOnJavaScriptTextInputDialog should throw unimplemented error',
448+
() {
449+
final PlatformWebViewController controller =
450+
ExtendsPlatformWebViewController(
451+
const PlatformWebViewControllerCreationParams());
452+
453+
expect(
454+
() => controller.setOnJavaScriptTextInputDialog((_) async {
455+
return '';
456+
}),
457+
throwsUnimplementedError,
458+
);
459+
});
417460
}
418461

419462
class MockWebViewPlatformWithMixin extends MockWebViewPlatform

0 commit comments

Comments
 (0)