TextField is used in Flutter

The purpose is to find the callback of the delete key on the keyboard. Hence the following:

The TextField trace finds a _requestKeyboard() function in the return of the control:

// source code implementation void_requestKeyboard() { _editableTextKey.currentState? .requestKeyboard(); } // Then call requestKeyboard() voidrequestKeyboard() {
    if (_hasFocus)
      _openInputConnection();
    elseFocusscope.of (context).requestFocus(widget.focusNode); }Copy the code

In requestKeyboard(), the keyboard pops up if _hasFocus == true.

// _openInputConnection () opens the soft keyboard TextInputConnection. bool get _hasInputConnection => _textInputConnection ! = null && _textInputConnection.attached; void_openInputConnection() {
    if(! _hasInputConnection) { final TextEditingValuelocalValue = _value;
      _lastKnownRemoteTextEditingValue = localValue; _textInputConnection = TextInput.attach(this, TextInputConfiguration( inputType: widget.keyboardType, obscureText: widget.obscureText, autocorrect: widget.autocorrect, inputAction: widget.textInputAction ?? (widget.keyboardType == TextInputType.multiline ? TextInputAction.newline : TextInputAction.done ), textCapitalization: widget.textCapitalization, ) ).. setEditingState(localValue);
    }
    _textInputConnection.show();
  }
Copy the code
Parse 1 uses TextInputConnection and its class needs to implement the TextInputClient method and override it:
Override void performAction(TextInputAction) {// TODO: Implement performAction // Click the clickback button on the keyboard position shown in Figure 1print("  performAction  action $action "); } @override void updateEditingValue(TextEditingValue value) { // TODO: Implement updateEditingValue // Value Text input callback example 1print("updateEditingValue  value  $value  ");
  }
Copy the code

Figure 1

TextEditingValue(text: ┤ Line ├, Selection: TextSelection(baseOffset: 2, extentOffset: 2, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1))

Resolution 2 The TextInputConnection is closed
// Check whether _hasInputConnection is empty or whether void is already bound_closeInputConnectionIfNeeded() {
    if(_hasInputConnection) { _textInputConnection.close(); _textInputConnection = null; _lastKnownRemoteTextEditingValue = null; }}Copy the code
TextInputConnection: TextEditingValue
// value.tojson (), where the value returns json voidsetEditingState(TextEditingValue value) {
    assert(attached);
    SystemChannels.textInput.invokeMethod(
      'TextInput.setEditingState', value.toJSON(), ); } // Update data remotely void when needed_updateRemoteEditingValueIfNeeded() {
    if(! _hasInputConnection)return;
    final TextEditingValue localValue = _value;
    if (localValue == _lastKnownRemoteTextEditingValue)
      return;
    _lastKnownRemoteTextEditingValue = localValue;
    _textInputConnection.setEditingState(localValue);
  }
Copy the code

Texteditingvalue.text retrieves the entered value

Parse setEditingState in 4 TextInputConnection
/// Requests that the text input control change its internal state to match the given state.
  void setEditingState(TextEditingValue value) {
    assert(attached);
    print(" setEditingState ${value.text} ");
    SystemChannels.textInput.invokeMethod(
      'TextInput.setEditingState',
      value.toJSON(),
    );
  }
Copy the code

This function is called when the control gets focus.

Future<dynamic> _handleTextInputInvocation(MethodCall methodCall) async {
    print(" _handleTextInputInvocation  MethodCall  ${methodCall.method} ");
    if (_currentConnection == null)
      return;
    final String method = methodCall.method;
    final List<dynamic> args = methodCall.arguments;
    final int client = args[0];
    // The incoming message was for a different client.
    if(client ! = _currentConnection._id)return;
    switch (method) {
      case 'TextInputClient.updateEditingState':
        _currentConnection._client.updateEditingValue(TextEditingValue.fromJSON(args[1]));
        break;
      case 'TextInputClient.performAction':
        _currentConnection._client.performAction(_toTextInputAction(args[1]));
        break; default: throw MissingPluginException(); }}Copy the code

The above function to print it is concluded that there are only two state TextInputClient. UpdateEditingState and TextInputClient performAction.

TextInputClient updateEditingState is get focus and content changes are to be invoked

TextInputClient performAction this is by click the button as shown in figure 1 position on the state to trigger

This tells us that there is no listening or callback for the delete key.

The keyboard class in the package import ‘package: flutter/services. The dart’; I need to import this in order to use this class

Actually, I was trying to find the delete key. At least it wasn’t in vain!