passo_mobile_app/lib/screens/offline/passo/building/add/AddBuildingAndStructure..dart

356 lines
16 KiB
Dart
Raw Normal View History

2024-02-08 00:52:29 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:intl/intl.dart';
import 'package:searchfield/searchfield.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/unit_construction/unit_construction_admin_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/building/building_and_structure/building_and_structure_bloc.dart';
import 'package:unit2/model/offline/offline_profile.dart';
import 'package:unit2/model/passo/building_and_structure.dart';
import '../../../../../model/passo/unit_construct.dart';
import '../../../../../theme-data.dart/form-style.dart';
import '../../../../../widgets/passo/custom_formBuilder_fields.dart';
class AddBuildingAndStructureOffline extends StatefulWidget {
final OfflineProfile offlineProfile;
AddBuildingAndStructureOffline(this.offlineProfile);
@override
_AddBuildingAndStructureOffline createState() =>
_AddBuildingAndStructureOffline();
}
class _AddBuildingAndStructureOffline
extends State<AddBuildingAndStructureOffline> {
GlobalKey<FormBuilderState> formKey = GlobalKey<FormBuilderState>();
final focus = FocusNode();
final DateTime now;
final String formatter;
_AddBuildingAndStructureOffline()
: now = DateTime.now(),
formatter = DateFormat.yMMMMd('en_US').format(DateTime.now());
final actual_use = [
"Residential",
"Agricultural",
"Commercial",
"Industrial",
"Mineral",
"Timberland",
];
double _unitBase = 0;
String _structureType = "";
double _areaValue = 0;
double _depRate = 0;
double _totalMarketValue(unitBase, bldgArea) {
return unitBase * bldgArea;
}
double _amountofDepreciation(unitBase, area, depreciation) {
return (unitBase * area) * depreciation;
}
double _adjustedMarketValue(unitBase, area, depreciation) {
double marketVal = unitBase * area;
double depAmount = (unitBase * area) * depreciation;
return marketVal - depAmount;
}
@override
Widget build(BuildContext context) {
return BlocConsumer<BuildingAndStructureBloc, BuildingAndStructureState>(
listener: (context, state) {
// TODO: implement listener
},
builder: (context, state) {
print(state);
if (state is ShowBldgAndStructuresScreen) {
return BlocConsumer<UnitConstructionAdminBloc,
UnitConstructionAdminState>(
listener: (context, state) {
// TODO: implement listener
},
builder: (context, state) {
if (state is UnitConstructLoaded) {
return FormBuilder(
key: formKey,
onChanged: () {
formKey.currentState?.save();
},
autovalidateMode: AutovalidateMode.disabled,
child: SingleChildScrollView(
padding: const EdgeInsets.all(2.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: const EdgeInsets.only(
left: 0, top: 10, right: 0, bottom: 0),
child: SizedBox(
height: 45,
child: SearchField(
itemHeight: 70,
suggestions: state.unit
.map((UnitConstruct unit) =>
SearchFieldListItem(
'${unit.bldgType} - ${unit.building}',
item: unit,
child: ListTile(
title: Text(
'${unit.bldgType} - ${unit.building!.toUpperCase()}',
overflow: TextOverflow.ellipsis,
),
)))
.toList(),
2024-02-08 00:52:29 +00:00
validator: FormBuilderValidators.required(
errorText: "This field is required"),
2024-02-08 00:52:29 +00:00
searchInputDecoration: normalTextFieldStyle(
"Structure Type", "")
.copyWith(
suffixIcon:
const Icon(Icons.arrow_drop_down)),
////agency suggestion tap
focusNode: focus,
suggestionState: Suggestion.expand,
onSuggestionTap: (unit) {
setState(() {
_unitBase =
double.parse(unit.item!.unitValue);
_structureType =
'${unit.item!.bldgType} - ${unit.item!.building}';
2024-02-08 00:52:29 +00:00
formKey.currentState!.patchValue(
{'unit_value': unit.item?.unitValue});
});
focus.unfocus();
},
2024-02-08 00:52:29 +00:00
),
),
),
const SizedBox(
height: 10,
),
customDropDownField(
"Actual Use", "", 'actual_use', actual_use),
const SizedBox(
height: 5,
),
const SizedBox(
height: 10,
),
Container(
child: FormBuilderTextField(
name: 'unit_value',
decoration: normalTextFieldStyle("Unit Value", ""),
validator: FormBuilderValidators.compose([]),
onChanged: (value) {
// setState(() {
// _areaValue = int.parse(value!);
// });
},
),
),
SizedBox(
height: 10,
),
Row(
children: [
Expanded(
child: FormBuilderTextField(
name: 'dep_rate',
decoration: normalTextFieldStyle(
"Depreciation Rate", ""),
validator: FormBuilderValidators.compose([]),
onChanged: (value) {
setState(() {
_depRate = double.parse(value!);
});
},
),
2024-02-08 00:52:29 +00:00
),
const SizedBox(
width: 5,
2024-02-08 00:52:29 +00:00
),
Expanded(
2024-02-08 00:52:29 +00:00
child: FormBuilderTextField(
name: 'bldg_area',
decoration: normalTextFieldStyle("Area", ""),
2024-02-08 00:52:29 +00:00
validator: FormBuilderValidators.compose([]),
onChanged: (value) {
setState(() {
_areaValue = double.parse(value!);
});
2024-02-08 00:52:29 +00:00
},
),
),
],
),
const SizedBox(height: 10),
const Text('Market Value'),
const SizedBox(height: 5),
Container(
height: 45.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 1.0,
2024-02-08 00:52:29 +00:00
),
borderRadius: BorderRadius.circular(5.0),
),
child: Align(
alignment: Alignment.center,
child: Text(NumberFormat.currency(
locale: 'en-PH', symbol: "")
.format(_totalMarketValue(
_unitBase, _areaValue)))),
),
const SizedBox(height: 10),
const Text('Amount of Depreciation'),
const SizedBox(height: 5),
Container(
height: 45.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 1.0,
2024-02-08 00:52:29 +00:00
),
borderRadius: BorderRadius.circular(5.0),
),
child: Align(
alignment: Alignment.center,
child: Text(NumberFormat.currency(
locale: 'en-PH', symbol: "")
.format(_amountofDepreciation(
_unitBase, _areaValue, _depRate)))),
),
const SizedBox(height: 10),
const Text('Adjusted Market Value'),
const SizedBox(height: 5),
Container(
height: 45.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 1.0,
2024-02-08 00:52:29 +00:00
),
borderRadius: BorderRadius.circular(5.0),
),
child: Align(
alignment: Alignment.center,
child: Text(NumberFormat.currency(
locale: 'en-PH', symbol: "")
.format(_adjustedMarketValue(
_unitBase, _areaValue, _depRate)))),
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2024-02-08 00:52:29 +00:00
Container(
width: 120,
height: 60,
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () async {
try {
final tempID =
await SharedPreferences.getInstance();
var bldgStructure = BldgAndStructure(
id: 1,
bldgapprDetailsId: tempID.getInt(
'tempid')!,
// assessedById: widget
// .offlineProfile.id
// .toString(),
// assessedByName: widget
// .offlineProfile.firstName,
// dateCreated: formatter,
// dateModified: 'none',
bldgType: _structureType,
structType: _structureType,
description: 'None',
buccPercentage: '1',
actualUse: formKey.currentState?.value[
'actual_use'] ??
"",
floorCount: '1',
bldgArea: _areaValue.toString(),
unitValue: _unitBase.toString(),
depRate: _depRate.toString(),
marketValue: _totalMarketValue(
_unitBase, _areaValue)
.toString(),
depAmount: _amountofDepreciation(
_unitBase, _areaValue, _depRate)
.toString(),
adjustedMarketValue:
_adjustedMarketValue(_unitBase,
_areaValue, _depRate)
.toString(),
genCode: "5TH");
context
.read<BuildingAndStructureBloc>()
.add(AddBuildingAndStructure(
bldgAndStructure: bldgStructure));
} catch (e) {
print(e);
}
},
style: ElevatedButton.styleFrom(
primary: Colors.black,
2024-02-08 00:52:29 +00:00
),
child: const Text("Submit"),
2024-02-08 00:52:29 +00:00
),
),
const SizedBox(
width:
5), // Use SizedBox for horizontal spacing in a Row
2024-02-08 00:52:29 +00:00
Container(
width: 120,
height: 60,
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
context
.read<BuildingAndStructureBloc>()
.add(const LoadBuildingAndStructure());
},
style: ElevatedButton.styleFrom(
primary: Colors.black,
2024-02-08 00:52:29 +00:00
),
child: const Text("Cancel"),
2024-02-08 00:52:29 +00:00
),
),
],
)
],
2024-02-08 00:52:29 +00:00
),
),
);
}
return Container();
},
);
}
return Container();
},
);
}
}