TextControlBinder constructor
TextControlBinder({
- required FormControl<
String> control, - required TextEditingController controller,
Creates a new binder and immediately wires the two-way synchronization.
On initialization:
- The controller is updated to match the current
control.value. - Listeners are attached:
- controller listener propagates user edits to the control.
- control listener propagates programmatic updates back to the controller.
Implementation
TextControlBinder({
required this.control,
required this.controller,
}) {
// Initialize the controller with the current control value.
// We don't preserve selection on first set because there is no meaningful
// cursor state to keep yet.
_setControllerText(control.value, preserveSelection: false);
// When the user types (controller changes), update the form control.
controller.addListener(_onControllerChanged);
// When the form control changes programmatically, update the controller text.
control.valueNotifier.addListener(_onControlChanged);
}