232 lines
7.6 KiB
Dart
232 lines
7.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mobile_monitoring/core/constants/constants.dart';
|
|
import 'package:mobile_monitoring/ui/widgets/graph_component_service.dart';
|
|
|
|
class WeeklyStatsCard extends StatelessWidget {
|
|
final String? userId;
|
|
|
|
const WeeklyStatsCard({super.key, this.userId});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final graphService = GraphComponentService();
|
|
|
|
return Container(
|
|
width: 320,
|
|
padding: const EdgeInsets.all(18),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.white,
|
|
borderRadius: BorderRadius.circular(30),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: AppColors.darkGray.withValues(alpha: 0.08),
|
|
spreadRadius: 2,
|
|
blurRadius: 10,
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'Statistik Mingguan pH',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 16,
|
|
color: Color.fromARGB(255, 0, 0, 0),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
StreamBuilder<List<Map<String, dynamic>>>(
|
|
stream: graphService.historyStream(limit: 7, userId: userId),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasError) {
|
|
final isAuthError = graphService.isAuthError(snapshot.error!);
|
|
|
|
if (isAuthError) {
|
|
return SizedBox(
|
|
height: 150,
|
|
child: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.error_outline,
|
|
color: AppColors.error,
|
|
size: 32,
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
AppStrings.pleaseLogin,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const SizedBox(
|
|
height: 150,
|
|
child: Center(
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
),
|
|
);
|
|
}
|
|
|
|
final docs = snapshot.data ?? [];
|
|
if (docs.isEmpty) {
|
|
return SizedBox(
|
|
height: 150,
|
|
child: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.agriculture,
|
|
color: AppColors.primary,
|
|
size: 32,
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
AppStrings.startPlantingFirst,
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
final statsResult = graphService.calculateWeeklyStats(docs);
|
|
if (!statsResult.success || statsResult.data == null) {
|
|
return const SizedBox(
|
|
height: 150,
|
|
child: Center(
|
|
child: Text(
|
|
AppStrings.noDataAvailable,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
final stats = statsResult.data!;
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
_StatBox(
|
|
label: 'Minimum',
|
|
value: stats.minPh.toStringAsFixed(1),
|
|
color: AppColors.primary,
|
|
),
|
|
_StatBox(
|
|
label: 'Maksimum',
|
|
value: stats.maxPh.toStringAsFixed(1),
|
|
color: AppColors.primary,
|
|
),
|
|
_StatBox(
|
|
label: 'Rata-rata',
|
|
value: stats.avgPh.toStringAsFixed(1),
|
|
color: AppColors.primary,
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 24),
|
|
const Text(
|
|
'Statistik Mingguan Nutrisi (TDS)',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 16,
|
|
color: Color.fromARGB(255, 0, 0, 0),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
_StatBox(
|
|
label: 'Minimum',
|
|
value: stats.minTds.toStringAsFixed(0),
|
|
color: AppColors.primary,
|
|
),
|
|
_StatBox(
|
|
label: 'Maksimum',
|
|
value: stats.maxTds.toStringAsFixed(0),
|
|
color: AppColors.primary,
|
|
),
|
|
_StatBox(
|
|
label: 'Rata-rata',
|
|
value: stats.avgTds.toStringAsFixed(0),
|
|
color: AppColors.primary,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _StatBox extends StatelessWidget {
|
|
final String label;
|
|
final String value;
|
|
final Color color;
|
|
const _StatBox({
|
|
required this.label,
|
|
required this.value,
|
|
required this.color,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 18),
|
|
decoration: BoxDecoration(
|
|
color: color.withValues(alpha: 0.15),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Text(
|
|
value,
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 18,
|
|
color: color,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
Text(label, style: TextStyle(fontSize: 13, color: color)),
|
|
],
|
|
);
|
|
}
|
|
}
|