TKK_E32220429/lib/app/modules/maps/views/maps_view.dart

176 lines
5.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '../controllers/maps_controller.dart';
class MapsView extends GetView<MapsController> {
const MapsView({super.key});
Future<void> _downloadPdf() async {
final pdf = pw.Document();
final controller = Get.find<MapsController>();
pdf.addPage(
pw.Page(
build: (pw.Context context) {
return pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Text('Maps History', style: pw.TextStyle(fontSize: 18, fontWeight: pw.FontWeight.bold)),
pw.SizedBox(height: 12),
pw.Table.fromTextArray(
headers: ['Timestamp', 'Latitude', 'Longitude'],
data: controller.mapsData.map((item) {
final timestamp = item['timestamp'] is Timestamp
? DateFormat('dd/MM/yyyy HH:mm').format((item['timestamp'] as Timestamp).toDate())
: item['timestamp']?.toString() ?? '-';
return [
timestamp,
item['latitude'] ?? '-',
item['longitude'] ?? '-',
];
}).toList(),
cellStyle: const pw.TextStyle(fontSize: 10),
headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold),
headerDecoration: const pw.BoxDecoration(color: PdfColors.grey300),
cellAlignment: pw.Alignment.centerLeft,
),
],
);
},
),
);
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => pdf.save(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[400],
floatingActionButton: FloatingActionButton(
onPressed: _downloadPdf,
backgroundColor: Colors.blue[800],
child: const Icon(Icons.picture_as_pdf, color: Colors.white),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Row(
children: [
IconButton(
icon: const Icon(
Icons.arrow_back,
color: Colors.white, // Set the icon color to white
size: 30.0, // Set the icon size
),
onPressed: () => Get.back(),
),
const SizedBox(width: 8),
Expanded(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
child: Center(
child: Text(
'Maps History',
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
const SizedBox(height: 20),
Obx(() {
if (controller.isLoading.value) {
return const CircularProgressIndicator();
}
if (controller.mapsData.isEmpty) {
return Text(
"No map history available.",
style: GoogleFonts.poppins(fontSize: 14),
);
}
return Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.vertical, // Tambahkan scroll vertikal
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columnSpacing: 20,
headingRowColor: MaterialStateColor.resolveWith((states) => Colors.blue[800]!),
headingTextStyle: GoogleFonts.poppins(
color: Colors.white,
fontWeight: FontWeight.bold,
),
dataTextStyle: GoogleFonts.poppins(
fontSize: 13,
color: Colors.black87,
),
columns: const [
DataColumn(label: Text("Timestamp")),
DataColumn(label: Text("Latitude")),
DataColumn(label: Text("Longitude")),
],
rows: List<DataRow>.generate(
controller.mapsData.length,
(index) {
final item = controller.mapsData[index];
final formattedTimestamp = item['timestamp'] is Timestamp
? DateFormat('dd/MM/yyyy HH:mm').format(
(item['timestamp'] as Timestamp).toDate(),
)
: item['timestamp']?.toString() ?? '-';
final isEven = index % 2 == 0;
return DataRow(
color: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
return isEven
? const Color(0xFFF7F7F7)
: const Color(0xFFEFEFEF);
},
),
cells: [
DataCell(Text(formattedTimestamp)),
DataCell(Text(item['latitude'] ?? '-')),
DataCell(Text(item['longitude'] ?? '-')),
],
);
},
),
),
),
),
);
}),
],
),
),
),
);
}
}