Last active
January 15, 2021 05:12
-
-
Save nhancv/7181eb1706df726f184e043d6d9f5a31 to your computer and use it in GitHub Desktop.
Flutter Insert text to TextField
This file contains 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
// Insert text to TextField | |
void insertText(String insert, TextEditingController controller) { | |
final int cursorPos = controller.selection.base.offset; | |
controller.value = controller.value.copyWith( | |
text: controller.text.replaceRange(max(cursorPos, 0), max(cursorPos, 0), insert), | |
selection: TextSelection.fromPosition(TextPosition(offset: max(cursorPos, 0) + insert.length)) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment