import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart'; import 'package:diplomaticquarterapp/models/InPatientServices/get_admitted_patient_response_model.dart'; import 'package:diplomaticquarterapp/models/InPatientServices/get_meal_items_schedule_response_model.dart'; import 'package:diplomaticquarterapp/models/InPatientServices/get_meals_schedule_response_model.dart'; import 'package:diplomaticquarterapp/services/clinic_services/get_clinic_service.dart'; import 'package:diplomaticquarterapp/theme/colors.dart'; import 'package:diplomaticquarterapp/uitl/app_toast.dart'; import 'package:diplomaticquarterapp/uitl/date_uitl.dart'; import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:diplomaticquarterapp/uitl/utils_new.dart'; import 'package:diplomaticquarterapp/widgets/avatar/large_avatar.dart'; import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart'; import 'package:diplomaticquarterapp/widgets/my_rich_text.dart'; import 'package:diplomaticquarterapp/widgets/others/app_expandable_notifier.dart'; import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class MealPlanPage extends StatefulWidget { const MealPlanPage({Key key}) : super(key: key); @override State createState() => _MealPlanPageState(); } class _MealPlanPageState extends State { ProjectViewModel projectViewModel; GetAdmittedPatientsResponseModel getAdmittedPatientsResponseModel; List getMealsScheduleResponseModelList = []; List getMealItemsScheduleResponseModelBreakfast = []; List getMealItemsScheduleResponseModelLunch = []; List getMealItemsScheduleResponseModelDinner = []; List _getMealItemsScheduleResponseModelBreakfast = []; List _getMealItemsScheduleResponseModelLunch = []; List _getMealItemsScheduleResponseModelDinner = []; int currentDay = 0; int currentWeek = 0; int selectedMealScheduleID = 0; final ValueNotifier isFasting = ValueNotifier(false); bool isFastingVal = false; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { getAdmittedPatientDetails(); }); } @override Widget build(BuildContext context) { projectViewModel = Provider.of(context); return AppScaffold( isShowAppBar: true, isShowDecPage: false, showNewAppBarTitle: true, showNewAppBar: true, appBarTitle: TranslationBase.of(context).mealPlanTitle, body: SingleChildScrollView( child: Padding( padding: EdgeInsets.all(21.0), child: getMealsScheduleResponseModelList.length != 0 ? Column( children: [ Container( decoration: containerRadius((getMealsScheduleResponseModelList[0].isShow) ? Colors.white : Colors.grey[300], 12), child: AppExpandableNotifier( title: "Breakfast TAT: " + DateUtil.convertTime(getMealsScheduleResponseModelList[0].tATTime), isTitleSingleLine: false, widgetColor: Colors.transparent, isExpand: getMealsScheduleResponseModelList[0].isShow, isDisabled: !getMealsScheduleResponseModelList[0].isShow, bodyWidget: Column( children: [ getDivider(), ListView.separated( physics: BouncingScrollPhysics(), shrinkWrap: true, itemBuilder: (context, index) { return Container( padding: EdgeInsets.all(21.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(_getMealItemsScheduleResponseModelBreakfast[index].filterName, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 18.0, letterSpacing: -0.64, color: CustomColors.darkGrey)), SizedBox( height: 12.0, ), ListView.separated( physics: NeverScrollableScrollPhysics(), shrinkWrap: true, itemBuilder: (context, index2) { return Container( child: Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ ValueListenableBuilder( valueListenable: _getMealItemsScheduleResponseModelBreakfast[index].selectedItemID, builder: (context, val, child) { return Radio( value: _getMealItemsScheduleResponseModelBreakfast[index].getMealItemsScheduleResponseModelList[index2].itemID, groupValue: _getMealItemsScheduleResponseModelBreakfast[index].selectedItemID.value, activeColor: Colors.red[800], onChanged: (value) { _getMealItemsScheduleResponseModelBreakfast[index].selectedItemID.value = value; }, ); }), LargeAvatar( name: _getMealItemsScheduleResponseModelBreakfast[index].getMealItemsScheduleResponseModelList[index2].description, url: "https://www.pumpkinnspice.com/wp-content/uploads/2016/03/easy-breakfast-quesadillas-11-683x1024.jpg", width: 60, height: 60, radius: 5, disableProfileView: true, ), SizedBox(width: 11), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ MyRichText( "", projectViewModel.isArabic ? _getMealItemsScheduleResponseModelBreakfast[index].getMealItemsScheduleResponseModelList[index2].descriptionN : _getMealItemsScheduleResponseModelBreakfast[index].getMealItemsScheduleResponseModelList[index2].description, projectViewModel.isArabic), ], ), ), ], ), ); }, separatorBuilder: (context, index) { return Padding( padding: EdgeInsets.all(16.0), child: Divider( height: 1.5, thickness: 1.0, color: Colors.grey.shade200, ), ); }, itemCount: _getMealItemsScheduleResponseModelBreakfast[index].getMealItemsScheduleResponseModelList.length), ], ), ); }, separatorBuilder: (context, index) { return Padding( padding: EdgeInsets.only(left: 15, right: 15), child: Divider( height: 1.5, thickness: 1.0, color: Colors.grey.shade200, ), ); }, itemCount: _getMealItemsScheduleResponseModelBreakfast.length), ], ), ), ), mHeight(21), Container( decoration: containerRadius(getMealsScheduleResponseModelList[1].isShow ? Colors.white : Colors.grey[300], 12), child: AppExpandableNotifier( title: "Lunch TAT: " + DateUtil.convertTime(getMealsScheduleResponseModelList[1].tATTime), isTitleSingleLine: false, widgetColor: Colors.transparent, isExpand: getMealsScheduleResponseModelList[1].isShow, isDisabled: !getMealsScheduleResponseModelList[1].isShow, bodyWidget: Column( children: [ getDivider(), ListView.separated( physics: BouncingScrollPhysics(), shrinkWrap: true, itemBuilder: (context, index) { return Container( padding: EdgeInsets.all(21.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(_getMealItemsScheduleResponseModelLunch[index].filterName, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 18.0, letterSpacing: -0.64, color: CustomColors.darkGrey)), SizedBox( height: 12.0, ), ListView.separated( physics: NeverScrollableScrollPhysics(), shrinkWrap: true, itemBuilder: (context, index2) { return Container( child: Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ ValueListenableBuilder( valueListenable: _getMealItemsScheduleResponseModelLunch[index].selectedItemID, builder: (context, val, child) { return Radio( value: _getMealItemsScheduleResponseModelLunch[index].getMealItemsScheduleResponseModelList[index2].itemID, groupValue: _getMealItemsScheduleResponseModelLunch[index].selectedItemID.value, activeColor: Colors.red[800], onChanged: (value) { _getMealItemsScheduleResponseModelLunch[index].selectedItemID.value = value; }, ); }), LargeAvatar( name: _getMealItemsScheduleResponseModelLunch[index].getMealItemsScheduleResponseModelList[index2].description, url: "https://www.pumpkinnspice.com/wp-content/uploads/2016/03/easy-breakfast-quesadillas-11-683x1024.jpg", width: 60, height: 60, radius: 5, disableProfileView: true, ), SizedBox(width: 11), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ MyRichText( "", projectViewModel.isArabic ? _getMealItemsScheduleResponseModelLunch[index].getMealItemsScheduleResponseModelList[index2].descriptionN : _getMealItemsScheduleResponseModelLunch[index].getMealItemsScheduleResponseModelList[index2].description, projectViewModel.isArabic), ], ), ), ], ), ); }, separatorBuilder: (context, index) { return Padding( padding: EdgeInsets.all(16.0), child: Divider( height: 1.5, thickness: 1.0, color: Colors.grey.shade200, ), ); }, itemCount: _getMealItemsScheduleResponseModelLunch[index].getMealItemsScheduleResponseModelList.length), ], ), ); }, separatorBuilder: (context, index) { return Padding( padding: EdgeInsets.only(left: 15, right: 15), child: Divider( height: 1.5, thickness: 1.0, color: Colors.grey.shade200, ), ); }, itemCount: _getMealItemsScheduleResponseModelLunch.length), ], ), ), ), mHeight(21), Container( decoration: containerRadius(getMealsScheduleResponseModelList[2].isShow ? Colors.white : Colors.grey[300], 12), child: AppExpandableNotifier( title: "Dinner TAT: " + DateUtil.convertTime(getMealsScheduleResponseModelList[2].tATTime), isTitleSingleLine: false, widgetColor: Colors.transparent, isExpand: getMealsScheduleResponseModelList[2].isShow, isDisabled: !getMealsScheduleResponseModelList[2].isShow, bodyWidget: Column( children: [ getDivider(), ListView.separated( physics: BouncingScrollPhysics(), shrinkWrap: true, itemBuilder: (context, index) { return Container( padding: EdgeInsets.all(21.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(_getMealItemsScheduleResponseModelDinner[index].filterName, style: TextStyle(fontWeight: FontWeight.w600, fontSize: 18.0, letterSpacing: -0.64, color: CustomColors.darkGrey)), SizedBox( height: 12.0, ), ListView.separated( physics: NeverScrollableScrollPhysics(), shrinkWrap: true, itemBuilder: (context, index2) { return Container( child: Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ ValueListenableBuilder( valueListenable: _getMealItemsScheduleResponseModelDinner[index].selectedItemID, builder: (context, val, child) { return Radio( value: _getMealItemsScheduleResponseModelDinner[index].getMealItemsScheduleResponseModelList[index2].itemID, groupValue: _getMealItemsScheduleResponseModelDinner[index].selectedItemID.value, activeColor: Colors.red[800], onChanged: (value) { _getMealItemsScheduleResponseModelDinner[index].selectedItemID.value = value; }, ); }), LargeAvatar( name: _getMealItemsScheduleResponseModelDinner[index].getMealItemsScheduleResponseModelList[index2].description, url: "https://www.pumpkinnspice.com/wp-content/uploads/2016/03/easy-breakfast-quesadillas-11-683x1024.jpg", width: 60, height: 60, radius: 5, disableProfileView: true, ), SizedBox(width: 11), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ MyRichText( "", projectViewModel.isArabic ? _getMealItemsScheduleResponseModelDinner[index].getMealItemsScheduleResponseModelList[index2].descriptionN : _getMealItemsScheduleResponseModelDinner[index].getMealItemsScheduleResponseModelList[index2].description, projectViewModel.isArabic), ], ), ), ], ), ); }, separatorBuilder: (context, index) { return Padding( padding: EdgeInsets.all(16.0), child: Divider( height: 1.5, thickness: 1.0, color: Colors.grey.shade200, ), ); }, itemCount: _getMealItemsScheduleResponseModelDinner[index].getMealItemsScheduleResponseModelList.length), ], ), ); }, separatorBuilder: (context, index) { return Padding( padding: EdgeInsets.only(left: 15, right: 15), child: Divider( height: 1.5, thickness: 1.0, color: Colors.grey.shade200, ), ); }, itemCount: _getMealItemsScheduleResponseModelDinner.length), ], ), ), ), ], ) : Container(), ), ), bottomSheet: Container( color: Colors.white, width: double.infinity, padding: EdgeInsets.all(20), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ ValueListenableBuilder( valueListenable: isFasting, builder: (context, val, child) { return Checkbox( onChanged: (bool value) { isFasting.value = value; isFastingVal = value; }, value: isFastingVal, activeColor: Color(0xFFc5272d), ); }), Text( TranslationBase.of(context).isFasting, style: TextStyle( fontSize: 14, letterSpacing: -0.48, ), ), ], ), mHeight(12), DefaultButton( TranslationBase.of(context).placeOrder, () { placeOrder(); }, color: Color(0xff359846), disabledColor: Colors.grey, ), ], ), ), ); } void placeOrder() { List selectedItemIDs = []; if (_getMealItemsScheduleResponseModelBreakfast.isNotEmpty) { _getMealItemsScheduleResponseModelBreakfast.forEach((element) { selectedItemIDs.add(element.selectedItemID.value.toString()); }); } if (_getMealItemsScheduleResponseModelLunch.isNotEmpty) { _getMealItemsScheduleResponseModelLunch.forEach((element) { selectedItemIDs.add(element.selectedItemID.value.toString()); }); } if (_getMealItemsScheduleResponseModelDinner.isNotEmpty) { _getMealItemsScheduleResponseModelDinner.forEach((element) { selectedItemIDs.add(element.selectedItemID.value.toString()); }); } ClinicListService service = new ClinicListService(); GifLoaderDialogUtils.showMyDialog(context); service .placeMealPlanOrder(getAdmittedPatientsResponseModel.projectID, selectedMealScheduleID, selectedItemIDs.join(","), projectViewModel.user.patientID, getAdmittedPatientsResponseModel.admissionNo, getAdmittedPatientsResponseModel.isHasCompanion, isFastingVal, context) .then((res) { GifLoaderDialogUtils.hideDialog(context); AppToast.showSuccessToast(message: "Your meal order has been received successfully"); Navigator.pop(context); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } Widget getDivider() { return Padding( padding: EdgeInsets.only(left: 15, right: 15), child: Divider( height: 1.5, thickness: 2.0, color: Colors.black, ), ); } void getAdmittedPatientDetails() { ClinicListService service = new ClinicListService(); GifLoaderDialogUtils.showMyDialog(context); service.getAdmittedPatientDetails(projectViewModel.user.patientID, projectViewModel.inPatientProjectID, projectViewModel.getAdmissionInfoResponseModel.admissionNo, 0, 0, 0, context).then((res) { GifLoaderDialogUtils.hideDialog(context); if (res["List_MOP_AdmittedPatients"] != null && res["List_MOP_AdmittedPatients"].length != 0) { print(res["List_MOP_AdmittedPatients"]); getAdmittedPatientsResponseModel = new GetAdmittedPatientsResponseModel.fromJson(res["List_MOP_AdmittedPatients"][0]); print(getAdmittedPatientsResponseModel.dietType); getCurrentWeekIDAndDay(getAdmittedPatientsResponseModel.dietType); } else { AppToast.showErrorToast(message: "No data found for admitted patient"); getCurrentWeekIDAndDay(1); } }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } void getCurrentWeekIDAndDay(int dietType) { ClinicListService service = new ClinicListService(); GifLoaderDialogUtils.showMyDialog(context); service.getCurrentWeekIDAndDay(context).then((res) { GifLoaderDialogUtils.hideDialog(context); currentDay = res["CurrentDay"]; currentWeek = res["CurrentWeek"]; getMealsOfScheduleID(dietType); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } void getMealsOfScheduleID(int dietType) { ClinicListService service = new ClinicListService(); GifLoaderDialogUtils.showMyDialog(context); service.getMealsOfScheduleID(projectViewModel.inPatientProjectID, projectViewModel.getAdmissionInfoResponseModel.admissionNo, dietType, currentWeek, currentDay, context).then((res) { GifLoaderDialogUtils.hideDialog(context); if (res["List_MOP_MealsOfScheduleID_Mobile"] != null && res["List_MOP_MealsOfScheduleID_Mobile"].length != 0) { res['List_MOP_MealsOfScheduleID_Mobile'].forEach((scheduleItem) { getMealsScheduleResponseModelList.add(GetMealsScheduleResponseModel.fromJson(scheduleItem)); }); if (getMealsScheduleResponseModelList[0].isShow) { selectedMealScheduleID = getMealsScheduleResponseModelList[0].scheduleID; getDefaultItemsOfScheduleID(getMealsScheduleResponseModelList[0].scheduleID, 1); } if (getMealsScheduleResponseModelList[1].isShow) { selectedMealScheduleID = getMealsScheduleResponseModelList[1].scheduleID; getDefaultItemsOfScheduleID(getMealsScheduleResponseModelList[1].scheduleID, 2); } if (getMealsScheduleResponseModelList[2].isShow) { selectedMealScheduleID = getMealsScheduleResponseModelList[2].scheduleID; getDefaultItemsOfScheduleID(getMealsScheduleResponseModelList[2].scheduleID, 3); } } else { AppToast.showErrorToast(message: "No meal schedule found for admitted patient"); } }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } void getDefaultItemsOfScheduleID(int scheduleID, int mealType) { ClinicListService service = new ClinicListService(); GifLoaderDialogUtils.showMyDialog(context); service.getDefaultItemsOfScheduleID(projectViewModel.inPatientProjectID, scheduleID, context).then((res) { GifLoaderDialogUtils.hideDialog(context); if (res["List_MOP_DefaultItemsOfMealModel"] != null && res["List_MOP_DefaultItemsOfMealModel"].length != 0) { if (mealType == 1) { res['List_MOP_DefaultItemsOfMealModel'].forEach((scheduleItem) { getMealItemsScheduleResponseModelBreakfast.add(GetMealItemsScheduleResponseModel.fromJson(scheduleItem)); }); sortMealsByCategoryBreakfast(); } if (mealType == 2) { res['List_MOP_DefaultItemsOfMealModel'].forEach((scheduleItem) { getMealItemsScheduleResponseModelLunch.add(GetMealItemsScheduleResponseModel.fromJson(scheduleItem)); }); sortMealsByCategoryLunch(); } if (mealType == 3) { res['List_MOP_DefaultItemsOfMealModel'].forEach((scheduleItem) { getMealItemsScheduleResponseModelDinner.add(GetMealItemsScheduleResponseModel.fromJson(scheduleItem)); }); sortMealsByCategoryDinner(); } } else { AppToast.showErrorToast(message: "No meal items found for admitted patient"); } setState(() {}); }).catchError((err) { GifLoaderDialogUtils.hideDialog(context); print(err); }); } void sortMealsByCategoryBreakfast() { getMealItemsScheduleResponseModelBreakfast.forEach((element) { List breakfastMealItems = _getMealItemsScheduleResponseModelBreakfast .where((elementClinic) => elementClinic.filterName == (projectViewModel.isArabic ? element.categoryDescriptionN : element.categoryDescription)) .toList(); if (breakfastMealItems.length != 0) { _getMealItemsScheduleResponseModelBreakfast[_getMealItemsScheduleResponseModelBreakfast.indexOf(breakfastMealItems[0])].getMealItemsScheduleResponseModelList.add(element); } else { _getMealItemsScheduleResponseModelBreakfast .add(GetMealItemsScheduleResponseModelList(filterName: projectViewModel.isArabic ? element.categoryDescriptionN : element.categoryDescription, getMealItemsScheduleResponseModel: element)); } }); _getMealItemsScheduleResponseModelBreakfast.forEach((element) { element.getMealItemsScheduleResponseModelList.forEach((elementInner) { if (elementInner.isDefault) { element.selectedItemID.value = elementInner.itemID; } }); }); } void sortMealsByCategoryLunch() { getMealItemsScheduleResponseModelLunch.forEach((element) { List breakfastMealItems = _getMealItemsScheduleResponseModelLunch .where((elementClinic) => elementClinic.filterName == (projectViewModel.isArabic ? element.categoryDescriptionN : element.categoryDescription)) .toList(); if (breakfastMealItems.length != 0) { _getMealItemsScheduleResponseModelLunch[_getMealItemsScheduleResponseModelLunch.indexOf(breakfastMealItems[0])].getMealItemsScheduleResponseModelList.add(element); } else { _getMealItemsScheduleResponseModelLunch .add(GetMealItemsScheduleResponseModelList(filterName: projectViewModel.isArabic ? element.categoryDescriptionN : element.categoryDescription, getMealItemsScheduleResponseModel: element)); } }); _getMealItemsScheduleResponseModelLunch.forEach((element) { element.getMealItemsScheduleResponseModelList.forEach((elementInner) { if (elementInner.isDefault) { element.selectedItemID.value = elementInner.itemID; } }); }); } void sortMealsByCategoryDinner() { getMealItemsScheduleResponseModelDinner.forEach((element) { List breakfastMealItems = _getMealItemsScheduleResponseModelDinner .where((elementClinic) => elementClinic.filterName == (projectViewModel.isArabic ? element.categoryDescriptionN : element.categoryDescription)) .toList(); if (breakfastMealItems.length != 0) { _getMealItemsScheduleResponseModelDinner[_getMealItemsScheduleResponseModelDinner.indexOf(breakfastMealItems[0])].getMealItemsScheduleResponseModelList.add(element); } else { _getMealItemsScheduleResponseModelDinner .add(GetMealItemsScheduleResponseModelList(filterName: projectViewModel.isArabic ? element.categoryDescriptionN : element.categoryDescription, getMealItemsScheduleResponseModel: element)); } }); _getMealItemsScheduleResponseModelDinner.forEach((element) { element.getMealItemsScheduleResponseModelList.forEach((elementInner) { if (elementInner.isDefault) { element.selectedItemID.value = elementInner.itemID; } }); }); } }