Dialog not show when I click to text from WebView
I want when I click on a word in the webview, a dialog will open for that text, but nothing happens.
This is full code
child: InAppWebView(
initialUrlRequest: URLRequest(url: Uri.parse("https://your-website.com")),
onLoadResource: (controller, resource) async {
// Inject JavaScript code to handle click event on text
await controller.evaluateJavascript(source: '''
document.addEventListener('click', function(event) {
var target = event.target;
if (target.nodeType === 3) {
// Your custom logic when text is clicked
console.log('Clicked on text:', target.textContent);
// Send the clicked text to Flutter for further processing
if (window.flutter_inappwebview) {
window.flutter_inappwebview.callHandler('textClicked', target.textContent);
}
}
});
''');
},
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
// Add JavaScript handler to receive clicked text from webview
controller.addJavaScriptHandler(handlerName: 'textClicked', callback: (args) {
String clickedText = args[0];
// Display a dialog with the clicked text
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Clicked Text"),
content: Text("You clicked on the text: $clickedText"),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("OK"),
),
],
);
},
);
});
},
),