@@ -108,6 +108,38 @@ const String kLogExamplePage = '''
108108</html>
109109''' ;
110110
111+ const String kAlertTestPage = '''
112+ <!DOCTYPE html>
113+ <html>
114+ <head>
115+ <script type = "text/javascript">
116+ function showAlert(text) {
117+ alert(text);
118+ }
119+
120+ function showConfirm(text) {
121+ var result = confirm(text);
122+ alert(result);
123+ }
124+
125+ function showPrompt(text, defaultText) {
126+ var inputString = prompt('Enter input', 'Default text');
127+ alert(inputString);
128+ }
129+ </script>
130+ </head>
131+
132+ <body>
133+ <p> Click the following button to see the effect </p>
134+ <form>
135+ <input type = "button" value = "Alert" onclick = "showAlert('Test Alert');" />
136+ <input type = "button" value = "Confirm" onclick = "showConfirm('Test Confirm');" />
137+ <input type = "button" value = "Prompt" onclick = "showPrompt('Test Prompt', 'Default Value');" />
138+ </form>
139+ </body>
140+ </html>
141+ ''' ;
142+
111143class WebViewExample extends StatefulWidget {
112144 const WebViewExample ({super .key, this .cookieManager});
113145
@@ -297,6 +329,7 @@ enum MenuOptions {
297329 setCookie,
298330 logExample,
299331 basicAuthentication,
332+ javaScriptAlert,
300333}
301334
302335class SampleMenu extends StatelessWidget {
@@ -348,6 +381,8 @@ class SampleMenu extends StatelessWidget {
348381 _onLogExample ();
349382 case MenuOptions .basicAuthentication:
350383 _promptForUrl (context);
384+ case MenuOptions .javaScriptAlert:
385+ _onJavaScriptAlertExample (context);
351386 }
352387 },
353388 itemBuilder: (BuildContext context) => < PopupMenuItem <MenuOptions >> [
@@ -412,6 +447,10 @@ class SampleMenu extends StatelessWidget {
412447 value: MenuOptions .basicAuthentication,
413448 child: Text ('Basic Authentication Example' ),
414449 ),
450+ const PopupMenuItem <MenuOptions >(
451+ value: MenuOptions .javaScriptAlert,
452+ child: Text ('JavaScript Alert Example' ),
453+ ),
415454 ],
416455 );
417456 }
@@ -536,6 +575,28 @@ class SampleMenu extends StatelessWidget {
536575 return webViewController.loadHtmlString (kTransparentBackgroundPage);
537576 }
538577
578+ Future <void > _onJavaScriptAlertExample (BuildContext context) {
579+ webViewController.setOnJavaScriptAlertDialog (
580+ (JavaScriptAlertDialogRequest request) async {
581+ await _showAlert (context, request.message);
582+ });
583+
584+ webViewController.setOnJavaScriptConfirmDialog (
585+ (JavaScriptConfirmDialogRequest request) async {
586+ final bool result = await _showConfirm (context, request.message);
587+ return result;
588+ });
589+
590+ webViewController.setOnJavaScriptTextInputDialog (
591+ (JavaScriptTextInputDialogRequest request) async {
592+ final String result =
593+ await _showTextInput (context, request.message, request.defaultText);
594+ return result;
595+ });
596+
597+ return webViewController.loadHtmlString (kAlertTestPage);
598+ }
599+
539600 Widget _getCookieList (String cookies) {
540601 if (cookies == '""' ) {
541602 return Container ();
@@ -605,6 +666,65 @@ class SampleMenu extends StatelessWidget {
605666 },
606667 );
607668 }
669+
670+ Future <void > _showAlert (BuildContext context, String message) async {
671+ return showDialog <void >(
672+ context: context,
673+ builder: (BuildContext ctx) {
674+ return AlertDialog (
675+ content: Text (message),
676+ actions: < Widget > [
677+ TextButton (
678+ onPressed: () {
679+ Navigator .of (ctx).pop ();
680+ },
681+ child: const Text ('OK' ))
682+ ],
683+ );
684+ });
685+ }
686+
687+ Future <bool > _showConfirm (BuildContext context, String message) async {
688+ return await showDialog <bool >(
689+ context: context,
690+ builder: (BuildContext ctx) {
691+ return AlertDialog (
692+ content: Text (message),
693+ actions: < Widget > [
694+ TextButton (
695+ onPressed: () {
696+ Navigator .of (ctx).pop (false );
697+ },
698+ child: const Text ('Cancel' )),
699+ TextButton (
700+ onPressed: () {
701+ Navigator .of (ctx).pop (true );
702+ },
703+ child: const Text ('OK' )),
704+ ],
705+ );
706+ }) ??
707+ false ;
708+ }
709+
710+ Future <String > _showTextInput (
711+ BuildContext context, String message, String ? defaultText) async {
712+ return await showDialog <String >(
713+ context: context,
714+ builder: (BuildContext ctx) {
715+ return AlertDialog (
716+ content: Text (message),
717+ actions: < Widget > [
718+ TextButton (
719+ onPressed: () {
720+ Navigator .of (ctx).pop ('Text test' );
721+ },
722+ child: const Text ('Enter' )),
723+ ],
724+ );
725+ }) ??
726+ '' ;
727+ }
608728}
609729
610730class NavigationControls extends StatelessWidget {
0 commit comments