最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to disable keyboard hiding in flutter when I click submit? - Stack Overflow

programmeradmin1浏览0评论

When I click on the submit keyboard button, the keyboard is hidden. How to not hide the keyboard, so that it remains even after clicking on submit? (the picture shows how it happens by default)

I tried using requestFocus() but seems it doesn't solve the issue.

class KeyboardDismissDemo extends StatefulWidget {
  const KeyboardDismissDemo({super.key});

  @override
  _KeyboardDismissDemo createState() => _KeyboardDismissDemo();
}

class _KeyboardDismissDemo extends State<KeyboardDismissDemo> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _focusNode = FocusNode();

  @override
  void dispose() {
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    TextEditingController _controller = TextEditingController();
    final FocusNode _focusNode = FocusNode();

    return Scaffold(
      appBar: AppBar(
        title: Text('Keyboard Dismiss Demo'),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Spacer(),
              TextField(
                controller: _controller,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  labelText: 'Input box for keyboard test',
                ),
                autofocus: true,
                onSubmitted: (value) {
                  // Handle the submit action without hiding the keyboard
                  print('Submitted: $value');
                  // Refocus the text field to keep the keyboard open
                  _focusNode.requestFocus();
                },
              ),
              Spacer(),
            ],
          ),
        ),
      ),
    );
  }
}

When I click on the submit keyboard button, the keyboard is hidden. How to not hide the keyboard, so that it remains even after clicking on submit? (the picture shows how it happens by default)

I tried using requestFocus() but seems it doesn't solve the issue.

class KeyboardDismissDemo extends StatefulWidget {
  const KeyboardDismissDemo({super.key});

  @override
  _KeyboardDismissDemo createState() => _KeyboardDismissDemo();
}

class _KeyboardDismissDemo extends State<KeyboardDismissDemo> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _focusNode = FocusNode();

  @override
  void dispose() {
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    TextEditingController _controller = TextEditingController();
    final FocusNode _focusNode = FocusNode();

    return Scaffold(
      appBar: AppBar(
        title: Text('Keyboard Dismiss Demo'),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Spacer(),
              TextField(
                controller: _controller,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  labelText: 'Input box for keyboard test',
                ),
                autofocus: true,
                onSubmitted: (value) {
                  // Handle the submit action without hiding the keyboard
                  print('Submitted: $value');
                  // Refocus the text field to keep the keyboard open
                  _focusNode.requestFocus();
                },
              ),
              Spacer(),
            ],
          ),
        ),
      ),
    );
  }
}

Share Improve this question edited Feb 10 at 10:40 rozerro asked Feb 10 at 10:24 rozerrorozerro 7,21614 gold badges64 silver badges117 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

To prevent the keyboard from closing when you press the submit button, you can create a code structure like the one below.

First of all, define a FocusNode at just above build Widget for manage better focus scoping on TextField and request focus at onSubmitted function for keep the keyboard on the screen.

The code example for your situation;

final FocusNode _textFieldFocus = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Spacer(),
              TextField(
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  labelText: 'Input box for keyboard test',
                ),
                autofocus: true,
                focusNode: _textFieldFocus, //dont fet this parameter
                onSubmitted: (value) {
                  // Request focus again to keep the keyboard open
                  FocusScope.of(context).requestFocus(_textFieldFocus);
                },
              ),
              Spacer(),
            ],
          ),
        ),
      ),
    );
  }

To prevent the keyboard from hiding in Flutter when clicking submit, you can use a FocusNode. Keep the focus on the text field by calling FocusScope.of(context).requestFocus(focusNode) inside the submit function. This prevents the keyboard from dismissing when the submit button is pressed.

发布评论

评论列表(0)

  1. 暂无评论