有没有一种方法可以在flutter中的WebView TextInput字段中自动对焦键盘?

发布于 2025-02-12 20:17:30 字数 1466 浏览 0 评论 0原文

我正在使用 inappwebview 在颤音应用程序中显示该网页。仅当我点击输入字段时,仅显示键盘。有没有办法在攻击输入字段之前可见键盘?键盘应在导航到此屏幕后立即出现。这是代码:

import 'package:flutter_inappwebview/flutter_inappwebview.dart';


class WebViewScreen extends StatefulWidget {
  const WebViewScreen({Key? key}) : super(key: key);

  @override
  State<WebViewScreen> createState() => _WebViewScreenState();
}

class _WebViewScreenState extends State<WebViewScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: InAppWebView(
            initialUrlRequest:
                URLRequest(url: Uri.parse('https://www.google.com')),
            onWebViewCreated: (InAppWebViewController controller) {
              print('onWebViewCreated');
            },
            onProgressChanged:
                (InAppWebViewController controller, int progress) {
              print('onProgressChanged: $progress');
            },
          ),
        ),
      ),
    );
  }
}

“在此处输入图像描述”

I am using InAppWebView to show the webpage in a flutter app. Here keyboard is shown only when I tap on the input field. Is there a way that the keyboard is visible before tapping on the input field? The keyboard should appear right after navigating to this screen. Here is the code:

import 'package:flutter_inappwebview/flutter_inappwebview.dart';


class WebViewScreen extends StatefulWidget {
  const WebViewScreen({Key? key}) : super(key: key);

  @override
  State<WebViewScreen> createState() => _WebViewScreenState();
}

class _WebViewScreenState extends State<WebViewScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: InAppWebView(
            initialUrlRequest:
                URLRequest(url: Uri.parse('https://www.google.com')),
            onWebViewCreated: (InAppWebViewController controller) {
              print('onWebViewCreated');
            },
            onProgressChanged:
                (InAppWebViewController controller, int progress) {
              print('onProgressChanged: $progress');
            },
          ),
        ),
      ),
    );
  }
}

enter image description here
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2025-02-19 20:17:31

使用

并使用 webview_flutter 插件,它可以使用:

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

  @override
  Widget build(BuildContext context) {
    return WebView(
      javascriptMode: JavascriptMode.unrestricted,
      initialUrl: "https://google.com",
      onWebViewCreated: (webViewController) {
        String jsStr = "window.addEventListener('load', (event) => {"
            "let i = document.getElementsByTagName('input');"
            "i[0].focus();"
            "console.log('done');"
            "});";
        webViewController.runJavascript(jsStr);
      },
    );
  }
}

Using addUserScript method gives method implementation error.

And using webview_flutter plugin, it works:

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

  @override
  Widget build(BuildContext context) {
    return WebView(
      javascriptMode: JavascriptMode.unrestricted,
      initialUrl: "https://google.com",
      onWebViewCreated: (webViewController) {
        String jsStr = "window.addEventListener('load', (event) => {"
            "let i = document.getElementsByTagName('input');"
            "i[0].focus();"
            "console.log('done');"
            "});";
        webViewController.runJavascript(jsStr);
      },
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文