buildForm method
FormGroup
buildForm(
- BuildContext context,
- List<
DeliveryProductVariant> ? productVariants, - List<
ProductVariantModel> ? variants
Implementation
FormGroup buildForm(
BuildContext context,
List<DeliveryProductVariant>? productVariants,
List<ProductVariantModel>? variants,
) {
final bloc = context.read<DeliverInterventionBloc>().state;
final overViewbloc = context.read<HouseholdOverviewBloc>().state;
_controllers.forEachIndexed((index, element) {
_controllers.removeAt(index);
});
// Add controllers for each product variant to the _controllers list.
if (_controllers.isEmpty) {
final int r = RegistrationDeliverySingleton()
.selectedProject
?.additionalDetails
?.projectType
?.cycles ==
null
? 1
: fetchProductVariant(
RegistrationDeliverySingleton()
.selectedProject
?.additionalDetails
?.projectType
?.cycles![bloc.cycle - 1]
.deliveries?[bloc.dose - 1],
overViewbloc.selectedIndividual,
overViewbloc.householdMemberWrapper.household)!
.productVariants
?.length ??
0;
_controllers.addAll(List.generate(r, (index) => index)
.mapIndexed((index, element) => index));
}
return fb.group(<String, Object>{
_doseAdministrationKey: FormControl<String>(
value:
'${localizations.translate(i18.deliverIntervention.cycle)} ${bloc.cycle == 0 ? (bloc.cycle + 1) : bloc.cycle}'
.toString(),
validators: [],
),
_deliveryCommentKey: FormControl<String>(
value: RegistrationDeliverySingleton().beneficiaryType !=
BeneficiaryType.individual
? (bloc.tasks?.last.additionalFields?.fields
.where((a) =>
a.key ==
AdditionalFieldsType.deliveryComment.toValue())
.toList() ??
[])
.isNotEmpty
? bloc.tasks?.last.additionalFields?.fields
.where((a) =>
a.key == AdditionalFieldsType.deliveryComment.toValue())
.first
.value
: ''
: null,
validators: [],
),
_dateOfAdministrationKey:
FormControl<DateTime>(value: DateTime.now(), validators: []),
_resourceDeliveredKey: FormArray<ProductVariantModel>(
[
..._controllers.map((e) => FormControl<ProductVariantModel>(
value: variants != null && variants.length < _controllers.length
? variants.last
: (variants != null &&
_controllers.indexOf(e) < variants.length
? variants.firstWhereOrNull(
(element) =>
element.id ==
productVariants
?.elementAt(_controllers.indexOf(e))
.productVariantId,
)
: null),
)),
],
),
_quantityDistributedKey: FormArray<int>([
..._controllers.mapIndexed(
(i, e) => FormControl<int>(
value: RegistrationDeliverySingleton().beneficiaryType !=
BeneficiaryType.individual
? int.tryParse(
bloc.tasks?.last.resources?.elementAt(i).quantity ?? '0',
)
: 0,
validators: [Validators.min(1)],
),
),
]),
});
}