Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix emoji insertion corruption by using grapheme-aware text operations#183112

Open
Istiak-Ahmed78 wants to merge 6 commits intoflutter:masterfrom
Istiak-Ahmed78:fix/emoji-surrogate-pair-android
Open

Fix emoji insertion corruption by using grapheme-aware text operations#183112
Istiak-Ahmed78 wants to merge 6 commits intoflutter:masterfrom
Istiak-Ahmed78:fix/emoji-surrogate-pair-android

Conversation

@Istiak-Ahmed78
Copy link
Contributor

Fixes a bug where inserting emojis between existing emojis in an RTL TextField on Android would break the text and render ? characters. The root cause was that the Android IME sends text updates using UTF-16 code unit positions, which can split surrogate pairs when the cursor is placed between emoji characters.

Changes made:

  • TextEditingValue.replaced() — Updated to use grapheme cluster boundaries instead of raw UTF-16 code unit positions, preventing surrogate pair splits during text replacement.
  • TextInput._validateAndCorrectTextEditingValue() — New method that detects and fixes broken surrogate pairs received from the Android IME before they reach the text input client.
  • _handleTextInputInvocation() — Now intercepts platform text updates and applies the correction above before forwarding to the text input client.

Before / After:

Before fix After fix
Behavior Inserting emoji between emojis in RTL TextField renders ? and breaks text Emoji inserts correctly without breaking surrounding characters
Before fix
543327209-706052f5-2309-4bdf-a44b-6ab05848f8a7.mp4
After fix
IMG_20260302_154756_666.mp4

Fixes #181759

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team.

- Update TextEditingValue.replaced() to use grapheme cluster boundaries
  instead of UTF-16 code unit positions to prevent splitting surrogate pairs
- Add TextInput._validateAndCorrectTextEditingValue() to detect and fix
  broken surrogate pairs received from the Android IME
- Intercept platform text updates in _handleTextInputInvocation() to apply
  corrections before updating the text input client

Fixes flutter#181759
@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. labels Mar 2, 2026
@Istiak-Ahmed78 Istiak-Ahmed78 marked this pull request as ready for review March 2, 2026 12:36
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a grapheme-aware text replacement logic and a correction mechanism for broken surrogate pairs from the Android IME, which effectively fixes the emoji insertion issue. The changes are well-tested. My review includes a couple of suggestions: one to improve the performance of the grapheme boundary calculation in TextEditingValue.replaced, and another to fix a potential bug in the logic for identifying the inserted character in _validateAndCorrectTextEditingValue.

Copy link
Contributor Author

@Istiak-Ahmed78 Istiak-Ahmed78 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All suggestions are applied. Ready to be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RTL TextField breaks when inserting emojis between existing emojis

1 participant