update home_page halaman dashboard

This commit is contained in:
kleponijo 2026-03-03 15:21:49 +07:00
parent cd2bc3e0cc
commit 75d9e3220e
1 changed files with 125 additions and 65 deletions

View File

@ -15,6 +15,20 @@ class HomeScreen extends StatefulWidget {
State<HomeScreen> createState() => _HomeScreenState(); State<HomeScreen> createState() => _HomeScreenState();
} }
// simple model to represent one activity log item
class ActivityItem {
final String type; // evaporasi, angin, udara, etc
final String message;
final DateTime time;
ActivityItem({
required this.type,
required this.message,
required this.time,
});
}
class _HomeScreenState extends State<HomeScreen> { class _HomeScreenState extends State<HomeScreen> {
// Firebase realtime database references // Firebase realtime database references
final DatabaseReference _evaporasiRef = FirebaseDatabase.instance.ref('evaporasi/realtime'); final DatabaseReference _evaporasiRef = FirebaseDatabase.instance.ref('evaporasi/realtime');
@ -40,6 +54,9 @@ class _HomeScreenState extends State<HomeScreen> {
final List<double> _windSpeedData = List<double>.filled(24, 0.0); final List<double> _windSpeedData = List<double>.filled(24, 0.0);
final List<double> _airQualityData = List<double>.filled(24, 0.0); final List<double> _airQualityData = List<double>.filled(24, 0.0);
// activity log
final List<ActivityItem> _activities = [];
// Hourly aggregation helpers // Hourly aggregation helpers
double _evaporasiHourlyTotal = 0.0; double _evaporasiHourlyTotal = 0.0;
int _evaporasiHourlyCount = 0; int _evaporasiHourlyCount = 0;
@ -71,6 +88,17 @@ class _HomeScreenState extends State<HomeScreen> {
_currentEvaporasi = value; _currentEvaporasi = value;
_evaporasiUpdateTime = waktu; _evaporasiUpdateTime = waktu;
// add to activity log
_activities.insert(
0,
ActivityItem(
type: "evaporasi",
message: "Evaporasi tercatat ${value.toStringAsFixed(1)} mm",
time: waktu,
),
);
if (_activities.length > 50) _activities.removeLast();
final incomingHour = waktu.hour; final incomingHour = waktu.hour;
if (incomingHour != _currentHour) { if (incomingHour != _currentHour) {
@ -108,6 +136,17 @@ class _HomeScreenState extends State<HomeScreen> {
_currentWindSpeed = value; _currentWindSpeed = value;
_windSpeedUpdateTime = waktu; _windSpeedUpdateTime = waktu;
// add to activity log
_activities.insert(
0,
ActivityItem(
type: "angin",
message: "Kecepatan angin update menjadi ${value.toStringAsFixed(1)} km/h",
time: waktu,
),
);
if (_activities.length > 50) _activities.removeLast();
final incomingHour = waktu.hour; final incomingHour = waktu.hour;
if (incomingHour != _currentHour) { if (incomingHour != _currentHour) {
@ -145,6 +184,17 @@ class _HomeScreenState extends State<HomeScreen> {
_currentAirQuality = value; _currentAirQuality = value;
_airQualityUpdateTime = waktu; _airQualityUpdateTime = waktu;
// add to activity log
_activities.insert(
0,
ActivityItem(
type: "udara",
message: "AQI berubah menjadi ${value.toStringAsFixed(0)}",
time: waktu,
),
);
if (_activities.length > 50) _activities.removeLast();
final incomingHour = waktu.hour; final incomingHour = waktu.hour;
if (incomingHour != _currentHour) { if (incomingHour != _currentHour) {
@ -286,72 +336,44 @@ class _HomeScreenState extends State<HomeScreen> {
/// 🔹 DASHBOARD UTAMA - 3 MONITORING /// 🔹 DASHBOARD UTAMA - 3 MONITORING
body: Padding( body: Padding(
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
child: SingleChildScrollView( child: Column(
child: Column( crossAxisAlignment: CrossAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start, children: [
children: [ BlocBuilder<AuthenticationBloc, AuthenticationState>(
BlocBuilder<AuthenticationBloc, AuthenticationState>( builder: (context, state) {
builder: (context, state) { final name = state.user?.name.isNotEmpty == true
final name = state.user?.name.isNotEmpty == true ? state.user!.name
? state.user!.name : 'Pengguna';
: 'Pengguna'; return Text(
return Text( 'Selamat Datang $name',
'Selamat Datang $name', style: const TextStyle(
style: const TextStyle( fontSize: 22,
fontSize: 22, fontWeight: FontWeight.bold,
fontWeight: FontWeight.bold, ),
);
},
),
const SizedBox(height: 10),
const Text(
"Recent Activity",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
Expanded(
child: _activities.isEmpty
? const Center(child: Text("Belum ada aktivitas"))
: ListView.builder(
itemCount: _activities.length,
itemBuilder: (context, index) {
final item = _activities[index];
return _buildActivityTile(item);
},
), ),
); ),
}, ],
),
const SizedBox(height: 20),
// 🔹 3 BLOCK DATA HARI INI
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_buildDataBlock(
"Evaporasi",
_currentEvaporasi.toStringAsFixed(1),
"mm",
Colors.blue,
Icons.opacity,
),
_buildDataBlock(
"Kec. Angin",
_currentWindSpeed.toStringAsFixed(1),
"km/h",
Colors.green,
Icons.air,
),
_buildDataBlock(
"Kualitas Udara",
_currentAirQuality.toStringAsFixed(0),
"AQI",
Colors.orange,
Icons.cloud,
),
],
),
const SizedBox(height: 30),
const Text(
"Grafik Monitoring",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 15),
_buildDashboardCard(context, "Evaporasi"),
const SizedBox(height: 20),
_buildDashboardCard(context, "Kecepatan Angin"),
const SizedBox(height: 20),
_buildDashboardCard(context, "Kualitas Udara"),
],
),
), ),
), ),
); );
@ -697,4 +719,42 @@ class _HomeScreenState extends State<HomeScreen> {
return Colors.grey; return Colors.grey;
} }
} }
/// ACTIVITY TILE
Widget _buildActivityTile(ActivityItem item) {
IconData icon;
Color color;
switch (item.type) {
case "evaporasi":
icon = Icons.opacity;
color = Colors.blue;
break;
case "angin":
icon = Icons.air;
color = Colors.green;
break;
case "udara":
icon = Icons.cloud;
color = Colors.orange;
break;
default:
icon = Icons.info;
color = Colors.grey;
}
return Card(
margin: const EdgeInsets.only(bottom: 12),
child: ListTile(
leading: CircleAvatar(
backgroundColor: color.withOpacity(0.2),
child: Icon(icon, color: color),
),
title: Text(item.message),
subtitle: Text(
"${item.time.hour}:${item.time.minute.toString().padLeft(2, '0')}",
),
),
);
}
} }