Flutter TextField的onEditingComplete与onSubmitted的区别

伯英武
2023-12-01

先看定义

final ValueChanged<String>? onSubmitted;
final VoidCallback? onEditingComplete;

onSubmitted带有当前字符串,onEditingComplete是个空回调

再看源码

TextField的build内使用的是EditableText组件.

可定位到 _finalizeEditing方法

@pragma('vm:notify-debugger-on-exception')
void _finalizeEditing(TextInputAction action, {required bool shouldUnfocus}) {
  // Take any actions necessary now that the user has completed editing.
  if (widget.onEditingComplete != null) {
    try {
      widget.onEditingComplete!();
    } catch (exception, stack) {
      FlutterError.reportError(FlutterErrorDetails(
        exception: exception,
        stack: stack,
        library: 'widgets',
        context: ErrorDescription('while calling onEditingComplete for $action'),
      ));
    }
  } else {
    // Default behavior if the developer did not provide an
    // onEditingComplete callback: Finalize editing and remove focus, or move
    // it to the next/previous field, depending on the action.
    widget.controller.clearComposing();
    if (shouldUnfocus) {
      switch (action) {
        case TextInputAction.none:
        case TextInputAction.unspecified:
        case TextInputAction.done:
        case TextInputAction.go:
        case TextInputAction.search:
        case TextInputAction.send:
        case TextInputAction.continueAction:
        case TextInputAction.join:
        case TextInputAction.route:
        case TextInputAction.emergencyCall:
        case TextInputAction.newline:
          widget.focusNode.unfocus();
          break;
        case TextInputAction.next:
          widget.focusNode.nextFocus();
          break;
        case TextInputAction.previous:
          widget.focusNode.previousFocus();
          break;
      }
    }
  }

  final ValueChanged<String>? onSubmitted = widget.onSubmitted;
  if (onSubmitted == null) {
    return;
  }

  // Invoke optional callback with the user's submitted content.
  try {
    onSubmitted(_value.text);
  } catch (exception, stack) {
    FlutterError.reportError(FlutterErrorDetails(
      exception: exception,
      stack: stack,
      library: 'widgets',
      context: ErrorDescription('while calling onSubmitted for $action'),
    ));
  }

  // If `shouldUnfocus` is true, the text field should no longer be focused
  // after the microtask queue is drained. But in case the developer cancelled
  // the focus change in the `onSubmitted` callback by focusing this input
  // field again, reset the soft keyboard.
  // See https://github.com/flutter/flutter/issues/84240.
  //
  // `_restartConnectionIfNeeded` creates a new TextInputConnection to replace
  // the current one. This on iOS switches to a new input view and on Android
  // restarts the input method, and in both cases the soft keyboard will be
  // reset.
  if (shouldUnfocus) {
    _scheduleRestartConnection();
  }
}

 onEditingCompleteonSubmitted都在同一个方法内调用.先调用onEditingComplete再调用onSubmitted.

onEditingComplete方法时,会忽略设置了的TextInputAction类型系统默认焦点操作.(释放焦点,跳转下一个/上一个焦点)

总结

一般情况下,只获取输入结果的实现onSubmitted

若要输入完成后,不让系统自动控制焦点跳转,实现onEditingComplete

 类似资料: