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 { GlobalKey formKey = GlobalKey(); 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( listener: (context, state) { // TODO: implement listener }, builder: (context, state) { print(state); if (state is ShowBldgAndStructuresScreen) { return BlocConsumer( 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: Padding( padding: const EdgeInsets.all(0.0), child: Container( height: 800, child: SingleChildScrollView( padding: const EdgeInsets.all(2.0), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ 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(), validator: FormBuilderValidators.required( errorText: "This field is required"), 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}'; formKey.currentState!.patchValue( {'unit_value': unit.item?.unitValue}); }); focus.unfocus(); }, ), ), ), 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!); }); }, ), ), const SizedBox( width: 5, ), Expanded( child: FormBuilderTextField( name: 'bldg_area', decoration: normalTextFieldStyle("Area", ""), validator: FormBuilderValidators.compose([]), onChanged: (value) { setState(() { _areaValue = double.parse(value!); }); }, ), ), ], ), 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, ), 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, ), 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, ), 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: [ 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, strucType: _structureType, description: 'None', 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()); context .read() .add(AddBuildingAndStructure( bldgAndStructure: bldgStructure)); } catch (e) { print(e); } }, style: ElevatedButton.styleFrom( primary: Colors.black, ), child: const Text("Submit"), ), ), const SizedBox( width: 5), // Use SizedBox for horizontal spacing in a Row Container( width: 120, height: 60, padding: const EdgeInsets.all(8.0), child: ElevatedButton( onPressed: () { context .read() .add( const LoadBuildingAndStructure()); }, style: ElevatedButton.styleFrom( primary: Colors.black, ), child: const Text("Cancel"), ), ), ], ) ], ), ), ), ), ); } return Container(); }, ); } return Container(); }, ); } }