Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:【update Demo】manualDelete function support delete system emoji #219

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion example/lib/pages/complex/text_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,28 @@ class _TextDemoState extends State<TextDemo> {
});
}


// Is then code (a 16-bit unsigned integer) a UTF-16 lead surrogate.
bool _isLeadSurrogate(int code) => (code & 0xFC00) == 0xD800;

// Is then code (a 16-bit unsigned integer) a UTF-16 trail surrogate.
bool _isTrailSurrogate(int code) => (code & 0xFC00) == 0xDC00;


bool checkIsUTF16(String content, int index) {
if(content.isEmpty || index < 0) {
return false;
}
final int code = content.codeUnitAt(index);
if (_isTrailSurrogate(code) && content.length > 1 && index >= 1) {
final int previousCode = content.codeUnitAt(index - 1);
if (_isLeadSurrogate(previousCode)) {
return true;
}
}
return false;
}

void manualDelete() {
//delete by code
final TextEditingValue _value = _textEditingController.value;
Expand All @@ -433,10 +455,14 @@ class _TextDemoState extends State<TextDemo> {
if (selection.isCollapsed && selection.start == 0) {
return;
}
final int start =
int start =
selection.isCollapsed ? selection.start - 1 : selection.start;
final int end = selection.end;

if(checkIsUTF16(actualText, end - 1)){
start--;
}

value = TextEditingValue(
text: actualText.replaceRange(start, end, ''),
selection: TextSelection.collapsed(offset: start),
Expand Down