Fix emoji insertion corruption by using grapheme-aware text operations#183112
Open
Istiak-Ahmed78 wants to merge 6 commits intoflutter:masterfrom
Open
Fix emoji insertion corruption by using grapheme-aware text operations#183112Istiak-Ahmed78 wants to merge 6 commits intoflutter:masterfrom
Istiak-Ahmed78 wants to merge 6 commits intoflutter:masterfrom
Conversation
- 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
Contributor
There was a problem hiding this comment.
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.
…/Istiak-Ahmed78/flutter into fix/emoji-surrogate-pair-android
Istiak-Ahmed78
commented
Mar 2, 2026
Contributor
Author
Istiak-Ahmed78
left a comment
There was a problem hiding this comment.
All suggestions are applied. Ready to be merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a bug where inserting emojis between existing emojis in an RTL
TextFieldon 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:
?and breaks text543327209-706052f5-2309-4bdf-a44b-6ab05848f8a7.mp4
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-assistbot should not be taken as authoritative feedback from the Flutter team.