Skip to content

Instantly share code, notes, and snippets.

@roipeker
Created September 5, 2020 23:48
Show Gist options
  • Save roipeker/a67808ca42fcc0c85dde95c0c54f38e3 to your computer and use it in GitHub Desktop.
Save roipeker/a67808ca42fcc0c85dde95c0c54f38e3 to your computer and use it in GitHub Desktop.
WIP idea for future GetTextField
/// roipeker 2020
/// Easy way to bind a RxString to a TextEditingController.
abstract class SingleTextController implements DisposableInterface {
TextEditingController _textController;
Worker _worker;
RxString _rx;
TextEditingController _buildController() => TextEditingController();
TextEditingController get textController {
_textController ??= _buildController();
return _textController;
}
onClose() {
_worker?.dispose();
_textController?.removeListener(_onTextChanged);
_textController?.dispose();
}
void bindRx(RxString val) {
_rx = val;
textController.addListener(_onTextChanged);
_worker = ever(
_rx,
(newText) => _textController.value =
_textController.value.copyWith(text: newText));
}
void _onTextChanged() => _rx.value = _textController.text;
}
class TextController extends GetxController with SingleTextController {
TextController(RxString value) {
bindRx(value);
textController.text = '$value';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment