passo_mobile_app/lib/screens/profile/components/references_screen.dart

354 lines
19 KiB
Dart
Raw Permalink Normal View History

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';
2023-02-09 08:48:19 +00:00
import 'package:unit2/theme-data.dart/box_shadow.dart';
import 'package:unit2/theme-data.dart/colors.dart';
2023-02-09 08:48:19 +00:00
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/Leadings/add_leading.dart';
import 'package:unit2/widgets/Leadings/close_leading.dart';
2023-06-06 06:54:51 +00:00
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(
2023-06-06 06:54:51 +00:00
resizeToAvoidBottomInset: true,
appBar: AppBar(
2023-04-11 01:27:53 +00:00
title: context.watch<ReferencesBloc>().state is AddReferenceState
? const Text("Add Personal Reference")
: context.watch<ReferencesBloc>().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<ReferencesBloc>().state
is AddReferenceState ||
context.watch<ReferencesBloc>().state is EditReferenceState)
? [
//// close button
CloseLeading(onPressed: () {
context.read<ReferencesBloc>().add(
2023-06-06 06:54:51 +00:00
LoadReferences());
2023-04-11 01:27:53 +00:00
}),
]
:
//// if state is loaded state show add button
context.watch<ReferencesBloc>().state is ReferencesLoadedState
? [
AddLeading(onPressed: () {
context
.read<ReferencesBloc>()
.add(ShowAddReferenceForm());
}),
]
: []),
body: ProgressHUD(
padding: const EdgeInsets.all(24),
backgroundColor: Colors.black87,
indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
child: BlocBuilder<UserBloc, UserState>(
builder: (context, state) {
//userbloc
if (state is UserLoggedIn) {
token = state.userData!.user!.login!.token;
profileId = state.userData!.user!.login!.user!.profileId;
return BlocBuilder<ProfileBloc, ProfileState>(
builder: (context, state) {
//profilebloc
if (state is ProfileLoaded) {
return BlocConsumer<ReferencesBloc, ReferencesState>(
//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();
2023-06-06 06:54:51 +00:00
context
.read<ReferencesBloc>()
.add(LoadReferences());
});
} else {
errorAlert(context, "Adding Failed",
"Something went wrong. Please try again.",
() {
Navigator.of(context).pop();
2023-06-06 06:54:51 +00:00
context
.read<ReferencesBloc>()
.add(LoadReferences());
});
}
}
////EDIT REFERENCE STATE
if (state is ReferenceEditedState) {
if (state.response['success']) {
successAlert(context, "Update Successfull!",
state.response['message'], () {
Navigator.of(context).pop();
2023-06-06 06:54:51 +00:00
context
.read<ReferencesBloc>()
.add(LoadReferences());
});
} else {
errorAlert(context, "Update Failed",
"Something went wrong. Please try again.",
() {
Navigator.of(context).pop();
2023-06-06 06:54:51 +00:00
context
.read<ReferencesBloc>()
.add(LoadReferences());
});
}
}
////DELETED STATE
if (state is DeleteReferenceState) {
if (state.success) {
successAlert(context, "Deletion Successfull",
2023-06-06 06:54:51 +00:00
"Personal reference has been deleted successfully",
() {
Navigator.of(context).pop();
2023-06-06 06:54:51 +00:00
context
.read<ReferencesBloc>()
.add(LoadReferences());
});
} else {
errorAlert(context, "Deletion Failed",
2023-06-06 06:54:51 +00:00
"Error Deleting Personal reference", () {
Navigator.of(context).pop();
2023-06-06 06:54:51 +00:00
context
.read<ReferencesBloc>()
.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) {
2023-06-06 06:54:51 +00:00
String? middlename = state.references[index].middleName ?? "";
String fullname =
2023-06-06 06:54:51 +00:00
"${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
2023-06-06 06:54:51 +00:00
.w500,
color:
primary)),
const SizedBox(
2023-06-06 06:54:51 +00:00
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<int>(
offset: const Offset(-10, -10),
elevation: 3,
onSelected: (value) {
////delete eligibilty-= = = = = = = = =>>
if (value == 2) {
confirmAlert(context, () {
2023-06-06 06:54:51 +00:00
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-= = = = = = = = =>>
2023-06-06 06:54:51 +00:00
final progress =
2023-04-11 01:27:53 +00:00
ProgressHUD.of(context);
progress!.showWithText(
"Please wait...");
context
.read<ReferencesBloc>()
.add(ShowEditReferenceForm(
personalReference:
state.references[
index]));
}
},
2023-06-06 06:54:51 +00:00
menuItems: [
popMenuItem(
2023-06-06 06:54:51 +00:00
text: "Update",
value: 1,
icon: Icons.edit),
popMenuItem(
2023-06-06 06:54:51 +00:00
text: "Remove",
value: 2,
icon: Icons.delete),
2023-06-06 06:54:51 +00:00
popMenuItem(
text: "Attach",
value: 2,
icon: Icons.attach_file),
],
icon: const Icon(
Icons.more_vert,
color: Colors.grey,
),
tooltip: "Options",
)
],
),
),
const SizedBox(
height: 5,
),
],
);
});
2023-06-06 06:54:51 +00:00
}else{
return const EmptyData(message: "You don't have any references added. Please click + to add.");
}
}
if (state is AddReferenceState) {
2023-06-06 06:54:51 +00:00
return AddReferenceScreen(
profileId: profileId!,
token: token!,
);
}
if (state is EditReferenceState) {
2023-06-06 06:54:51 +00:00
return EditReferenceScreen(
profileId: profileId!,
token: token!,
);
}
if (state is ReferencesErrorState) {
return SomethingWentWrong(
2023-06-06 06:54:51 +00:00
message: state.message,
onpressed: () {
context.read<ReferencesBloc>().add(
GetReferences(
profileId: profileId!,
token: token!));
2023-04-11 01:27:53 +00:00
});
}
return Container();
},
);
}
return Container();
},
);
}
return Container();
},
),
));
}
PopupMenuItem<int> popMenuItem({String? text, int? value, IconData? icon}) {
return PopupMenuItem(
value: value,
child: Row(
children: [
Icon(
icon,
),
const SizedBox(
width: 10,
),
Text(
text!,
),
],
),
);
}
}