0

Run this code sample on Android

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    return const MaterialApp(
      home: SafeArea(
        child: WebView(
          initialUrl: "https://www.w3schools.com/howto/howto_js_copy_clipboard.asp",
          javascriptMode: JavascriptMode.unrestricted,
        ),
      ),
    );
  }
}

Click the "Copy Text" button

Expected results: The text should be copied to the clipboard.

Actual results: Log message: [INFO:CONSOLE(0)] "Uncaught (in promise) NotAllowedError: Write permission denied."

FYI:

We have tried many many experiments from the internet and from AI for this problem and none of it has worked so far or involved editing java code, which does not seem to apply in flutter context. For example:

AI is currently also suggesting this, but does not even seem to compile

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:flutter/services.dart';

class WebViewExample extends StatefulWidget {
  @override
  _WebViewExampleState createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<WebViewExample> {
  WebViewController _controller;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter WebView example'),
      ),
      body: WebView(
        initialUrl: 'https://flutter.dev',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          _controller = webViewController;
          _controller.evaluateJavascript("window.addEventListener('flutterInAppWebViewPlatformReady', function(event) {window.flutter_inappwebview.callHandler('test', 'Text to copy').then(function(result) {console.log(result);});});");
        },
        javascriptChannels: <JavascriptChannel>[
          JavascriptChannel(
              name: 'Clipboard',
              onMessageReceived: (JavascriptMessage message) {
                Clipboard.setData(ClipboardData(text: message.message));
              }),
        ].toSet(),
      ),
    );
  }
}

Any help would be greatly appreciated. Thanks!

0 Answers0