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/model/passo/barangay.dart'; import 'package:unit2/sevices/offline/offline_passo/admin/api_services/barangay_api_services.dart'; import '../../../../sevices/offline/offline_passo/admin/sql_services/sql_services.dart'; import '../../../../theme-data.dart/colors.dart'; class BarangayAdminPage extends StatefulWidget { const BarangayAdminPage(); @override _BarangayAdminPage createState() => _BarangayAdminPage(); } class _BarangayAdminPage extends State { final items = []; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: primary, title: const Text("Barangay"), centerTitle: true, actions: [ TextButton( style: TextButton.styleFrom( textStyle: const TextStyle(fontSize: 15), ), onPressed: () async { try { final result = await BrgyAdminApiServices.instance.fetch(); // Assuming result is a List of JSON objects, convert them to City objects. final brgys = result.map((json) => Brgy.fromJson(json)).toList(); // Loop through the list of City objects and insert them into the local database. for (Brgy brgy in brgys) { await SQLServices.instance.createBarangay(brgy); } context.read().add(LoadBarangay()); } catch (e) { // Handle any errors that might occur during the API call or database insertion. print("Error: $e"); } }, child: const Text('SYNC'), ), ], ), body: BlocConsumer( listener: (context, state) { // TODO: implement listener }, builder: (context, state) { if (state is BarangayLoaded) { 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('ID'), ), const DataColumn( label: Text('Code'), ), const DataColumn( label: Text('City Code'), ), const DataColumn( label: Text('Description'), ), ], rows: state.brgy.map((dataRow) { return DataRow( cells: [ DataCell(Text(dataRow.barangayId.toString() ?? 'N/A')), // Use a default value if cityCode is null DataCell(Text(dataRow.barangayCode ?? 'N/A')), // Use a default value if cityDescription is null DataCell(Text(dataRow.cityCode ?? 'N/A')), DataCell(Text( dataRow.barangayDescription ?? 'N/A')), ], ); }).toList(), ), ) ], ), ), ), ) ]); } return Container(); }, ), ); } }