Created
September 5, 2020 23:48
-
-
Save roipeker/a67808ca42fcc0c85dde95c0c54f38e3 to your computer and use it in GitHub Desktop.
WIP idea for future GetTextField
This file contains hidden or 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
/// 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