Skip to content

WidgetTester.sendKeyEvent(LogicalKeyboardKey.enter) does not trigger TextField.onSubmitted #116752

@Jordan-Nelson

Description

@Jordan-Nelson

Steps to Reproduce

  1. Execute the first test below under code sample, and observe that it fails
  2. Execute the second test below under code sample, and observe that it passes

Expected results:
WidgetTester.sendKeyEvent(LogicalKeyboardKey.enter) would call the onSubmit callback in a TextField

Actual results:
WidgetTester.sendKeyEvent(LogicalKeyboardKey.enter) can interact with buttons, but not form fields.

Code sample
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Can submit form field via LogicalKeyboardKey', (tester) async {
    bool wasSubmitted = false;
    final app = MaterialApp(
      home: Material(
        child: Center(
          child: TextField(
            onSubmitted: ((value) => wasSubmitted = true),
          ),
        ),
      ),
    );
    await tester.pumpWidget(app);

    // Set initial focus to window.
    await tester.tapAt(const Offset(0, 0));

    // Move focus to first focusable widget via keyboard (TextField).
    await tester.sendKeyEvent(LogicalKeyboardKey.tab);

    // Press enter to submit
    await tester.sendKeyEvent(LogicalKeyboardKey.enter);

    expect(wasSubmitted, true); // <- THIS FAILS
  });

  testWidgets('Can submit button via LogicalKeyboardKey', (tester) async {
    bool wasSubmitted = false;
    final app = MaterialApp(
      home: Material(
        child: Center(
          child: ElevatedButton(
            onPressed: (() => wasSubmitted = true),
            child: const Text('Submit'),
          ),
        ),
      ),
    );
    await tester.pumpWidget(app);

    // Set initial focus to window.
    await tester.tapAt(const Offset(0, 0));

    // Move focus to first focusable widget via keyboard (ElevatedButton).
    await tester.sendKeyEvent(LogicalKeyboardKey.tab);

    // Press enter to submit
    await tester.sendKeyEvent(LogicalKeyboardKey.enter);

    expect(wasSubmitted, true); // <- THIS PASSES
  });
}
[✓] Flutter (Channel stable, 3.3.8, on macOS 12.6.1 21G217 darwin-x64, locale en-US)
    • Flutter version 3.3.8 on channel stable at /Users/nejrd/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 52b3dc25f6 (4 weeks ago), 2022-11-09 12:09:26 +0800
    • Engine revision 857bd6b74c
    • Dart version 2.18.4
    • DevTools version 2.15.0

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/nejrd/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.73.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.54.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our testsa: text inputEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/found in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions