import 'package:app_popup_menu/app_popup_menu.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_progress_hud/flutter_progress_hud.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'package:unit2/bloc/profile/profile_bloc.dart'; import 'package:unit2/bloc/user/user_bloc.dart'; import 'package:unit2/screens/profile/components/reference/add_modal.dart'; import 'package:unit2/screens/profile/components/reference/edit_modal.dart'; import 'package:unit2/theme-data.dart/box_shadow.dart'; import 'package:unit2/theme-data.dart/colors.dart'; import 'package:unit2/utils/text_container.dart'; import 'package:unit2/widgets/Leadings/add_leading.dart'; import 'package:unit2/widgets/Leadings/close_leading.dart'; import 'package:unit2/widgets/empty_data.dart'; import 'package:unit2/widgets/error_state.dart'; import '../../../bloc/profile/references/references_bloc.dart'; import '../../../utils/alerts.dart'; class ReferencesScreen extends StatelessWidget { const ReferencesScreen({super.key}); @override Widget build(BuildContext context) { int? profileId; String? token; return Scaffold( resizeToAvoidBottomInset: true, appBar: AppBar( title: context.watch().state is AddReferenceState ? const Text("Add Personal Reference") : context.watch().state is EditReferenceState ? const Text("Edit Personal Reference") : const Text("Personal References"), centerTitle: true, backgroundColor: primary, ////if state is adding or editing state show close button actions: (context.watch().state is AddReferenceState || context.watch().state is EditReferenceState) ? [ //// close button CloseLeading(onPressed: () { context.read().add( LoadReferences()); }), ] : //// if state is loaded state show add button context.watch().state is ReferencesLoadedState ? [ AddLeading(onPressed: () { context .read() .add(ShowAddReferenceForm()); }), ] : []), body: ProgressHUD( padding: const EdgeInsets.all(24), backgroundColor: Colors.black87, indicatorWidget: const SpinKitFadingCircle(color: Colors.white), child: BlocBuilder( builder: (context, state) { //userbloc if (state is UserLoggedIn) { token = state.userData!.user!.login!.token; profileId = state.userData!.user!.login!.user!.profileId; return BlocBuilder( builder: (context, state) { //profilebloc if (state is ProfileLoaded) { return BlocConsumer( //listener listener: (context, state) { print(state); if (state is ReferencesLoadingState) { final progress = ProgressHUD.of(context); progress!.showWithText("Please wait..."); } if (state is ReferencesLoadedState || state is AddReferenceState || state is ReferencesErrorState || state is DeleteReferenceState || state is ReferencesAddedState || state is EditReferenceState) { final progress = ProgressHUD.of(context); progress!.dismiss(); } ////ADDED STATE if (state is ReferencesAddedState) { if (state.response['success']) { successAlert(context, "Adding Successfull!", state.response['message'], () { Navigator.of(context).pop(); context .read() .add(LoadReferences()); }); } else { errorAlert(context, "Adding Failed", "Something went wrong. Please try again.", () { Navigator.of(context).pop(); context .read() .add(LoadReferences()); }); } } ////EDIT REFERENCE STATE if (state is ReferenceEditedState) { if (state.response['success']) { successAlert(context, "Update Successfull!", state.response['message'], () { Navigator.of(context).pop(); context .read() .add(LoadReferences()); }); } else { errorAlert(context, "Update Failed", "Something went wrong. Please try again.", () { Navigator.of(context).pop(); context .read() .add(LoadReferences()); }); } } ////DELETED STATE if (state is DeleteReferenceState) { if (state.success) { successAlert(context, "Deletion Successfull", "Personal reference has been deleted successfully", () { Navigator.of(context).pop(); context .read() .add(LoadReferences()); }); } else { errorAlert(context, "Deletion Failed", "Error Deleting Personal reference", () { Navigator.of(context).pop(); context .read() .add(LoadReferences()); }); } } }, //builder builder: (context, state) { //references bloc if (state is ReferencesLoadedState) { if (state.references.isNotEmpty) { return ListView.builder( padding: const EdgeInsets.symmetric( vertical: 8, horizontal: 10), itemCount: state.references.length, itemBuilder: (BuildContext context, int index) { String? middlename = state.references[index].middleName ?? ""; String fullname = "${state.references[index].firstName} $middlename ${state.references[index].lastName}"; String addres = state.references[index] .address!.country?.id != 175 ? "COUNTRY: ${state.references[index].address!.country!.name!.toUpperCase()}" : "${state.references[index].address?.cityMunicipality?.description}, ${state.references[index].address?.cityMunicipality?.province?.description}, ${state.references[index].address?.cityMunicipality?.province?.region?.description}"; String mobile = state .references[index].contactNo .toString(); return Column( children: [ Container( padding: const EdgeInsets.symmetric( horizontal: 12, vertical: 8), decoration: box1(), child: Row( children: [ Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(fullname.toUpperCase(), style: Theme.of(context) .textTheme .titleMedium! .copyWith( fontWeight: FontWeight .w500, color: primary)), const SizedBox( height: 8, ), Text(addres, style: Theme.of(context) .textTheme .titleSmall! .copyWith( fontWeight: FontWeight .w500)), const SizedBox( height: 8, ), Text( "${mobileOrPhone.toUpperCase()} : $mobile", style: Theme.of(context) .textTheme .labelMedium!), ], ), ), AppPopupMenu( offset: const Offset(-10, -10), elevation: 3, onSelected: (value) { ////delete eligibilty-= = = = = = = = =>> if (value == 2) { confirmAlert(context, () { final progress = ProgressHUD.of( context); progress!.showWithText( "Please wait..."); context .read< ReferencesBloc>() .add(DeleteReference( profileId: profileId!, refId: state .references[ index] .id!, references: state .references, token: token!)); }, "Delete?", "Confirm Delete?"); } if (value == 1) { ////edit reference-= = = = = = = = =>> final progress = ProgressHUD.of(context); progress!.showWithText( "Please wait..."); context .read() .add(ShowEditReferenceForm( personalReference: state.references[ index])); } }, menuItems: [ popMenuItem( text: "Update", value: 1, icon: Icons.edit), popMenuItem( text: "Remove", value: 2, icon: Icons.delete), popMenuItem( text: "Attach", value: 2, icon: Icons.attach_file), ], icon: const Icon( Icons.more_vert, color: Colors.grey, ), tooltip: "Options", ) ], ), ), const SizedBox( height: 5, ), ], ); }); }else{ return const EmptyData(message: "You don't have any references added. Please click + to add."); } } if (state is AddReferenceState) { return AddReferenceScreen( profileId: profileId!, token: token!, ); } if (state is EditReferenceState) { return EditReferenceScreen( profileId: profileId!, token: token!, ); } if (state is ReferencesErrorState) { return SomethingWentWrong( message: state.message, onpressed: () { context.read().add( GetReferences( profileId: profileId!, token: token!)); }); } return Container(); }, ); } return Container(); }, ); } return Container(); }, ), )); } PopupMenuItem popMenuItem({String? text, int? value, IconData? icon}) { return PopupMenuItem( value: value, child: Row( children: [ Icon( icon, ), const SizedBox( width: 10, ), Text( text!, ), ], ), ); } }