form_field_controller.dart 759 B

1234567891011121314151617181920212223
  1. import 'package:flutter/foundation.dart';
  2. class FormFieldController<T> extends ValueNotifier<T?> {
  3. FormFieldController(this.initialValue) : super(initialValue);
  4. final T? initialValue;
  5. void reset() => value = initialValue;
  6. void update() => notifyListeners();
  7. }
  8. // If the initial value is a list (which it is for multiselect),
  9. // we need to use this controller to avoid a pass by reference issue
  10. // that can result in the initial value being modified.
  11. class FormListFieldController<T> extends FormFieldController<List<T>> {
  12. final List<T>? _initialListValue;
  13. FormListFieldController(super.initialValue)
  14. : _initialListValue = List<T>.from(initialValue ?? []);
  15. @override
  16. void reset() => value = List<T>.from(_initialListValue ?? []);
  17. }