Flutter Webview 关闭键盘

问题描述

我通过 Flutter WebView 将用户发送到付款表单。最后,收到付款后,我的 Flutter 应用程序会收到一条消息,通知我购买已完成。此时显示的视图是订单摘要/收据。用户在填写付款表格后不会总是关闭键盘。我无法让键盘为他们自动关闭。

我只在 Android 模拟器和设备上进行了测试。键盘也不会关闭。

当收到订单完成消息时,我将 orderCompleted 变量的状态设置为 true 以触发重建,当 true 调用 FocusScope.of(context).unfocus(); 以尝试关闭键盘。这是行不通的。我还在 MobileOrderReceived 接收器函数内部调用了此代码,但它也无法在那里工作。

对我可以做些什么来解决这个问题有什么想法吗?

这是我现在的构建函数:

bool orderCompleted = false;

@override
Widget build(BuildContext context) {
    if (orderCompleted) {
        FocusScope.of(context).unfocus(); // hides the keyboard when the order is received as successful  
    }
    Set<JavascriptChannel> jsChannels = new Set<JavascriptChannel>();
    jsChannels.add(
        JavascriptChannel(
            name: "MobileOrderReceived",onMessageReceived: (JavascriptMessage receiver) async { 
                print("Order Completed => ${receiver.message}");
                // { ... } code for saving to order history
                setState(() {
                    orderCompleted = true;
                });
            }
        )
    );
    return Scaffold(
        appBar: appBar(context),body: mainContainer(
            WebView(
                initialUrl: widget.url,debuggingEnabled: true,javascriptMode: JavascriptMode.unrestricted,javascriptChannels: jsChannels,onWebResourceError: (error) {
                    print("==> Web Resource Error <==");
                    print(error);
                },)
        ),drawer: MainMenu.buildMenu(context),);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)