passo_mobile_app/lib/screens/profile/components/eligibility/edit_modal.dart

548 lines
28 KiB
Dart
Raw Normal View History

2023-02-15 03:40:12 +00:00
import 'package:date_time_picker/date_time_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2023-02-15 03:40:12 +00:00
import 'package:flutter_form_builder/flutter_form_builder.dart';
2023-04-11 01:27:53 +00:00
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
2023-04-05 00:54:24 +00:00
import 'package:form_builder_validators/form_builder_validators.dart';
2023-02-15 03:40:12 +00:00
import 'package:intl/intl.dart';
2023-02-23 05:51:53 +00:00
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
2023-02-20 07:48:24 +00:00
import 'package:unit2/model/location/city.dart';
2023-02-15 03:40:12 +00:00
import 'package:unit2/model/profile/eligibility.dart';
2023-02-20 07:48:24 +00:00
import 'package:unit2/model/utils/eligibility.dart';
2023-02-23 00:53:14 +00:00
import 'package:unit2/utils/location_utilities.dart';
import '../../../../bloc/profile/eligibility/eligibility_bloc.dart';
2023-02-20 07:48:24 +00:00
import '../../../../model/location/country.dart';
import '../../../../model/location/region.dart';
import '../../../../model/location/provinces.dart';
2023-02-15 03:40:12 +00:00
import '../../../../theme-data.dart/btn-style.dart';
import '../../../../theme-data.dart/colors.dart';
import '../../../../theme-data.dart/form-style.dart';
import '../../../../utils/global.dart';
import '../../../../utils/text_container.dart';
class EditEligibilityScreen extends StatefulWidget {
final EligibityCert eligibityCert;
2023-04-11 01:27:53 +00:00
final int profileId;
final String token;
2023-06-06 06:54:51 +00:00
const EditEligibilityScreen(
{super.key,
required this.eligibityCert,
required this.profileId,
required this.token});
2023-02-15 03:40:12 +00:00
@override
State<EditEligibilityScreen> createState() => _EditEligibilityScreenState();
}
class _EditEligibilityScreenState extends State<EditEligibilityScreen> {
final formKey = GlobalKey<FormBuilderState>();
2023-02-20 07:48:24 +00:00
bool? overseas;
List<Province>? provinces;
List<CityMunicipality>? citymuns;
List<Region>? regions;
DateFormat dteFormat2 = DateFormat.yMMMMd('en_US');
2023-02-23 00:53:14 +00:00
Region? selectedRegion;
Province? selectedProvince;
CityMunicipality? selectedMunicipality;
2023-02-20 07:48:24 +00:00
Country? selectedCountry;
2023-02-23 00:53:14 +00:00
Eligibility? selectedEligibility;
2023-02-23 05:51:53 +00:00
bool provinceCall = false;
bool cityCall = false;
String? token;
String? profileId;
String? rating;
String? license;
final examDateController = TextEditingController();
final validityDateController = TextEditingController();
2023-06-06 06:54:51 +00:00
2023-02-15 03:40:12 +00:00
@override
2023-06-06 06:54:51 +00:00
void dispose() {
examDateController.dispose();
validityDateController.dispose();
super.dispose();
}
2023-04-11 01:27:53 +00:00
2023-06-06 06:54:51 +00:00
@override
Widget build(BuildContext context) {
return BlocBuilder<EligibilityBloc, EligibilityState>(
buildWhen: (previous, current) {
return false;
},
builder: (context, state) {
//EDIT ELIGIBILITY STATE
if (state is EditEligibilityState) {
examDateController.text = state.eligibityCert.examDate == null
? ''
: state.eligibityCert.examDate.toString();
validityDateController.text = state.eligibityCert.validityDate == null
? ''
: state.eligibityCert.validityDate.toString();
DateTime? examDate = DateTime.tryParse(examDateController.text) ;
DateTime? expireDate = DateTime.tryParse(validityDateController.text);
provinces = state.provinces;
citymuns = state.cities;
regions = state.regions;
overseas = state.isOverseas;
selectedRegion = state.currentRegion;
selectedProvince = state.currentProvince;
selectedMunicipality = state.currentCity;
selectedEligibility = state.currentEligibility;
rating = state.eligibityCert.rating?.toString();
license = state.eligibityCert.licenseNumber;
selectedCountry = state.selectedCountry;
return Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 25, horizontal: 28),
child: FormBuilder(
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox(
height: 24,
),
////ELIGIBILITIES DROPDOWN
DropdownButtonFormField<Eligibility>(
validator: (value) =>
value == null ? 'required' : null,
isExpanded: true,
onChanged: (Eligibility? eligibility) {
selectedEligibility = eligibility;
},
value: selectedEligibility,
items: state.eligibilities
.map<DropdownMenuItem<Eligibility>>(
(Eligibility eligibility) {
return DropdownMenuItem<Eligibility>(
value: eligibility,
child: Text(eligibility.title));
}).toList(),
decoration: normalTextFieldStyle("Eligibility", "")),
const SizedBox(
height: 12,
),
2023-02-16 07:10:54 +00:00
2023-06-06 06:54:51 +00:00
SizedBox(
width: screenWidth,
child: Row(
children: [
////LICENSE NUMBER
Flexible(
flex: 1,
child: FormBuilderTextField(
onChanged: (value) {
license = value;
},
name: 'license_number',
initialValue: license,
decoration: normalTextFieldStyle(
"license number", "license number"),
),
),
const SizedBox(
width: 12,
),
// //RATING
Flexible(
flex: 1,
child: FormBuilderTextField(
validator: FormBuilderValidators.numeric(
errorText: "Enter a number"),
keyboardType:
const TextInputType.numberWithOptions(),
onChanged: (value) {
rating = value;
},
name: 'rating',
initialValue:
rating == null ? 'N/A' : rating.toString(),
decoration:
normalTextFieldStyle('rating', 'rating'),
),
),
],
),
),
const SizedBox(
height: 12,
),
SizedBox(
width: screenWidth,
child: StatefulBuilder(builder: (context, setState) {
return Row(
children: [
// //EXAM DATE
Flexible(
flex: 1,
child: DateTimePicker(
use24HourFormat: false,
controller: examDateController,
firstDate: DateTime(1990),
lastDate: DateTime(2100),
decoration:
normalTextFieldStyle("Exam date", "")
.copyWith(
prefixIcon: const Icon(
Icons.date_range,
color: Colors.black87,
)),
initialDate: expireDate == null
? DateTime.now()
: expireDate!
.subtract(const Duration(days: 1)),
selectableDayPredicate: (date) {
if (expireDate != null &&
expireDate!.microsecondsSinceEpoch <=
date.microsecondsSinceEpoch) {
return false;
}
return true;
},
2023-06-06 06:54:51 +00:00
onChanged: (value) {
setState(() {
examDate = DateTime.parse(value);
});
},
)),
2023-06-06 06:54:51 +00:00
const SizedBox(
width: 12,
),
////VALIDITY DATE
Flexible(
flex: 1,
child: DateTimePicker(
use24HourFormat: false,
controller: validityDateController,
firstDate: DateTime(1970),
lastDate: DateTime(2100),
decoration:
normalTextFieldStyle("validity date", "")
.copyWith(
prefixIcon: const Icon(
Icons.date_range,
color: Colors.black87,
)),
selectableDayPredicate: (date) {
if (examDate != null &&
examDate!.microsecondsSinceEpoch >=
date.microsecondsSinceEpoch) {
return false;
}
return true;
},
onChanged: (value) {
setState(() {
expireDate = DateTime.parse(value);
});
},
initialDate: examDate == null
? DateTime.now()
: examDate!.add(const Duration(days: 1)),
),
2023-06-06 06:54:51 +00:00
),
],
);
}),
),
const SizedBox(
height: 20,
),
Text(
"Placement of Examination/Confinement",
style: Theme.of(context)
.textTheme
.displaySmall!
.copyWith(fontSize: blockSizeVertical * 2),
),
const SizedBox(
height: 12,
),
//OVERSEAS ADDRESS SWITCH
StatefulBuilder(builder: (context, StateSetter setState) {
return Column(
children: [
FormBuilderSwitch(
initialValue: overseas,
activeColor: second,
onChanged: (value) {
setState(() {
overseas = value;
});
},
decoration: normalTextFieldStyle("", ''),
name: 'overseas',
title: const Text("Overseas Address?"),
),
const SizedBox(
height: 12,
),
//COUNTRY DROPDOWN
SizedBox(
child: overseas == true
? FormBuilderDropdown<Country>(
validator: (value) =>
value == null ? 'required' : null,
initialValue: selectedCountry!.id == 175
? null
: selectedCountry,
items: state.countries
.map<DropdownMenuItem<Country>>(
(Country country) {
return DropdownMenuItem<Country>(
value: country,
child: FittedBox(
child: Text(country.name!)));
}).toList(),
name: 'country',
decoration: normalTextFieldStyle(
"Country*", "Country"),
onChanged: (Country? value) {
selectedCountry = value;
},
2023-06-06 06:54:51 +00:00
)
: Column(
children: [
////REGION DROPDOWN
DropdownButtonFormField<Region?>(
validator: (value) => value == null
? 'required'
: null,
isExpanded: true,
onChanged: (Region? region) async {
setState(() {
provinceCall = true;
});
selectedRegion = region;
provinces = await LocationUtils
.instance
.getProvinces(
regionCode:
selectedRegion!.code
.toString());
selectedProvince = provinces![0];
setState(() {
provinceCall = false;
cityCall = true;
});
citymuns = await LocationUtils
.instance
.getCities(
code: selectedProvince!
.code!);
selectedMunicipality =
citymuns![0];
setState(() {
cityCall = false;
});
},
value: selectedRegion,
decoration: normalTextFieldStyle(
"Region*", "Region"),
items: regions == null
? []
: regions!.map<
DropdownMenuItem<
Region>>(
(Region region) {
return DropdownMenuItem<
Region>(
value: region,
child: Text(region
.description!));
}).toList(),
),
const SizedBox(
height: 12,
),
////PROVINCE DROPDOWN
SizedBox(
height: 60,
child: ModalProgressHUD(
color: Colors.transparent,
inAsyncCall: provinceCall,
child: DropdownButtonFormField<
Province?>(
validator: (value) =>
value == null
? 'required'
: null,
2023-06-06 06:54:51 +00:00
isExpanded: true,
value: selectedProvince,
onChanged: (Province?
province) async {
setState(() {
cityCall = true;
});
selectedProvince = province;
citymuns = await LocationUtils
.instance
.getCities(
code:
selectedProvince!
.code
.toString());
selectedMunicipality =
citymuns![0];
setState(() {
cityCall = false;
});
},
2023-06-06 06:54:51 +00:00
items: provinces == null
? []
: provinces!.map<
DropdownMenuItem<
2023-06-06 06:54:51 +00:00
Province>>(
(Province province) {
return DropdownMenuItem(
value: province,
child: FittedBox(
child: Text(province
.description!),
));
}).toList(),
decoration:
normalTextFieldStyle(
"Province*",
"Province")),
),
),
2023-02-23 05:51:53 +00:00
2023-06-06 06:54:51 +00:00
//// City municipality
SizedBox(
height: 60,
child: ModalProgressHUD(
color: Colors.transparent,
inAsyncCall: cityCall,
child: DropdownButtonFormField<
CityMunicipality>(
validator: (value) =>
value == null
? 'required'
: null,
isExpanded: true,
onChanged:
(CityMunicipality? city) {
selectedMunicipality = city;
},
decoration:
normalTextFieldStyle(
"Municipality*",
"Municipality"),
value: selectedMunicipality,
items: citymuns == null
? []
: citymuns!.map<
DropdownMenuItem<
CityMunicipality>>(
(CityMunicipality c) {
return DropdownMenuItem(
value: c,
child: Text(c
.description!));
}).toList(),
),
),
),
const SizedBox(
height: 20,
),
],
)),
],
);
}),
2023-06-06 06:54:51 +00:00
const Expanded(
child: SizedBox(),
),
2023-02-20 07:48:24 +00:00
2023-06-06 06:54:51 +00:00
SizedBox(
width: screenWidth,
height: 60,
child: ElevatedButton(
style: mainBtnStyle(
primary, Colors.transparent, second),
onPressed: () {
ExamAddress examAddress;
////rating
double? rate =
rating == null ? null : double.parse(rating!);
////license
String? newLicense = license;
////city municipality
CityMunicipality? cityMunicipality =
selectedMunicipality;
////exam date
DateTime? examDate =
examDateController.text.isEmpty
? null
: DateTime.parse(examDateController.text);
// // validity date
DateTime? validityDate = validityDateController
.text.isEmpty
? null
: DateTime.parse(validityDateController.text);
//// exam address
if (overseas!) {
examAddress = ExamAddress(
barangay: null,
id: state.eligibityCert.examAddress?.id,
addressCategory: state.eligibityCert
.examAddress?.addressCategory,
examAddressClass: state.eligibityCert
.examAddress?.examAddressClass,
country: selectedCountry,
cityMunicipality: null);
} else {
examAddress = ExamAddress(
barangay: state
.eligibityCert.examAddress?.barangay,
id: state.eligibityCert.examAddress?.id,
addressCategory: state.eligibityCert
.examAddress?.addressCategory,
examAddressClass: state.eligibityCert
.examAddress?.examAddressClass,
country: Country(
id: 175,
name: 'Philippines',
code: 'PH'),
cityMunicipality: cityMunicipality);
}
EligibityCert eligibityCert = EligibityCert(
id: state.eligibityCert.id,
rating: rate,
examDate: examDate,
attachments: null,
eligibility: selectedEligibility,
examAddress: examAddress,
validityDate: validityDate,
licenseNumber: newLicense,
overseas: overseas);
if (formKey.currentState!.saveAndValidate()) {
final progress = ProgressHUD.of(context);
progress!.showWithText("Loading...");
context.read<EligibilityBloc>().add(
UpdateEligibility(
eligibityCert: eligibityCert,
oldEligibility:
state.eligibityCert.eligibility!.id,
profileId: widget.profileId.toString(),
token: widget.token));
}
},
child: const Text(submit)),
),
2023-06-06 06:54:51 +00:00
]),
),
),
);
}
return Container();
},
);
2023-02-15 03:40:12 +00:00
}
}