Skip to content

TextField: cursor position bugs when using TextSpan with one character (e.g. space). #49044

@mikeesouth

Description

@mikeesouth

Steps to Reproduce

  1. Run flutter create single_char_textspan_bug
  2. Create a new file: fruit_colorizer.dart (inspired from this stackoverflow post)
  3. Input this code for fruit_colorizer.dart:
import 'package:flutter/material.dart';

class FruitColorizer extends TextEditingController {
  final Map<String, TextStyle> mapping;
  final Pattern pattern;

  FruitColorizer(this.mapping)
      : pattern =
            RegExp(mapping.keys.map((key) => RegExp.escape(key)).join('|'));
  @override
  TextSpan buildTextSpan({TextStyle style, bool withComposing}) {
    List<InlineSpan> children = [];
    // splitMapJoin is a bit tricky here but i found it very handy for populating children list
    text.splitMapJoin(
      pattern,
      onMatch: (Match match) {
        children.add(
            TextSpan(text: match[0], style: style.merge(mapping[match[0]])));
        return '';
      },
      onNonMatch: (String text) {
        children.add(TextSpan(text: text, style: style));
        return '';
      },
    );
    return TextSpan(style: style, children: children);
  }
}
  1. Add this field to the _MyHomePageState in main.dart:
final _controller = FruitColorizer({'apple': TextStyle(color: Colors.green),
    'orange': TextStyle(color: Colors.orange),});
  1. Add a TextField as the last child to the Column() in the build() method in main.dart:
TextField(style: TextStyle(fontSize: 20),
              controller: _controller1,),
  1. Start the app, try to put the cursor before the "o" in "orange". Also - try to drag the cursor character per character from right to left in the text field, notice that it "jumps" when dragging over the space.

Expected results: I want to be able to place the cursor before the "o" in "orange".

Actual results: The cursor is buggy around the space and I cannot put it after the space, before the o. When dragging the cursor using the selection handle, it "jumps" around the space.

No exception occurs. No logs should be interesting. I'm currently on master branch (1.14.2-pre34) but I have also reproduced this in beta branch (1.13.6). It's probably in stable too but I haven't checked.
Also - I tried to use a normal TextEditingController and extend the EditableText widget and override it's buildTextSpan function to achieve the same result - mutiple text styles within the same text field. It gives the exact same bug.

Minimal reproduction with instructions can be found here:
https://github.com/mikeesouth/single_char_textspan

Logs

Flutter analyze:

C:\Stuff\git\single_char_textspan [master ≡]> flutter analyze
Analyzing single_char_textspan...
No issues found! (ran in 1.2s)

flutter doctor -v

C:\Stuff\git\single_char_textspan [master ≡]> flutter doctor -v
[√] Flutter (Channel master, v1.14.2-pre.34, on Microsoft Windows [Version 10.0.18363.592], locale sv-SE)
    • Flutter version 1.14.2-pre.34 at C:\Stuff\flutter
    • Framework revision bf1d822198 (9 hours ago), 2020-01-16 19:38:01 -0800
    • Engine revision 85a8ac4255
    • Dart version 2.8.0 (build 2.8.0-dev.3.0 f1df196ddf)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Stuff\AndroidSDK
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = C:\Stuff\AndroidSDK
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 42.1.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] VS Code (version 1.41.1)
    • VS Code at C:\Users\micke\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.7.1

[√] Connected device (1 available)
    • AOSP on IA Emulator • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsf: gesturesflutter/packages/flutter/gestures repository.f: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions