简体   繁体   English

Flutter Webview 关闭键盘

[英]Flutter Webview Dismiss Keyboard

I am sending users to a payment form via a Flutter WebView .我通过Flutter WebView将用户发送到付款表单。 At the end, after payment is received, my Flutter app is receiving a message that informs me that the purchase is completed.最后,收到付款后,我的 Flutter 应用程序收到一条消息,通知我购买已完成。 The view that is displayed at this point is the order summary/receipt.此时显示的视图是订单摘要/收据。 The user won't always dismiss the keyboard after filling out the payment form.用户在填写付款表格后不会总是关闭键盘。 I am having troubles getting the keyboard to dismiss automatically for them.我无法让键盘自动为他们关闭。

I have only tested on Android emulator and device.我只在 Android 仿真器和设备上进行了测试。 The keyboard does not dismiss for either.键盘也不会关闭。

When the order completed message is received, I am setting the state of the orderCompleted variable to true which triggers a rebuild and when true it calls FocusScope.of(context).unfocus();当收到订单完成消息时,我将orderCompleted变量的 state 设置为true ,这会触发重建,如果为true ,它会调用FocusScope.of(context).unfocus(); to attempt to dismiss the keyboard.试图关闭键盘。 This is not working.这是行不通的。 I've also called this code inside of the MobileOrderReceived receiver function and it doesn't work there either.我还在MobileOrderReceived接收器 function 中调用了此代码,但它也不起作用。

Any thoughts on what I could do to solve the issue?关于我可以做些什么来解决这个问题的任何想法?

Here is my build function as it is right now:这是我现在的构建 function:

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),
    );
}
FocusScope.of(context).requestFocus(FocusNode());

This line is working for me to close the keyboard programetically.这条线正在为我工作以编程方式关闭键盘。

One option would be to useFocusManager , more specifically:一种选择是使用FocusManager ,更具体地说:

FocusManager.instance.primaryFocus.unfocus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM