passo_mobile_app/lib/screens/profile/components/reference/add_modal.dart

550 lines
31 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/bloc/profile/references/references_bloc.dart';
import 'package:unit2/bloc/user/user_bloc.dart';
import 'package:unit2/model/location/address_category.dart';
import 'package:unit2/model/location/barangay.dart';
import 'package:unit2/model/profile/references.dart';
import 'package:unit2/theme-data.dart/btn-style.dart';
import 'package:unit2/theme-data.dart/form-style.dart';
import '../../../../model/location/city.dart';
import '../../../../model/location/country.dart';
import '../../../../model/location/provinces.dart';
import '../../../../model/location/region.dart';
import '../../../../theme-data.dart/colors.dart';
import '../../../../utils/location_utilities.dart';
import '../../../../utils/text_container.dart';
class AddReferenceScreen extends StatefulWidget {
const AddReferenceScreen({super.key});
@override
State<AddReferenceScreen> createState() => _AddReferenceScreenState();
}
class _AddReferenceScreenState extends State<AddReferenceScreen> {
final formKey = GlobalKey<FormBuilderState>();
String? token;
String? profileId;
bool provinceCall = false;
bool cityCall = false;
bool barangayCall = false;
bool overseas = false;
List<Province>? provinces;
List<CityMunicipality>? citymuns;
List<Barangay>? barangays;
List<String> category = ['Permanent', "Residential", "Birthplace"];
////seletected
Region? selectedRegion;
Province? selectedProvince;
CityMunicipality? selectedMunicipality;
Barangay? selectedBarangay;
Country? selectedCountry;
AddressCategory? selectedCategory;
@override
Widget build(BuildContext context) {
return BlocBuilder<UserBloc, UserState>(
builder: (context, state) {
if (state is UserLoggedIn) {
token = state.userData!.user!.login!.token;
profileId = state.userData!.user!.login!.user!.profileId.toString();
return BlocBuilder<ProfileBloc, ProfileState>(
builder: (context, state) {
if (state is ProfileLoaded) {
return BlocBuilder<ReferencesBloc, ReferencesState>(
builder: (context, state) {
if (state is AddReferenceState) {
return ProgressHUD(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 25, horizontal: 18),
child: FormBuilder(
key: formKey,
child: Column(
children: [
const SizedBox(height: 25),
Row(
children: [
////LAST NAME
Flexible(
flex: 1,
child: FormBuilderTextField(
decoration: normalTextFieldStyle(
"Last name *", "Last name *"),
name: "lastname",
validator:
FormBuilderValidators.required(
errorText:
"This field is required"),
),
),
const SizedBox(
width: 5,
),
////FIRST NAME
Flexible(
flex: 1,
child: FormBuilderTextField(
decoration: normalTextFieldStyle(
"First name *", "First name *"),
name: "firstname",
validator:
FormBuilderValidators.required(
errorText:
"This field is required"),
),
),
],
),
const SizedBox(
height: 8,
),
Row(
children: [
Flexible(
flex: 1,
child: FormBuilderTextField(
decoration: normalTextFieldStyle(
"Middle name *", "Midlle name *"),
name: "middlename",
validator:
FormBuilderValidators.required(
errorText:
"This field is required"),
),
),
const SizedBox(
width: 5,
),
////CATEGORY
Flexible(
flex: 1,
child: FormBuilderDropdown<
AddressCategory>(
name: 'category',
validator:
FormBuilderValidators.required(
errorText: ""),
decoration: normalTextFieldStyle(
"Category", "Category"),
items: state.categories.map<
DropdownMenuItem<
AddressCategory>>(
(AddressCategory cat) {
return DropdownMenuItem<
AddressCategory>(
value: cat,
child: Text(cat.name!),
);
}).toList(),
onChanged: (value) {
setState(() {
selectedCategory = value;
});
},
),
),
],
),
const SizedBox(
height: 8,
),
////OVERSEAS ADDRESS
FormBuilderSwitch(
initialValue: overseas,
activeColor: second,
onChanged: (value) {
setState(() {
overseas = value!;
});
},
decoration: normalTextFieldStyle("", ''),
name: 'overseas',
title: const Text("Overseas Address?"),
),
SizedBox(
height: overseas == true ? 8 : 0,
),
SizedBox(
child: overseas == false
? Column(
children: [
const SizedBox(
height: 12,
),
////REGION DROPDOWN
FormBuilderDropdown<Region?>(
autovalidateMode:
AutovalidateMode
.onUserInteraction,
validator: FormBuilderValidators
.required(
errorText:
"This field is required"),
onChanged:
(Region? region) async {
setState(() {
provinceCall = true;
});
selectedRegion = region;
getProvinces();
},
initialValue: null,
decoration:
normalTextFieldStyle(
"Region*", "Region"),
name: 'region',
items: state.regions.map<
DropdownMenuItem<
Region>>(
(Region region) {
return DropdownMenuItem<
Region>(
value: region,
child: Text(
region.description!));
}).toList(),
),
const SizedBox(
height: 8,
),
//// PROVINCE DROPDOWN
SizedBox(
height: 60,
child: ModalProgressHUD(
color: Colors.transparent,
inAsyncCall: provinceCall,
child: DropdownButtonFormField<
Province?>(
autovalidateMode:
AutovalidateMode
.onUserInteraction,
validator: (value) =>
value == null
? 'required'
: null,
isExpanded: true,
value: selectedProvince,
onChanged:
(Province? province) {
setState(() {
cityCall = true;
});
selectedProvince =
province;
getCities();
},
items: provinces == null
? []
: provinces!.map<
DropdownMenuItem<
Province>>(
(Province
province) {
return DropdownMenuItem(
value:
province,
child:
FittedBox(
child: Text(
province
.description!),
));
}).toList(),
decoration:
normalTextFieldStyle(
"Province*",
"Province")),
),
),
////CITY MUNICIPALITY
SizedBox(
height: 60,
child: ModalProgressHUD(
color: Colors.white,
inAsyncCall: cityCall,
child:
DropdownButtonFormField<
CityMunicipality>(
validator: FormBuilderValidators
.required(
errorText:
"This field is required"),
isExpanded: true,
onChanged:
(CityMunicipality?
city) {
setState(() {
barangayCall = true;
});
selectedMunicipality =
city;
getBarangays();
},
decoration:
normalTextFieldStyle(
"Municipality*",
"Municipality"),
value: selectedMunicipality,
items: citymuns == null
? []
: citymuns!.map<
DropdownMenuItem<
CityMunicipality>>(
(CityMunicipality
c) {
return DropdownMenuItem(
value: c,
child: Text(c
.description!));
}).toList(),
),
),
),
//// BARANGAY
SizedBox(
height: 60,
child: ModalProgressHUD(
color: Colors.white,
inAsyncCall: barangayCall,
child:
DropdownButtonFormField<
Barangay>(
validator: FormBuilderValidators
.required(
errorText:
"This field is required"),
isExpanded: true,
onChanged:
(Barangay? baragay) {
selectedBarangay =
baragay;
},
decoration:
normalTextFieldStyle(
"Barangay*",
"Barangay"),
value: selectedBarangay,
items: barangays == null
? []
: barangays!.map<
DropdownMenuItem<
Barangay>>(
(Barangay
barangay) {
return DropdownMenuItem(
value: barangay,
child: Text(barangay
.description!));
}).toList(),
),
),
),
],
)
//// COUNTRY DROPDOWN
: SizedBox(
height: 60,
child: FormBuilderDropdown<Country>(
initialValue: null,
validator: FormBuilderValidators
.required(
errorText:
"This field is required"),
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;
},
),
),
),
FormBuilderTextField(
name: "mobile",
decoration: normalTextFieldStyle(
"Tel./Mobile *", "Tel./Mobile"),
validator: FormBuilderValidators.required(
errorText: "This field is required"),
),
const Expanded(
child: SizedBox(),
),
SizedBox(
width: double.infinity,
height: 60,
child: ElevatedButton(
style: mainBtnStyle(
primary, Colors.transparent, second),
child: const Text(submit),
onPressed: () {
PersonalReference? personalReference;
if (formKey.currentState!
.saveAndValidate()) {
String lastname = formKey
.currentState!.value['lastname'];
String firstname = formKey
.currentState!.value['firstname'];
String middlename = formKey
.currentState!
.value['middlename'];
String mobile = formKey
.currentState!.value['mobile'];
Region? region = selectedRegion;
Province? province = Province(
code: selectedProvince?.code,
description:
selectedProvince?.description,
region: region,
psgcCode:
selectedProvince?.psgcCode,
shortname:
selectedProvince?.shortname);
CityMunicipality? city =
CityMunicipality(
code: selectedMunicipality
?.code,
description:
selectedMunicipality
?.description,
province: province,
psgcCode: selectedMunicipality
?.psgcCode,
zipcode: selectedMunicipality
?.zipcode);
Address address = Address(
id: null,
addressCategory: selectedCategory,
country: selectedCountry,
barangay: selectedBarangay,
addressClass: null,
cityMunicipality: city);
if (selectedCountry != null) {
personalReference =
PersonalReference(
id: null,
address: Address(
id: null,
addressCategory:
selectedCategory,
country: selectedCountry,
barangay: null,
cityMunicipality: null,
addressClass: null),
lastName: lastname,
contactNo: mobile,
firstName: firstname,
middleName: middlename);
} else {
personalReference =
PersonalReference(
id: null,
address: address,
lastName: lastname,
contactNo: mobile,
firstName: firstname,
middleName: middlename);
}
final progress = ProgressHUD.of(context);
progress!.showWithText("Please wait...");
context.read<ReferencesBloc>().add(
AddReference(
profileId:
int.parse(profileId!),
reference: personalReference,
token: token!));
}
},
),
),
const SizedBox(
height: 20,
),
],
)),
),
);
}
return Container();
},
);
}
return Container();
},
);
}
return Container();
},
);
}
Future<void> getProvinces() async {
try {
List<Province> newProvinces = await LocationUtils.instance
.getProvinces(regionCode: selectedRegion!.code.toString());
setState(() {
provinces = newProvinces;
selectedProvince = provinces![0];
provinceCall = false;
cityCall = true;
getCities();
});
} catch (e) {
context.read<ReferencesBloc>().add(CallErrorState());
}
}
Future<void> getCities() async {
try {
List<CityMunicipality> newCities = await LocationUtils.instance
.getCities(code: selectedProvince!.code.toString());
citymuns = newCities;
setState(() {
selectedMunicipality = newCities[0];
cityCall = false;
barangayCall = true;
getBarangays();
});
} catch (e) {
context.read<ReferencesBloc>().add(CallErrorState());
}
}
Future<void> getBarangays() async {
try {
List<Barangay> newBarangays = await LocationUtils.instance
.getBarangay(code: selectedMunicipality!.code.toString());
barangays = newBarangays;
setState(() {
selectedBarangay = newBarangays[0];
barangayCall = false;
});
} catch (e) {
context.read<ReferencesBloc>().add(CallErrorState());
}
}
}