refactor(form)!: input builders provides FormInput
This commit is contained in:
		
							parent
							
								
									ee8ce76839
								
							
						
					
					
						commit
						08383ec03b
					
				| @ -39,7 +39,7 @@ class _NameInput extends StatelessWidget { | |||||||
|     return InputBuilderTextController<SimpleCustomFormCubit, String?, Metadata>( |     return InputBuilderTextController<SimpleCustomFormCubit, String?, Metadata>( | ||||||
|       field: formFieldName, |       field: formFieldName, | ||||||
|       builder: |       builder: | ||||||
|           (context, cubit, state, field, inputValid, controller, metadata) { |           (context, cubit, state, field, input, controller, metadata) { | ||||||
|         final meta = state.form.metadataOf<Metadata>(field).extra; |         final meta = state.form.metadataOf<Metadata>(field).extra; | ||||||
|         final color = computeColor(meta); |         final color = computeColor(meta); | ||||||
|         return Row( |         return Row( | ||||||
| @ -65,7 +65,7 @@ class _NameInput extends StatelessWidget { | |||||||
|                 decoration: InputDecoration( |                 decoration: InputDecoration( | ||||||
|                   labelText: 'name', |                   labelText: 'name', | ||||||
|                   helperText: '', |                   helperText: '', | ||||||
|                   errorText: !inputValid ? 'invalid name' : null, |                   errorText: input.validator.invalid ? 'invalid name' : null, | ||||||
|                 ), |                 ), | ||||||
|               ), |               ), | ||||||
|             ) |             ) | ||||||
| @ -81,7 +81,7 @@ class _PhoneInput extends StatelessWidget { | |||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|     return InputBuilderMetadata<SimpleCustomFormCubit, Metadata>( |     return InputBuilderMetadata<SimpleCustomFormCubit, Metadata>( | ||||||
|       field: formFieldPhone, |       field: formFieldPhone, | ||||||
|       builder: (context, cubit, state, field, inputValid, metadata) { |       builder: (context, cubit, state, field, input, metadata) { | ||||||
|         final meta = state.form.metadataOf<Metadata>(field).extra; |         final meta = state.form.metadataOf<Metadata>(field).extra; | ||||||
|         final color = computeColor(meta); |         final color = computeColor(meta); | ||||||
|         return Row( |         return Row( | ||||||
| @ -106,7 +106,7 @@ class _PhoneInput extends StatelessWidget { | |||||||
|                 decoration: InputDecoration( |                 decoration: InputDecoration( | ||||||
|                   labelText: 'phone', |                   labelText: 'phone', | ||||||
|                   helperText: '', |                   helperText: '', | ||||||
|                   errorText: !inputValid ? 'invalid phone' : null, |                   errorText: input.validator.invalid ? 'invalid phone' : null, | ||||||
|                 ), |                 ), | ||||||
|               ), |               ), | ||||||
|             ) |             ) | ||||||
| @ -122,13 +122,13 @@ class _EmailInput extends StatelessWidget { | |||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|     return InputBuilder<SimpleCustomFormCubit>( |     return InputBuilder<SimpleCustomFormCubit>( | ||||||
|       field: formFieldEmail, |       field: formFieldEmail, | ||||||
|       builder: (context, cubit, state, field, inputValid) => TextField( |       builder: (context, cubit, state, field, input) => TextField( | ||||||
|         onChanged: (value) => cubit.dataChanged(field, Email.dirty(value)), |         onChanged: (value) => cubit.dataChanged(field, Email.dirty(value)), | ||||||
|         keyboardType: TextInputType.emailAddress, |         keyboardType: TextInputType.emailAddress, | ||||||
|         decoration: InputDecoration( |         decoration: InputDecoration( | ||||||
|           labelText: 'email', |           labelText: 'email', | ||||||
|           helperText: '', |           helperText: '', | ||||||
|           errorText: !inputValid ? 'invalid email' : null, |           errorText: input.validator.invalid ? 'invalid email' : null, | ||||||
|         ), |         ), | ||||||
|       ), |       ), | ||||||
|     ); |     ); | ||||||
|  | |||||||
| @ -25,8 +25,7 @@ class _NameInput extends StatelessWidget { | |||||||
|   Widget build(BuildContext context) { |   Widget build(BuildContext context) { | ||||||
|     return InputBuilderTextController<SimpleCustomFormCubit, String?, Metadata>( |     return InputBuilderTextController<SimpleCustomFormCubit, String?, Metadata>( | ||||||
|       field: formFieldName, |       field: formFieldName, | ||||||
|       builder: |       builder: (context, cubit, state, field, input, controller, metadata) { | ||||||
|           (context, cubit, state, field, inputValid, controller, metadata) { |  | ||||||
|         return TextField( |         return TextField( | ||||||
|           controller: controller, |           controller: controller, | ||||||
|           onChanged: (value) => cubit.dataChanged(field, Name.dirty(value)), |           onChanged: (value) => cubit.dataChanged(field, Name.dirty(value)), | ||||||
| @ -34,7 +33,7 @@ class _NameInput extends StatelessWidget { | |||||||
|           decoration: InputDecoration( |           decoration: InputDecoration( | ||||||
|             labelText: 'name', |             labelText: 'name', | ||||||
|             helperText: '', |             helperText: '', | ||||||
|             errorText: !inputValid ? 'invalid name' : null, |             errorText: input.validator.invalid ? 'invalid name' : null, | ||||||
|           ), |           ), | ||||||
|         ); |         ); | ||||||
|       }, |       }, | ||||||
|  | |||||||
| @ -28,16 +28,16 @@ class InputBuilder<Cubit extends FormDataCubit> extends StatelessWidget { | |||||||
|     Cubit cubit, |     Cubit cubit, | ||||||
|     FormDataState state, |     FormDataState state, | ||||||
|     String field, |     String field, | ||||||
|     bool inputValid, |     FormInput<dynamic, FormInputValidator<dynamic, ValidationError>, dynamic> | ||||||
|  |         input, | ||||||
|   ) builder; |   ) builder; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) => |   Widget build(BuildContext context) => BlocBuilder<Cubit, FormDataState>( | ||||||
|       BlocBuilder<Cubit, FormDataState>( |  | ||||||
|         builder: (context, state) { |         builder: (context, state) { | ||||||
|           final cubit = context.watch<Cubit>(); |           final cubit = context.watch<Cubit>(); | ||||||
|           final inputValid = state.form.validatorOf(field).valid; |           final input = state.form.inputOf(field); | ||||||
|           return builder.call(context, cubit, state, field, inputValid); |           return builder.call(context, cubit, state, field, input); | ||||||
|         }, |         }, | ||||||
|       ); |       ); | ||||||
| } | } | ||||||
|  | |||||||
| @ -16,7 +16,10 @@ | |||||||
| 
 | 
 | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||||
|  | import 'package:wyatt_form_bloc/src/core/enums/validation_error.dart'; | ||||||
|  | import 'package:wyatt_form_bloc/src/domain/entities/form_input.dart'; | ||||||
| import 'package:wyatt_form_bloc/src/domain/entities/form_input_metadata.dart'; | import 'package:wyatt_form_bloc/src/domain/entities/form_input_metadata.dart'; | ||||||
|  | import 'package:wyatt_form_bloc/src/domain/input_validators/form_input_validator.dart'; | ||||||
| import 'package:wyatt_form_bloc/src/presentation/presentation.dart'; | import 'package:wyatt_form_bloc/src/presentation/presentation.dart'; | ||||||
| 
 | 
 | ||||||
| class InputBuilderMetadata<Cubit extends FormDataCubit, Extra> | class InputBuilderMetadata<Cubit extends FormDataCubit, Extra> | ||||||
| @ -34,22 +37,23 @@ class InputBuilderMetadata<Cubit extends FormDataCubit, Extra> | |||||||
|     Cubit cubit, |     Cubit cubit, | ||||||
|     FormDataState state, |     FormDataState state, | ||||||
|     String field, |     String field, | ||||||
|     bool inputValid, |     FormInput<dynamic, FormInputValidator<dynamic, ValidationError>, dynamic> | ||||||
|  |         input, | ||||||
|     FormInputMetadata<Extra>? metadata, |     FormInputMetadata<Extra>? metadata, | ||||||
|   ) builder; |   ) builder; | ||||||
| 
 | 
 | ||||||
|   @override |   @override | ||||||
|   Widget build(BuildContext context) => BlocBuilder<Cubit, FormDataState>( |   Widget build(BuildContext context) => BlocBuilder<Cubit, FormDataState>( | ||||||
|         builder: (context, state) { |         builder: (context, state) { | ||||||
|           final cubit = context.read<Cubit>(); |           final cubit = context.watch<Cubit>(); | ||||||
|           final inputValid = state.form.validatorOf(field).valid; |           final input = state.form.inputOf(field); | ||||||
|           final metadata = state.form.metadataOf<Extra>(field); |           final metadata = input.metadata as FormInputMetadata<Extra>?; | ||||||
|           return builder.call( |           return builder.call( | ||||||
|             context, |             context, | ||||||
|             cubit, |             cubit, | ||||||
|             state, |             state, | ||||||
|             field, |             field, | ||||||
|             inputValid, |             input, | ||||||
|             metadata, |             metadata, | ||||||
|           ); |           ); | ||||||
|         }, |         }, | ||||||
|  | |||||||
| @ -16,7 +16,10 @@ | |||||||
| 
 | 
 | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||||
|  | import 'package:wyatt_form_bloc/src/core/enums/validation_error.dart'; | ||||||
|  | import 'package:wyatt_form_bloc/src/domain/entities/form_input.dart'; | ||||||
| import 'package:wyatt_form_bloc/src/domain/entities/form_input_metadata.dart'; | import 'package:wyatt_form_bloc/src/domain/entities/form_input_metadata.dart'; | ||||||
|  | import 'package:wyatt_form_bloc/src/domain/input_validators/form_input_validator.dart'; | ||||||
| import 'package:wyatt_form_bloc/src/domain/repositories/form_repository.dart'; | import 'package:wyatt_form_bloc/src/domain/repositories/form_repository.dart'; | ||||||
| import 'package:wyatt_form_bloc/src/presentation/features/form_data/form_data_cubit.dart'; | import 'package:wyatt_form_bloc/src/presentation/features/form_data/form_data_cubit.dart'; | ||||||
| 
 | 
 | ||||||
| @ -35,7 +38,8 @@ class InputBuilderTextController<Cubit extends FormDataCubit, S extends String?, | |||||||
|     Cubit cubit, |     Cubit cubit, | ||||||
|     FormDataState state, |     FormDataState state, | ||||||
|     String field, |     String field, | ||||||
|     bool inputValid, |     FormInput<dynamic, FormInputValidator<dynamic, ValidationError>, dynamic> | ||||||
|  |         input, | ||||||
|     TextEditingController textEditingController, |     TextEditingController textEditingController, | ||||||
|     FormInputMetadata<Extra>? metadata, |     FormInputMetadata<Extra>? metadata, | ||||||
|   ) builder; |   ) builder; | ||||||
| @ -57,15 +61,15 @@ class InputBuilderTextController<Cubit extends FormDataCubit, S extends String?, | |||||||
| 
 | 
 | ||||||
|     return BlocBuilder<Cubit, FormDataState>( |     return BlocBuilder<Cubit, FormDataState>( | ||||||
|       builder: (context, state) { |       builder: (context, state) { | ||||||
|         final cubit = context.read<Cubit>(); |         final cubit = context.watch<Cubit>(); | ||||||
|         final inputValid = state.form.validatorOf(field).valid; |         final input = state.form.inputOf(field); | ||||||
|         final metadata = state.form.metadataOf<Extra>(field); |         final metadata = input.metadata as FormInputMetadata<Extra>?; | ||||||
|         return builder.call( |         return builder.call( | ||||||
|           context, |           context, | ||||||
|           cubit, |           cubit, | ||||||
|           state, |           state, | ||||||
|           field, |           field, | ||||||
|           inputValid, |           input, | ||||||
|           _controller, |           _controller, | ||||||
|           metadata, |           metadata, | ||||||
|         ); |         ); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user