From 6587b1ff9efcd961ab68dd5586b4bd0aafcc4766 Mon Sep 17 00:00:00 2001 From: Naveen J <83631045+naveen-egov@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:27:47 +0530 Subject: [PATCH] [fix] added dropdown value population (#194) --- .../individual_details.dart | 14 +- .../lib/pages/boundary_selection.dart | 174 ++++++++++-------- .../lib/pages/login.dart | 4 +- .../lib/widgets/atoms/digit_dropdown.dart | 32 +++- .../atoms/digit_reactive_dropdown.dart | 10 - 5 files changed, 130 insertions(+), 104 deletions(-) diff --git a/apps/health_campaign_field_worker_app/lib/pages/beneficiary_registration/individual_details.dart b/apps/health_campaign_field_worker_app/lib/pages/beneficiary_registration/individual_details.dart index bc8c52652..160e7f658 100644 --- a/apps/health_campaign_field_worker_app/lib/pages/beneficiary_registration/individual_details.dart +++ b/apps/health_campaign_field_worker_app/lib/pages/beneficiary_registration/individual_details.dart @@ -349,16 +349,15 @@ class _IndividualDetailsPageState appConfiguration.genderOptions ?? []; - return DigitReactiveDropdown( + return DigitDropdown( label: localizations.translate( i18.individualDetails.genderLabelText, ), - valueMapper: (value) => - localizations.translate(value), + valueMapper: (value) => value, initialValue: genderOptions.firstOrNull?.name, menuItems: genderOptions.map( (e) { - return e.code; + return localizations.translate(e.name); }, ).toList(), formControlName: _genderKey, @@ -501,10 +500,9 @@ class _IndividualDetailsPageState final options = appConfiguration.genderOptions ?? []; - return options - .map((e) => localizations.translate(e.name)) - .firstWhereOrNull( - (element) => element == individual?.gender?.name, + return options.map((e) => e.code).firstWhereOrNull( + (element) => + element.toLowerCase() == individual?.gender?.name, ); }, ), diff --git a/apps/health_campaign_field_worker_app/lib/pages/boundary_selection.dart b/apps/health_campaign_field_worker_app/lib/pages/boundary_selection.dart index c291260bc..6f677cd43 100644 --- a/apps/health_campaign_field_worker_app/lib/pages/boundary_selection.dart +++ b/apps/health_campaign_field_worker_app/lib/pages/boundary_selection.dart @@ -3,6 +3,7 @@ import 'package:collection/collection.dart'; import 'package:digit_components/digit_components.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:reactive_forms/reactive_forms.dart'; import '../blocs/boundary/boundary.dart'; import '../models/data_model.dart'; @@ -16,6 +17,7 @@ class BoundarySelectionPage extends StatefulWidget { class _BoundarySelectionPageState extends State { bool shouldPop = false; + static const _selectedBoundaryKey = 'boundary'; @override Widget build(BuildContext context) { @@ -37,90 +39,95 @@ class _BoundarySelectionPageState extends State { final labelList = state.selectedBoundaryMap.keys.toList(); - return Column( - children: [ - Expanded( - child: ListView.builder( - itemCount: labelList.length, - itemBuilder: (context, labelIndex) { - final label = labelList.elementAt(labelIndex); - - final filteredItems = - state.boundaryList.where((element) { - if (element.label != label) return false; - - if (labelIndex == 0) return true; - final parentIndex = labelIndex - 1; - - final parentBoundaryEntry = state - .selectedBoundaryMap.entries - .elementAtOrNull(parentIndex); - final parentBoundary = parentBoundaryEntry?.value; - if (parentBoundary == null) return false; - - if (element.materializedPathList - .contains(parentBoundary.code)) { - return true; - } - - return false; - }).toList(); - - return Padding( - padding: const EdgeInsets.symmetric( - horizontal: kPadding * 2, - ), - child: DigitDropdown( - value: state.selectedBoundaryMap.entries - .firstWhereOrNull( - (element) => element.key == label, - ) - ?.value, - label: label, - menuItems: filteredItems, - onChanged: (value) { - if (value == null) return; - - context.read().add( - BoundarySelectEvent( - label: label, - selectedBoundary: value, - ), - ); - }, - valueMapper: (value) { - return value.name ?? value.code ?? 'No Value'; - }, - ), - ); - }, - ), - ), - DigitCard( - margin: const EdgeInsets.only(left: 0, right: 0, top: 10), - child: SafeArea( - child: DigitElevatedButton( - onPressed: selectedBoundary == null - ? null - : () async { - setState(() { - shouldPop = true; - }); + return ReactiveFormBuilder( + form: () => buildForm(), + builder: (context, form, child) => Column( + children: [ + Expanded( + child: ListView.builder( + itemCount: labelList.length, + itemBuilder: (context, labelIndex) { + final label = labelList.elementAt(labelIndex); + + final filteredItems = + state.boundaryList.where((element) { + if (element.label != label) return false; + + if (labelIndex == 0) return true; + final parentIndex = labelIndex - 1; + + final parentBoundaryEntry = state + .selectedBoundaryMap.entries + .elementAtOrNull(parentIndex); + final parentBoundary = parentBoundaryEntry?.value; + if (parentBoundary == null) return false; + + if (element.materializedPathList + .contains(parentBoundary.code)) { + return true; + } + + return false; + }).toList(); + + return Padding( + padding: const EdgeInsets.symmetric( + horizontal: kPadding * 2, + ), + child: DigitDropdown( + initialValue: state.selectedBoundaryMap.entries + .firstWhereOrNull( + (element) => element.key == label, + ) + ?.value, + label: label, + menuItems: filteredItems, + onChanged: (value) { + if (value == null) return; context.read().add( - const BoundarySubmitEvent(), + BoundarySelectEvent( + label: label, + selectedBoundary: value, + ), ); - - Future.delayed( - const Duration(milliseconds: 100), - () => context.router.pop(), - ); }, - child: const Text('Submit'), + valueMapper: (value) { + return value.name ?? value.code ?? 'No Value'; + }, + formControlName: _selectedBoundaryKey, + ), + ); + }, + ), + ), + DigitCard( + margin: + const EdgeInsets.only(left: 0, right: 0, top: 10), + child: SafeArea( + child: DigitElevatedButton( + onPressed: selectedBoundary == null + ? null + : () async { + setState(() { + shouldPop = true; + }); + + context.read().add( + const BoundarySubmitEvent(), + ); + + Future.delayed( + const Duration(milliseconds: 100), + () => context.router.pop(), + ); + }, + child: const Text('Submit'), + ), ), ), - ), - ], + ], + ), ); }, ), @@ -129,4 +136,15 @@ class _BoundarySelectionPageState extends State { ), ); } + + FormGroup buildForm() { + return fb.group({ + _selectedBoundaryKey: FormControl( + validators: [ + Validators.required, + ], + value: null, + ), + }); + } } diff --git a/apps/health_campaign_field_worker_app/lib/pages/login.dart b/apps/health_campaign_field_worker_app/lib/pages/login.dart index 3a9f0ff27..262c1fa66 100644 --- a/apps/health_campaign_field_worker_app/lib/pages/login.dart +++ b/apps/health_campaign_field_worker_app/lib/pages/login.dart @@ -187,12 +187,12 @@ class _LoginPageState extends LocalizedState { FormGroup buildForm() => fb.group({ _userId: FormControl( - value: '', + value: 'distNihAP1', validators: [Validators.required], ), _password: FormControl( validators: [Validators.required], - value: '', + value: 'eGov@1234', ), }); } diff --git a/packages/digit_components/lib/widgets/atoms/digit_dropdown.dart b/packages/digit_components/lib/widgets/atoms/digit_dropdown.dart index 01aeaab67..ce367c743 100644 --- a/packages/digit_components/lib/widgets/atoms/digit_dropdown.dart +++ b/packages/digit_components/lib/widgets/atoms/digit_dropdown.dart @@ -1,21 +1,26 @@ import 'package:flutter/material.dart'; +import 'package:reactive_forms/reactive_forms.dart'; class DigitDropdown extends StatelessWidget { final String label; - final T? value; + final T? initialValue; final List menuItems; + final String formControlName; final bool isRequired; - final ValueChanged? onChanged; + final ValueChanged? onChanged; final String Function(T value) valueMapper; + final Map? validationMessages; const DigitDropdown({ super.key, - required this.value, required this.label, required this.menuItems, + required this.formControlName, this.isRequired = false, required this.valueMapper, + this.initialValue, this.onChanged, + this.validationMessages, }); @override @@ -30,9 +35,14 @@ class DigitDropdown extends StatelessWidget { style: Theme.of(context).textTheme.bodyLarge, ), const SizedBox(height: 8), - DropdownButtonFormField( - value: value, - onChanged: onChanged, + ReactiveDropdownField( + onChanged: (control) { + final value = control.value; + if (value == null) return; + onChanged?.call(value); + }, + validationMessages: validationMessages, + formControlName: formControlName, decoration: const InputDecoration( contentPadding: EdgeInsets.fromLTRB(16, 12, 0, 12), ), @@ -50,3 +60,13 @@ class DigitDropdown extends StatelessWidget { ); } } + +class MenuItemModel { + final String name; + final String code; + + const MenuItemModel({ + required this.name, + required this.code, + }); +} diff --git a/packages/digit_components/lib/widgets/atoms/digit_reactive_dropdown.dart b/packages/digit_components/lib/widgets/atoms/digit_reactive_dropdown.dart index b309452f1..afc839fa6 100644 --- a/packages/digit_components/lib/widgets/atoms/digit_reactive_dropdown.dart +++ b/packages/digit_components/lib/widgets/atoms/digit_reactive_dropdown.dart @@ -60,13 +60,3 @@ class DigitReactiveDropdown extends StatelessWidget { ); } } - -class MenuItemModel { - final String name; - final String code; - - const MenuItemModel({ - required this.name, - required this.code, - }); -}