diff --git a/README.md b/README.md index a3df8d9..30cc10c 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,8 @@ The package contains a collection of field cubits useful for implementing common - `SingleSelectFieldCubit` - specialization of `FieldCubit` for a single choice of value from list of options, - `MultiSelectFieldCubit` - specialization of `FieldCubit` for a multiple choice of values from list of options. +`TextFieldCubit`, `SingleSelectFieldCubit` and `MultiSelectFieldCubit` contain the `clear()` method that resets the value of the field to the initial value by calling `reset()`. You can also call `reset()` as it is defined in the `FieldCubit` class. + ### Creating custom `FieldCubit` If none of the existing `FieldCubit` implementations meet your requirements, you can create your own. Simply create a class that extends `FieldCubit`. Inside such cubit, you can add any method or a field. @@ -289,4 +291,4 @@ class SubformCubit extends FormGroupCubit { final subformField = TextFieldCubit(); } -``` \ No newline at end of file +``` diff --git a/example/lib/screens/complex_form.dart b/example/lib/screens/complex_form.dart index 0470b33..41be625 100644 --- a/example/lib/screens/complex_form.dart +++ b/example/lib/screens/complex_form.dart @@ -80,6 +80,7 @@ class HumanSubform extends StatelessWidget { translateError: validatorTranslator, labelText: 'Gender', hintText: 'Select gender', + canSetToInitial: true, ), const SizedBox(height: 16), FormTextField( diff --git a/example/lib/screens/simple_form.dart b/example/lib/screens/simple_form.dart index d4327f3..0bc31e6 100644 --- a/example/lib/screens/simple_form.dart +++ b/example/lib/screens/simple_form.dart @@ -34,12 +34,14 @@ class SimpleForm extends StatelessWidget { translateError: validatorTranslator, labelText: 'First Name', hintText: 'Enter your first name', + canSetToInitial: true, ), FormTextField( field: context.read().lastName, translateError: validatorTranslator, labelText: 'Last Name', hintText: 'Enter your last name', + canSetToInitial: true, ), FormTextField( field: context.read().email, @@ -68,10 +70,12 @@ class SimpleFormCubit extends FormGroupCubit { } final firstName = TextFieldCubit( + initialValue: 'John', validator: filled(ValidationError.empty), ); final lastName = TextFieldCubit( + initialValue: 'Foo', validator: filled(ValidationError.empty), ); diff --git a/example/lib/widgets/app_dropdown_field.dart b/example/lib/widgets/app_dropdown_field.dart index 79585ea..aa6a66b 100644 --- a/example/lib/widgets/app_dropdown_field.dart +++ b/example/lib/widgets/app_dropdown_field.dart @@ -9,8 +9,9 @@ class AppDropdownField extends StatelessWidget { required this.onChanged, this.label, this.hint, - this.onClear, this.errorText, + this.onSetToInitial, + this.onEmpty, }); final List options; @@ -19,8 +20,9 @@ class AppDropdownField extends StatelessWidget { final ValueChanged onChanged; final String? label; final String? hint; - final VoidCallback? onClear; final String? errorText; + final VoidCallback? onSetToInitial; + final VoidCallback? onEmpty; @override Widget build(BuildContext context) { @@ -45,10 +47,20 @@ class AppDropdownField extends StatelessWidget { ), ), ), - ElevatedButton( - onPressed: onClear, - child: const Text('Clear choice'), - ), + if (onEmpty case final onEmpty?) ...[ + const SizedBox(width: 16), + ElevatedButton( + onPressed: onEmpty, + child: const Text('Empty'), + ), + ], + if (onSetToInitial case final onSetToInitial?) ...[ + const SizedBox(width: 16), + ElevatedButton( + onPressed: onSetToInitial, + child: const Text('Set to initial'), + ), + ], ], ); } diff --git a/example/lib/widgets/app_text_field.dart b/example/lib/widgets/app_text_field.dart index 0ab6898..e632a65 100644 --- a/example/lib/widgets/app_text_field.dart +++ b/example/lib/widgets/app_text_field.dart @@ -19,6 +19,7 @@ class AppTextField extends HookWidget { this.hintText, this.errorText, this.suffix, + this.onSetToInitial, }); final TextEditingController? controller; @@ -33,6 +34,7 @@ class AppTextField extends HookWidget { final String? hintText; final String? errorText; final Widget? suffix; + final String Function()? onSetToInitial; @override Widget build(BuildContext context) { @@ -59,19 +61,40 @@ class AppTextField extends HookWidget { [], ); - return TextFormField( - autocorrect: false, - focusNode: focusNode, - controller: textEditingController, - onChanged: onChanged, - onTapOutside: (_) => focusNode.unfocus(), - onFieldSubmitted: onFieldSubmitted, - decoration: InputDecoration( - labelText: labelText, - hintText: hintText, - errorText: errorText, - suffix: suffix, - ), + return Row( + children: [ + Flexible( + child: TextFormField( + autocorrect: false, + focusNode: focusNode, + controller: textEditingController, + onChanged: onChanged, + onTapOutside: (_) => focusNode.unfocus(), + onFieldSubmitted: onFieldSubmitted, + decoration: InputDecoration( + labelText: labelText, + hintText: hintText, + errorText: errorText, + suffix: suffix, + ), + ), + ), + const SizedBox(width: 16), + ElevatedButton( + onPressed: () { + textEditingController.clear(); + setValue(''); + }, + child: const Text('Empty'), + ), + if (onSetToInitial case final onSetToInitial?) ...[ + const SizedBox(width: 16), + ElevatedButton( + onPressed: () => textEditingController.text = onSetToInitial(), + child: const Text('Set to initial'), + ), + ], + ], ); } } diff --git a/example/lib/widgets/form_dropdown_field.dart b/example/lib/widgets/form_dropdown_field.dart index 819600c..73c18ce 100644 --- a/example/lib/widgets/form_dropdown_field.dart +++ b/example/lib/widgets/form_dropdown_field.dart @@ -9,6 +9,7 @@ class FormDropdownField extends FieldBuilder { required ErrorTranslator translateError, String? labelText, String? hintText, + bool canSetToInitial = false, }) : super( builder: (context, state) => AppDropdownField( value: state.value, @@ -17,9 +18,10 @@ class FormDropdownField extends FieldBuilder { labelBuilder: labelBuilder, label: labelText, hint: hintText, - onClear: () => field.select(null), errorText: state.error != null ? translateError(state.error!) : null, + onSetToInitial: canSetToInitial ? field.clear : null, + onEmpty: () => field.select(null), ), ); } diff --git a/example/lib/widgets/form_text_field.dart b/example/lib/widgets/form_text_field.dart index c564c9b..8e95c6b 100644 --- a/example/lib/widgets/form_text_field.dart +++ b/example/lib/widgets/form_text_field.dart @@ -14,6 +14,7 @@ class FormTextField extends FieldBuilder { bool? trimOnUnfocus, String? labelText, String? hintText, + bool canSetToInitial = false, }) : super( builder: (context, state) => AppTextField( key: key, @@ -34,6 +35,12 @@ class FormTextField extends FieldBuilder { child: CircularProgressIndicator(), ) : null, + onSetToInitial: canSetToInitial + ? () { + field.clear(); + return field.state.value; + } + : null, ), ); }