Skip to content

Commit

Permalink
Merge pull request #19 from leancodepl/chore/use-clear-in-example
Browse files Browse the repository at this point in the history
Use clear() in the example
  • Loading branch information
michalina-majewska authored Jun 17, 2024
2 parents 9b6855f + cf33575 commit 41106d4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -289,4 +291,4 @@ class SubformCubit extends FormGroupCubit {
final subformField = TextFieldCubit();
}
```
```
1 change: 1 addition & 0 deletions example/lib/screens/complex_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class HumanSubform extends StatelessWidget {
translateError: validatorTranslator,
labelText: 'Gender',
hintText: 'Select gender',
canSetToInitial: true,
),
const SizedBox(height: 16),
FormTextField(
Expand Down
4 changes: 4 additions & 0 deletions example/lib/screens/simple_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class SimpleForm extends StatelessWidget {
translateError: validatorTranslator,
labelText: 'First Name',
hintText: 'Enter your first name',
canSetToInitial: true,
),
FormTextField(
field: context.read<SimpleFormCubit>().lastName,
translateError: validatorTranslator,
labelText: 'Last Name',
hintText: 'Enter your last name',
canSetToInitial: true,
),
FormTextField(
field: context.read<SimpleFormCubit>().email,
Expand Down Expand Up @@ -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),
);

Expand Down
24 changes: 18 additions & 6 deletions example/lib/widgets/app_dropdown_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class AppDropdownField<T> extends StatelessWidget {
required this.onChanged,
this.label,
this.hint,
this.onClear,
this.errorText,
this.onSetToInitial,
this.onEmpty,
});

final List<T> options;
Expand All @@ -19,8 +20,9 @@ class AppDropdownField<T> extends StatelessWidget {
final ValueChanged<T?> onChanged;
final String? label;
final String? hint;
final VoidCallback? onClear;
final String? errorText;
final VoidCallback? onSetToInitial;
final VoidCallback? onEmpty;

@override
Widget build(BuildContext context) {
Expand All @@ -45,10 +47,20 @@ class AppDropdownField<T> 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'),
),
],
],
);
}
Expand Down
49 changes: 36 additions & 13 deletions example/lib/widgets/app_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AppTextField extends HookWidget {
this.hintText,
this.errorText,
this.suffix,
this.onSetToInitial,
});

final TextEditingController? controller;
Expand All @@ -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) {
Expand All @@ -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'),
),
],
],
);
}
}
4 changes: 3 additions & 1 deletion example/lib/widgets/form_dropdown_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class FormDropdownField<T, E extends Object> extends FieldBuilder<T?, E> {
required ErrorTranslator<E> translateError,
String? labelText,
String? hintText,
bool canSetToInitial = false,
}) : super(
builder: (context, state) => AppDropdownField(
value: state.value,
Expand All @@ -17,9 +18,10 @@ class FormDropdownField<T, E extends Object> extends FieldBuilder<T?, E> {
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),
),
);
}
7 changes: 7 additions & 0 deletions example/lib/widgets/form_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FormTextField<E extends Object> extends FieldBuilder<String, E> {
bool? trimOnUnfocus,
String? labelText,
String? hintText,
bool canSetToInitial = false,
}) : super(
builder: (context, state) => AppTextField(
key: key,
Expand All @@ -34,6 +35,12 @@ class FormTextField<E extends Object> extends FieldBuilder<String, E> {
child: CircularProgressIndicator(),
)
: null,
onSetToInitial: canSetToInitial
? () {
field.clear();
return field.state.value;
}
: null,
),
);
}
Expand Down

0 comments on commit 41106d4

Please sign in to comment.