Skip to content

Commit

Permalink
Merge branch 'hlm-2981-row-version' of https://github.com/egovernment…
Browse files Browse the repository at this point in the history
…s/health-campaign-field-worker-app into hlm-2981-row-version
  • Loading branch information
naveen-egov committed Aug 3, 2023
2 parents e6c4f24 + b376a98 commit 8caae85
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,15 @@ class _IndividualDetailsPageState
appConfiguration.genderOptions ??
<GenderOptions>[];

return DigitReactiveDropdown<String>(
return DigitDropdown<String>(
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,
Expand Down Expand Up @@ -501,10 +500,9 @@ class _IndividualDetailsPageState
final options =
appConfiguration.genderOptions ?? <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,
);
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,6 +17,7 @@ class BoundarySelectionPage extends StatefulWidget {

class _BoundarySelectionPageState extends State<BoundarySelectionPage> {
bool shouldPop = false;
static const _selectedBoundaryKey = 'boundary';

@override
Widget build(BuildContext context) {
Expand All @@ -37,90 +39,95 @@ class _BoundarySelectionPageState extends State<BoundarySelectionPage> {

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<BoundaryModel>(
value: state.selectedBoundaryMap.entries
.firstWhereOrNull(
(element) => element.key == label,
)
?.value,
label: label,
menuItems: filteredItems,
onChanged: (value) {
if (value == null) return;

context.read<BoundaryBloc>().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<BoundaryModel>(
initialValue: state.selectedBoundaryMap.entries
.firstWhereOrNull(
(element) => element.key == label,
)
?.value,
label: label,
menuItems: filteredItems,
onChanged: (value) {
if (value == null) return;

context.read<BoundaryBloc>().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<BoundaryBloc>().add(
const BoundarySubmitEvent(),
);

Future.delayed(
const Duration(milliseconds: 100),
() => context.router.pop(),
);
},
child: const Text('Submit'),
),
),
),
),
],
],
),
);
},
),
Expand All @@ -129,4 +136,15 @@ class _BoundarySelectionPageState extends State<BoundarySelectionPage> {
),
);
}

FormGroup buildForm() {
return fb.group(<String, Object>{
_selectedBoundaryKey: FormControl<BoundaryModel>(
validators: [
Validators.required,
],
value: null,
),
});
}
}
4 changes: 2 additions & 2 deletions apps/health_campaign_field_worker_app/lib/pages/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ class _LoginPageState extends LocalizedState<LoginPage> {

FormGroup buildForm() => fb.group(<String, Object>{
_userId: FormControl<String>(
value: '',
value: 'distNihAP1',
validators: [Validators.required],
),
_password: FormControl<String>(
validators: [Validators.required],
value: '',
value: 'eGov@1234',
),
});
}
32 changes: 26 additions & 6 deletions packages/digit_components/lib/widgets/atoms/digit_dropdown.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import 'package:flutter/material.dart';
import 'package:reactive_forms/reactive_forms.dart';

class DigitDropdown<T> extends StatelessWidget {
final String label;
final T? value;
final T? initialValue;
final List<T> menuItems;
final String formControlName;
final bool isRequired;
final ValueChanged<T?>? onChanged;
final ValueChanged<T>? onChanged;
final String Function(T value) valueMapper;
final Map<String, String Function(Object object)>? 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
Expand All @@ -30,9 +35,14 @@ class DigitDropdown<T> extends StatelessWidget {
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 8),
DropdownButtonFormField<T>(
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),
),
Expand All @@ -50,3 +60,13 @@ class DigitDropdown<T> extends StatelessWidget {
);
}
}

class MenuItemModel {
final String name;
final String code;

const MenuItemModel({
required this.name,
required this.code,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,3 @@ class DigitReactiveDropdown<T> extends StatelessWidget {
}
}

class MenuItemModel {
final String name;
final String code;

const MenuItemModel({
required this.name,
required this.code,
});
}

0 comments on commit 8caae85

Please sign in to comment.