diff --git a/lib/features/lab/lab_view_model.dart b/lib/features/lab/lab_view_model.dart index 115b28d..94ddd87 100644 --- a/lib/features/lab/lab_view_model.dart +++ b/lib/features/lab/lab_view_model.dart @@ -12,9 +12,9 @@ class LabViewModel extends ChangeNotifier { List patientLabOrders = []; - List labSuggestionsList =[]; + late List _labSuggestionsList = []; - get labSuggestions => labSuggestionsList; + List get labSuggestions => _labSuggestionsList; LabViewModel({required this.labRepo, required this.errorHandlerService}); @@ -38,6 +38,7 @@ class LabViewModel extends ChangeNotifier { patientLabOrders = apiResponse.data!; isLabOrdersLoading = false; isLabResultsLoading = false; + filterSuggestions(); notifyListeners(); if (onSuccess != null) { onSuccess(apiResponse); @@ -48,6 +49,13 @@ class LabViewModel extends ChangeNotifier { } filterSuggestions(){ + final List labels = patientLabOrders + .expand((order) => order.testDetails!) // flatten testDetails + .map((detail) => detail.description) // pick description + .whereType() // remove nulls if any + .toList(); + _labSuggestionsList = labels.toSet().toList(); // remove duplicates by converting to a set and back to a list + notifyListeners(); } } diff --git a/lib/presentation/lab/lab_orders_page.dart b/lib/presentation/lab/lab_orders_page.dart index 97e2216..ce70f9b 100644 --- a/lib/presentation/lab/lab_orders_page.dart +++ b/lib/presentation/lab/lab_orders_page.dart @@ -5,7 +5,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_staggered_animations/flutter_staggered_animations.dart'; import 'package:hmg_patient_app_new/core/app_assets.dart'; import 'package:hmg_patient_app_new/core/utils/date_util.dart'; -import 'package:hmg_patient_app_new/core/utils/size_config.dart'; import 'package:hmg_patient_app_new/core/utils/size_utils.dart'; import 'package:hmg_patient_app_new/core/utils/utils.dart'; import 'package:hmg_patient_app_new/extensions/string_extensions.dart'; @@ -15,7 +14,6 @@ import 'package:hmg_patient_app_new/generated/locale_keys.g.dart'; import 'package:hmg_patient_app_new/features/lab/lab_view_model.dart'; import 'package:hmg_patient_app_new/presentation/lab/search_lab_report.dart'; import 'package:hmg_patient_app_new/theme/colors.dart'; -import 'package:hmg_patient_app_new/widgets/bottom_sheet.dart'; import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart'; import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart'; import 'package:hmg_patient_app_new/widgets/shimmer/movies_shimmer_widget.dart'; @@ -66,8 +64,8 @@ class _LabOrdersPageState extends State { if(model.isLabOrdersLoading){ return; }else { - labSuggestions = getLabSuggestions(model); - showCommonBottomSheet(context, child: SearchLabResultsContent(), + + showCommonBottomSheet(context, child: SearchLabResultsContent(labSuggestionsList: model.labSuggestions), callBackFunc: () {}, title: LocaleKeys.searchLabReport.tr(), height: ResponsiveExtension.screenHeight, @@ -277,13 +275,6 @@ class _LabOrdersPageState extends State { return ""; } } - getLabSuggestions(LabViewModel model) { - if(model.patientLabOrders.isEmpty){ - return []; - } - return model.patientLabOrders.map((m) => m.testDetails).toList(); - } - } diff --git a/lib/presentation/lab/search_lab_report.dart b/lib/presentation/lab/search_lab_report.dart index 1c05174..d513b35 100644 --- a/lib/presentation/lab/search_lab_report.dart +++ b/lib/presentation/lab/search_lab_report.dart @@ -9,25 +9,54 @@ import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart'; import 'package:hmg_patient_app_new/widgets/input_widget.dart'; import 'package:sizer/sizer.dart'; -class SearchLabResultsContent extends StatelessWidget { - const SearchLabResultsContent({super.key}); +class SearchLabResultsContent extends StatefulWidget { + final List labSuggestionsList; - final List _chipLabels = const [ - "Blood Test", - "X-Ray", - "MRI Scan", - "CT Scan", - "Ultrasound", - "Urine Test", - "Allergy Test", - "Cholesterol Test", - "Diabetes Test", - "Thyroid Test", - ]; + const SearchLabResultsContent({super.key, required this.labSuggestionsList}); @override - Widget build(BuildContext context) { + State createState() => _SearchLabResultsContentState(); +} + +class _SearchLabResultsContentState extends State { + TextEditingController searchEditingController = TextEditingController(); + List filteredSuggestions = []; + + @override + void initState() { + super.initState(); + filteredSuggestions = List.from(widget.labSuggestionsList); + + // Listen for changes in the search field + searchEditingController.addListener(() { + filterSuggestions(); + }); + } + + @override + void dispose() { + searchEditingController.dispose(); + super.dispose(); + } + + void filterSuggestions() { + final query = searchEditingController.text.toLowerCase(); + if (query.isEmpty) { + setState(() { + filteredSuggestions = List.from(widget.labSuggestionsList); + }); + } else { + setState(() { + filteredSuggestions = widget.labSuggestionsList + .where((suggestion) => suggestion.toLowerCase().contains(query)) + .toList(); + }); + } + } + + @override + Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -37,19 +66,23 @@ class SearchLabResultsContent extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, children: [ TextInputWidget( - labelText:"Search lab results", + labelText: "Search lab results", hintText: "Type test name", - controller: TextEditingController(), + controller: searchEditingController, isEnable: true, prefix: null, autoFocus: true, isBorderAllowed: false, - padding: EdgeInsets.symmetric(vertical:ResponsiveExtension(10).h, horizontal: ResponsiveExtension(15).h), - + padding: EdgeInsets.symmetric( + vertical: ResponsiveExtension(10).h, + horizontal: ResponsiveExtension(15).h, + ), ), - SizedBox(height: ResponsiveExtension(20).h), - "Suggestions".toText16(isBold: true), - const SizedBox(height: 12), + SizedBox(height: ResponsiveExtension(20).h), + if (filteredSuggestions.isNotEmpty) ...[ + "Suggestions".toText16(isBold: true), + const SizedBox(height: 12), + ], ], ), ), @@ -60,11 +93,13 @@ class SearchLabResultsContent extends StatelessWidget { alignment: WrapAlignment.start, spacing: 10, runSpacing: 10, - children: _chipLabels + children: filteredSuggestions .map((label) => SuggestionChip( - label: label, - onTap: () {}, - )) + label: label, + onTap: () { + searchEditingController.text = label; + }, + )) .toList(), ), ), @@ -76,7 +111,7 @@ class SearchLabResultsContent extends StatelessWidget { text: LocaleKeys.search.tr(), icon: AppAssets.search_icon, iconColor: Colors.white, - onPressed: () => Navigator.pop(context), + onPressed: () => Navigator.pop(context, searchEditingController.text), ), ), ], @@ -99,12 +134,16 @@ class SuggestionChip extends StatelessWidget { @override Widget build(BuildContext context) { return GestureDetector( - onTap: onTap, // optional tap callback + onTap: onTap, child: Container( padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8), decoration: BoxDecoration( color: isSelected ? AppColors.primaryRedColor : AppColors.whiteColor, borderRadius: BorderRadius.circular(8), + border: Border.all( + color: AppColors.greyColor, + width: 1, + ), ), child: label.toText12( color: isSelected ? Colors.white : Colors.black87, @@ -113,4 +152,4 @@ class SuggestionChip extends StatelessWidget { ), ); } -} +} \ No newline at end of file