Keep keyboard open when temporarily disabling text field

妖精的绣舞 提交于 2020-02-06 06:09:31

问题


How to keep the keyboard on screen while temporarily disabling a text field?

CupertinoTextField dismisses the keyboard when enabled=false or readOnly=true. I need to keep the keyboard on screen.


回答1:


I searched for about four hours and finally came up with a solution: Have the text field's onChanged function focus a hidden widget that accepts keyboard input. Once the processing is complete, focus the text field again.

Working example:

import 'package:flutter/cupertino.dart'
    show
        CupertinoApp,
        CupertinoButton,
        CupertinoPageScaffold,
        CupertinoTextField;
import 'package:flutter/widgets.dart'
    show
        BuildContext,
        Center,
        ClipRect,
        Column,
        Container,
        FocusNode,
        FocusScope,
        MainAxisSize,
        runApp,
        State,
        StatefulWidget,
        Text,
        TextAlign,
        TextEditingController,
        Widget;
import 'package:meta/meta.dart' show required;

class KeepKeyboardOnScreen extends StatefulWidget {
  final FocusNode focusNode;

  const KeepKeyboardOnScreen({@required this.focusNode});

  @override
  State createState() => KeepKeyboardOnScreenState();
}

class KeepKeyboardOnScreenState extends State<KeepKeyboardOnScreen> {
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TextEditingController();
  }

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

  @override
  Widget build(BuildContext context) => Container(
        height: 0,
        child: ClipRect(
          child: CupertinoTextField(
            controller: _controller,
            focusNode: widget.focusNode,
            onChanged: (_) => _controller.clear(),
          ),
        ),
      );
}

class Page extends StatefulWidget {
  @override
  State createState() => PageState();
}

class PageState extends State<Page> {
  TextEditingController _controller;
  FocusNode _focusNode;
  FocusNode _keepKeyboardOnScreenFocusNode;
  bool enabled = true;

  @override
  void initState() {
    super.initState();
    _controller = new TextEditingController();
    _focusNode = new FocusNode();
    _keepKeyboardOnScreenFocusNode = new FocusNode();
  }

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

  @override
  Widget build(BuildContext context) => CupertinoApp(
        home: CupertinoPageScaffold(
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                CupertinoTextField(
                  controller: _controller,
                  focusNode: _focusNode,
                  enabled: enabled,
                ),
                KeepKeyboardOnScreen(focusNode: _keepKeyboardOnScreenFocusNode),
                CupertinoButton(
                    onPressed: () {
                      setState(() {
                        enabled = true;
                      });
                      FocusScope.of(context).requestFocus(_focusNode);
                    },
                    child: Text("Enable", textAlign: TextAlign.center)),
                CupertinoButton(
                    onPressed: () {
                      setState(() {
                        enabled = false;
                      });
                      FocusScope.of(context)
                          .requestFocus(_keepKeyboardOnScreenFocusNode);
                    },
                    child: Text("Disable", textAlign: TextAlign.center)),
              ],
            ),
          ),
        ),
      );
}

void main() async {
  runApp(Page());
}

See also: Flutter Issue #45076 Add high-level documentation and examples on managing keyboard focus.



来源:https://stackoverflow.com/questions/58906111/keep-keyboard-open-when-temporarily-disabling-text-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!