passo_mobile_app/lib/screens/offline/passo/admin/unit_construction.dart

133 lines
5.1 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/barangay_admin/barangay_admin_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/unit_construction/unit_construction_admin_bloc.dart';
import 'package:unit2/model/passo/barangay.dart';
import 'package:unit2/model/passo/unit_construct.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/barangay_api_services.dart';
import 'package:unit2/sevices/offline/offline_passo/admin/api_services/unit_construction_api_services.dart';
import '../../../../sevices/offline/offline_passo/admin/sql_services/sql_services.dart';
import '../../../../theme-data.dart/colors.dart';
import 'package:http/http.dart';
import '../../../../utils/urls.dart';
class UnitConstructionAdminPage extends StatefulWidget {
const UnitConstructionAdminPage();
@override
_UnitConstructionAdminPage createState() => _UnitConstructionAdminPage();
}
class _UnitConstructionAdminPage extends State<UnitConstructionAdminPage> {
final items = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: primary,
title: const Text("Structural Types"),
centerTitle: true,
actions: [
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 15),
),
onPressed: () async {
try {
// final result =
// await UnitConstructionAdminApiServices.instance.fetch();
// // Assuming result is a List of JSON objects, convert them to City objects.
// final units =
// result.map((json) => UnitConstruct.fromJson(json)).toList();
// Loop through the list of City objects and insert them into the local database.
Response response = await get(Uri.parse(
'http://${Url.instance.host()}/api/rptass_app/unitconstruct_values/'));
print(response.body);
if (response.statusCode == 200) {
final List result = jsonDecode(response.body)['data'];
result.map(((e) => UnitConstruct.fromJson(e))).toList();
final units = result
.map((json) => UnitConstruct.fromJson(json))
.toList();
for (UnitConstruct unit in units) {
await SQLServices.instance.createUnitConstruction(unit);
}
context
.read<UnitConstructionAdminBloc>()
.add(LoadUnitConstruct());
}
} catch (e) {
// Handle any errors that might occur during the API call or database insertion.
print("Error: $e");
}
},
child: const Text('SYNC'),
),
],
),
body: BlocConsumer<UnitConstructionAdminBloc, UnitConstructionAdminState>(
listener: (context, state) {
// TODO: implement listener
},
builder: (context, state) {
if (state is UnitConstructLoaded) {
return Column(children: [
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(15.0),
child: Column(
children: [
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
// ignore: prefer_const_literals_to_create_immutables
columns: [
const DataColumn(
label: Text('Bldg Type'),
),
const DataColumn(
label: Text('Building'),
),
const DataColumn(
label: Text('Unit Value'),
),
],
rows: state.unit.map((dataRow) {
return DataRow(
cells: [
DataCell(Text(dataRow.bldgType ??
'N/A')), // Use a default value if cityCode is null
DataCell(Text(dataRow.building ??
'N/A')), // Use a default value if cityDescription is null
DataCell(Text(dataRow.unitValue ?? 'N/A')),
],
);
}).toList(),
),
)
],
),
),
),
)
]);
}
return Container();
},
),
);
}
}