Skip to content

Instantly share code, notes, and snippets.

@nhancv
Last active January 15, 2021 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/7181eb1706df726f184e043d6d9f5a31 to your computer and use it in GitHub Desktop.
Save nhancv/7181eb1706df726f184e043d6d9f5a31 to your computer and use it in GitHub Desktop.
Flutter Insert text to TextField
// 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