Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clear() in the example #19

Merged
merged 7 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`, `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.
shilangyu marked this conversation as resolved.
Show resolved Hide resolved

### 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
51 changes: 38 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,42 @@ 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('');
}
},
shilangyu marked this conversation as resolved.
Show resolved Hide resolved
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
Loading