Skip to content

TextInputFormatter and Samsung Keyboard Autocorrect give wrong results due to race conditions #171955

@Bassiuz

Description

@Bassiuz

Steps to reproduce

  1. Create a TextFormField with a TextInputFormatter that edits the text (Such as capitalize only the first word)
  2. Type a text in all lower case in with a Samsung phone.

Expected results

The text in the text field should be formatted by the TextInputFormatter
The Autocorrect of the keyboard should correct/suggest based on the full word, including the first letter that was changed by the TextInputFormatter

Actual results

The first letter is removed from the suggestion. Example

When typing 'sebastiaan' the suggestion is 'ebastiaan' and not 'Sebastiaan'.

Code sample

Code sample
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String capitalizeFirstLetter(String input) {
    if (input.isEmpty) return input;
    return input[0].toUpperCase() + input.substring(1);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              textCapitalization: TextCapitalization.none,
              inputFormatters: [
                TextInputFormatter.withFunction(
                  (oldValue, newValue) {
                    final newText = newValue.text;
                    if (oldValue.text == newText) return newValue;
                    String capitalizedText = capitalizeFirstLetter(newText);

                    if (capitalizedText == newText) return newValue;

                    // Adding a delay here will prevent the first letter from being overwritten in autocorrect
                    // sleep(const Duration(milliseconds: 300));

                    return newValue.copyWith(
                      text: capitalizeFirstLetter(newText),
                      selection: newValue.selection,
                      composing: TextRange(start: 0, end: newText.length),
                    );
                  },
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

The example where the first letter is excluded in autocorrect due to Race Conditions with autocorrect
Image

This example correctly handles autocorrect; because the formatter has a delay. This delay
Image

Logs

Logs

No relevant logs shown

Flutter Doctor output

Doctor output
flutter doctor -v
[✓] Flutter (Channel stable, 3.22.3, on macOS 15.5 24F74 darwin-arm64, locale en-US)
    • Flutter version 3.22.3 on channel stable at /Users/bassiuz/fvm/versions/3.22.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b0850beeb2 (12 months ago), 2024-07-16 21:43:41 -0700
    • Engine revision 235db911ba
    • Dart version 3.4.4
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
    • Android SDK at /Users/bassiuz/Library/Android/sdk
    • Platform android-35, build-tools 35.0.1
    • ANDROID_SDK_ROOT = /Users/bassiuz/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

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

[✓] Android Studio (version 2024.1)
    • 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 17.0.11+0-17.0.11b1207.24-11852314)

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

[✓] Connected device (4 available)
    • SM G973F (mobile)               • RF8M22A0BJT           • android-arm64  • Android 12 (API 31)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.5 24F74 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.5 24F74 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 138.0.7204.100
    ! Error: Browsing on the local area network for iPhone TK. Ensure the device is unlocked and attached with a cable or associated with the same local area network as
      this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for RIVD Afluisterapparaat. Ensure the device is unlocked and attached with a cable or associated with the same local
      area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemse: device-specificOnly manifests on certain devicese: samsungIssues only reproducible on Samsung devicesplatform-androidAndroid applications specificallyteam-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy