import 'package:flutter/material.dart'; import 'package:firebase_database/firebase_database.dart'; class MyHomePage extends StatefulWidget { const MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _selectedIndex = 0; List> irData = []; List> pirData = []; @override void initState() { super.initState(); fetchData(); } Future fetchData() async { final DatabaseReference ref = FirebaseDatabase.instance.ref('sensor'); final DataSnapshot snapshot = await ref.get(); if (snapshot.exists) { final data = snapshot.value as Map? ?? {}; final ir = data['ir'] as Map? ?? {}; final pir = data['pir'] as Map? ?? {}; setState(() { irData = ir.entries.map((entry) { final value = entry.value as Map? ?? {}; return { 'timestamp': value['timestamp'] as String? ?? 'N/A', 'data': value['sensor_data'] as String? ?? 'N/A', 'snr': value['snr'] as String? ?? 'N/A', 'rssi': value['rssi'] as String? ?? 'N/A', 'delay': value['delay'] as String? ?? 'N/A', }; }).toList(); pirData = pir.entries.map((entry) { final value = entry.value as Map? ?? {}; return { 'timestamp': value['timestamp'] as String? ?? 'N/A', 'data': value['sensor_data'] as String? ?? 'N/A', 'snr': value['snr'] as String? ?? 'N/A', 'rssi': value['rssi'] as String? ?? 'N/A', 'delay': value['delay'] as String? ?? 'N/A', }; }).toList(); }); } else { print('No data available.'); } } void _onItemTapped(int index) { setState(() { _selectedIndex = index; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.green[900], title: Center( child: Text( widget.title, style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.white), ), ), ), body: RefreshIndicator( onRefresh: fetchData, child: _selectedIndex == 0 ? KetinggianAirPage(data: irData) : PendeteksiGerakPage(data: pirData), ), bottomNavigationBar: BottomNavigationBar( items: const [ BottomNavigationBarItem( icon: Icon(Icons.water), label: 'Ketinggian Air', ), BottomNavigationBarItem( icon: Icon(Icons.motion_photos_on), label: 'Pendeteksi Gerak', ), ], currentIndex: _selectedIndex, onTap: _onItemTapped, ), ); } } class KetinggianAirPage extends StatelessWidget { final List> data; const KetinggianAirPage({Key? key, required this.data}) : super(key: key); @override Widget build(BuildContext context) { return SingleChildScrollView( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'Ketinggian Air', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18), ), buildTable(data, 'IR Data'), ], ), ), ); } Widget buildTable(List> data, String header) { return Column( children: [ Text( header, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), Table( border: TableBorder.all(), children: [ buildRow(['Waktu', 'Ket', 'SNR', 'RSSI', 'Delay'], isHeader: true), for (var row in data) buildRow([row['timestamp']!, row['data']!, row['snr']!, row['rssi']!, row['delay']!]), ], ), ], ); } TableRow buildRow(List cells, {bool isHeader = false}) { return TableRow( children: cells.map((cell) { return Container( color: isHeader ? Colors.yellow[100] : null, padding: const EdgeInsets.all(8.0), child: Text( cell, textAlign: TextAlign.center, style: TextStyle( fontWeight: isHeader ? FontWeight.bold : FontWeight.normal, color: isHeader ? Colors.black : null, ), ), ); }).toList(), ); } } class PendeteksiGerakPage extends StatelessWidget { final List> data; const PendeteksiGerakPage({Key? key, required this.data}) : super(key: key); @override Widget build(BuildContext context) { return SingleChildScrollView( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'Pendeteksi Gerak dan Pengusir Burung', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18), ), buildTable(data, 'PIR Data'), ], ), ), ); } Widget buildTable(List> data, String header) { return Column( children: [ Text( header, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), Table( border: TableBorder.all(), children: [ buildRow(['Waktu', 'Ket', 'SNR', 'RSSI', 'Delay'], isHeader: true), for (var row in data) buildRow([row['timestamp']!, row['data']!, row['snr']!, row['rssi']!, row['delay']!]), ], ), ], ); } TableRow buildRow(List cells, {bool isHeader = false}) { return TableRow( children: cells.map((cell) { return Container( color: isHeader ? Colors.yellow[100] : null, padding: const EdgeInsets.all(8.0), child: Text( cell, textAlign: TextAlign.center, style: TextStyle( fontWeight: isHeader ? FontWeight.bold : FontWeight.normal, color: isHeader ? Colors.black : null, ), ), ); }).toList(), ); } }