Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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