我用TextFormField收集用户输入,当用户按下FloatingActionButton表示他们已经完成时,我想解散屏幕上的键盘。

如何让键盘自动消失?

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.send),
        onPressed: () {
          setState(() {
            // send message
            // dismiss on screen keyboard here
            _controller.clear();
          });
        },
      ),
      body: new Container(
        alignment: FractionalOffset.center,
        padding: new EdgeInsets.all(20.0),
        child: new TextFormField(
          controller: _controller,
          decoration: new InputDecoration(labelText: 'Example Text'),
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

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

当前回答

你可以用这个。

FocusScope或(上下文)。requestFocus (FocusNode ());

你可以使用这个手势侦测器的onTap或者像这样的InkWell。

“GestureDetector ( onTap:() {' //这个焦点范围将关闭键盘 FocusScope.of(上下文).requestFocus (FocusNode ()); forgotPasswordAPI (emailController.text); }”、“

其他回答

在Flutter中,一切都是一个小部件,我决定包装FocusScope.of(context).unfocus();在一个简短的实用程序小部件中实现。

只需创建KeyboardHider小部件:

import 'package:flutter/widgets.dart';

/// A widget that upon tap attempts to hide the keyboard.
class KeyboardHider extends StatelessWidget {
  /// Creates a widget that on tap, hides the keyboard.
  const KeyboardHider({
    required this.child,
    Key? key,
  }) : super(key: key);

  /// The widget below this widget in the tree.
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => FocusScope.of(context).unfocus(),
      child: child,
    );
  }
}

现在,您可以用KeyboardHider小部件包装任何小部件(在使用好的IDE时非常方便),然后当您点击某个东西时,键盘将自动关闭。它与表单和其他可点击区域一起工作得很好。

class SimpleWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return KeyboardHider(
      /* Here comes a widget tree that eventually opens the keyboard,
       * but the widget that opened the keyboard doesn't necessarily
       * takes care of hiding it, so we wrap everything in a
       * KeyboardHider widget */
      child: Container(),
    );
  }
}

取消键盘(1.7.8+热修复。2和以上)只需调用下面的方法:

FocusScope.of(context).unfocus();

一旦FocusScope.of(context).unfocus()方法在解散键盘之前检查是否有焦点,就不需要检查它了。但如果你需要它,只需调用另一个上下文方法:FocusScope.of(context).hasPrimaryFocus

你可以用"GestureDetector"来包装你的小部件,然后将"FocusScope.of(context).unfocus()"赋值给它的onTap函数

GestureDetector(
 onTap: () => FocusScope.of(context).unfocus(),
 child: child,
);

在构建小部件中添加此代码

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

如果你使用CustomScrollView,只要放,

keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,