简体   繁体   English

Flutter:如何在不丢失焦点的情况下关闭 TextField 的 onSubmitted() 键盘

[英]Flutter: How to close keyboard on onSubmitted() for TextField without lost focus

I have a TextField widget, After submit onSubmitted() I want to keep focusing on this TextField with readonly = false but in the same time I want to minimize the keyboard or close it, I can't use我有一个 TextField 小部件,在提交onSubmitted()之后,我想继续关注这个 TextField,使用readonly = false但同时我想最小化键盘或关闭它,我不能使用

 FocusScope.of(context).requestFocus(new FocusNode()); 

or或者

 FocusScope.of(context).unfocus();

or或者

  SystemChannels.textInput.invokeMethod('TextInput.hide');

When the onSubmit is called, keyboard will be disabled because readOnly is true but TextField would have the focus.当调用 onSubmit 时,键盘将被禁用,因为 readOnly 为真,但 TextField 将具有焦点。 But when you tap on the TextField, keyboard will appear because now readOnly is false and the it already had the focus但是当您点击 TextField 时,会出现键盘,因为现在 readOnly 是 false 并且它已经有了焦点

 class _MyAppState extends State<MyApp> {
      bool read = false;
    
      FocusNode focusNode = FocusNode();
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: Scaffold(
              body: Center(
                child: Container(
                  padding: EdgeInsets.all(10),
                  child: TextField(
                    onTap: () {
                      read = false;
                      setState(() {});
                    },
                    focusNode: focusNode,
                    readOnly: read,
                    cursorColor: Colors.blue,
                    onSubmitted: (val) async {
                      print(val);
                      read = true;
                      focusNode.requestFocus(new FocusNode());
                      setState(() {});
                    },
                  ),
                ),
              ),
            ));
      }
    }

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

相关问题 如何在 web TextField 的 Flutter 上触发 onSubmitted - How to trigger onSubmitted on a Flutter for web TextField 如何在 Flutter 中专注于 TextField 但不显示键盘? - How to keep focus on TextField but not display keyboard in Flutter? 使用Flutter,如何在iOS上触发TextField的onSubmitted()? - Using Flutter, how can I trigger onSubmitted() of TextField on iOS? 一种在不丢失文本字段焦点的情况下完全关闭键盘的方法 - A way to completely close the keyboard without losing textfield focus 如何发现 TextField 编辑被取消/键盘折叠/焦点丢失 - How to discover TextField editing being cancelled / keyboard collaped / focus lost 为什么导航器在颤动中不能在 TextField 的 onSubmitted 中工作? - Why navigator not working in onSubmitted of TextField in flutter? Flutter:onEdittingComplete 和 onSubmitted 之间的 TextField 差异 - Flutter: TextField difference between onEdittingComplete and onSubmitted 当焦点在 TextField 上并且在 Flutter 中打开键盘时,如何向上推送内容? - How to push content up when focus is on TextField and keyboard opens in Flutter? 如何在不显示键盘的情况下聚焦 TextField? - How can I focus a TextField without showing the keyboard? Flutter:当焦点文本字段时键盘不显示 - Flutter:Keyboard not show when focus TextField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM