Skip to content

[webview_flutter_wkwebview] Add webkit interface for showing javascript alert message #4555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ const String kTransparentBackgroundPage = '''
</html>
''';

const String kAlertTestPage = '''
<html>
<head>
<script type = "text/javascript">
function showAlert() {
alert ("This is an alert dialog box");
}
</script>
</head>

<body>
<p> Click the following button to see the effect </p>
<form>
<input type = "button" value = "Click me" onclick = "showAlert();" />
</form>
</body>
</html>
''';

class WebViewExample extends StatefulWidget {
const WebViewExample({super.key, this.cookieManager});

Expand Down Expand Up @@ -201,6 +220,7 @@ enum MenuOptions {
loadHtmlString,
transparentBackground,
setCookie,
javaScriptAlert
}

class SampleMenu extends StatelessWidget {
Expand Down Expand Up @@ -261,6 +281,9 @@ class SampleMenu extends StatelessWidget {
case MenuOptions.setCookie:
_onSetCookie();
break;
case MenuOptions.javaScriptAlert:
_onJavaScriptAlertExample(context);
break;
}
},
itemBuilder: (BuildContext context) => <PopupMenuItem<MenuOptions>>[
Expand Down Expand Up @@ -317,6 +340,10 @@ class SampleMenu extends StatelessWidget {
value: MenuOptions.transparentBackground,
child: Text('Transparent background example'),
),
const PopupMenuItem<MenuOptions>(
value: MenuOptions.javaScriptAlert,
child: Text('JavaScript Alert Example'),
),
],
);
}
Expand Down Expand Up @@ -441,6 +468,10 @@ class SampleMenu extends StatelessWidget {
return webViewController.loadHtmlString(kTransparentBackgroundPage);
}

Future<void> _onJavaScriptAlertExample(BuildContext context) {
return webViewController.loadHtmlString(kAlertTestPage);
}

Widget _getCookieList(String cookies) {
if (cookies == '""') {
return Container();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.7.3

* Add webkit interface for showing javascript alert message


## 3.7.2

* Fixes bug where `PlatformWebViewWidget` doesn't rebuild when the controller changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ const String kTransparentBackgroundPage = '''
</html>
''';

const String kAlertTestPage = '''
<html>
<head>
<script type = "text/javascript">
function showAlert() {
alert ("This is an alert dialog box");
}
</script>
</head>

<body>
<p> Click the following button to see the effect </p>
<form>
<input type = "button" value = "Click me" onclick = "showAlert();" />
</form>
</body>
</html>
''';

class WebViewExample extends StatefulWidget {
const WebViewExample({super.key, this.cookieManager});

Expand Down Expand Up @@ -202,6 +221,7 @@ enum MenuOptions {
loadHtmlString,
transparentBackground,
setCookie,
javaScriptAlert,
}

class SampleMenu extends StatelessWidget {
Expand Down Expand Up @@ -262,6 +282,9 @@ class SampleMenu extends StatelessWidget {
case MenuOptions.setCookie:
_onSetCookie();
break;
case MenuOptions.javaScriptAlert:
_onJavaScriptAlertExample(context);
break;
}
},
itemBuilder: (BuildContext context) => <PopupMenuItem<MenuOptions>>[
Expand Down Expand Up @@ -318,6 +341,10 @@ class SampleMenu extends StatelessWidget {
value: MenuOptions.transparentBackground,
child: Text('Transparent background example'),
),
const PopupMenuItem<MenuOptions>(
value: MenuOptions.javaScriptAlert,
child: Text('JavaScript Alert Example'),
),
],
);
}
Expand Down Expand Up @@ -433,7 +460,7 @@ class SampleMenu extends StatelessWidget {
Future<void> _onLoadFlutterAssetExample() {
return webViewController.loadFlutterAsset('assets/www/index.html');
}

Future<void> _onLoadHtmlStringExample() {
return webViewController.loadHtmlString(kLocalExamplePage);
}
Expand All @@ -442,6 +469,17 @@ class SampleMenu extends StatelessWidget {
return webViewController.loadHtmlString(kTransparentBackgroundPage);
}

Future<void> _onJavaScriptAlertExample(BuildContext context) {
if(webViewController is WebKitWebViewController) {
final webKitWebViewController = webViewController as WebKitWebViewController;
webKitWebViewController.setJavaScriptAlertPanelCallback((message) async {
return _showAlert(context, message);
});
}

return webViewController.loadHtmlString(kAlertTestPage);
}

Widget _getCookieList(String cookies) {
if (cookies == '""') {
return Container();
Expand All @@ -466,6 +504,18 @@ class SampleMenu extends StatelessWidget {

return indexFile.path;
}

Future<void> _showAlert(BuildContext context, String message) async {
return showDialog(context: context, builder: (BuildContext ctx) {
return AlertDialog(content: Text(message),
actions: [
TextButton(onPressed: () {
Navigator.of(ctx).pop();
}, child: const Text('OK'))
],
);
});
}
}

class NavigationControls extends StatelessWidget {
Expand Down
Loading