Neue Route /plants/:id/diagnoses mit PlantDiagnosesScreen; im Detail ersetzt der Button „Frühere Untersuchungen" (deaktiviert bei leerer Historie) die Inline-Liste. Ergebnis-Sheet nach diagnosis_sheet.dart ausgelagert und von beiden Screens genutzt. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
129 lines
4.6 KiB
Dart
129 lines
4.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import '../../../l10n/generated/app_localizations.dart';
|
|
import '../../household/data/household_providers.dart';
|
|
import '../../household/domain/household.dart';
|
|
import '../data/plant_diagnosis_service.dart';
|
|
import '../data/plants_provider.dart';
|
|
import 'diagnosis_sheet.dart';
|
|
|
|
/// Untersuchungs-Seite einer Pflanze: alle gespeicherten Diagnosen
|
|
/// (neueste zuerst); Antippen öffnet das volle Ergebnis, volle Mitglieder
|
|
/// können Einträge löschen.
|
|
class PlantDiagnosesScreen extends ConsumerWidget {
|
|
const PlantDiagnosesScreen({super.key, required this.plantId});
|
|
|
|
final String plantId;
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final theme = Theme.of(context);
|
|
final plant = ref.watch(plantByIdProvider(plantId));
|
|
|
|
if (plant == null) {
|
|
// Pflanze wurde gelöscht, während der Screen offen war.
|
|
return Scaffold(appBar: AppBar(), body: const SizedBox.shrink());
|
|
}
|
|
|
|
final entries = ref.watch(plantDiagnosesProvider(plantId)).value ?? [];
|
|
final canEdit = ref.watch(myRoleProvider) == HouseholdRole.member;
|
|
final locale = Localizations.localeOf(context).toString();
|
|
final dateFormat = DateFormat('d. MMMM y, HH:mm', locale);
|
|
final healthyColor = theme.brightness == Brightness.dark
|
|
? Colors.green.shade300
|
|
: Colors.green.shade700;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('${l10n.diagnosisHistoryTitle} · ${plant.nickname}'),
|
|
),
|
|
body: entries.isEmpty
|
|
// Kommt nur vor, wenn hier der letzte Eintrag gelöscht wurde —
|
|
// der Button im Detail ist bei leerer Historie deaktiviert.
|
|
? Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Text(
|
|
l10n.diagnosisHistoryEmpty,
|
|
style: theme.textTheme.bodyLarge,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
)
|
|
: ListView.builder(
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: entries.length,
|
|
itemBuilder: (context, index) {
|
|
final entry = entries[index];
|
|
return ListTile(
|
|
leading: Icon(
|
|
entry.result.healthy
|
|
? Icons.check_circle
|
|
: Icons.warning_amber_rounded,
|
|
color: entry.result.healthy
|
|
? healthyColor
|
|
: theme.colorScheme.error,
|
|
),
|
|
title: Text(
|
|
entry.result.summary,
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
subtitle: entry.createdAt != null
|
|
? Text(dateFormat.format(entry.createdAt!))
|
|
: null,
|
|
trailing: canEdit
|
|
? IconButton(
|
|
icon: const Icon(Icons.delete_outline),
|
|
tooltip: l10n.delete,
|
|
onPressed: () => _confirmDelete(context, ref, entry),
|
|
)
|
|
: null,
|
|
onTap: () => showDiagnosisSheet(
|
|
context,
|
|
result: entry.result,
|
|
plantNickname: plant.nickname,
|
|
createdAt: entry.createdAt,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _confirmDelete(
|
|
BuildContext context,
|
|
WidgetRef ref,
|
|
PlantDiagnosisEntry entry,
|
|
) async {
|
|
final l10n = AppLocalizations.of(context);
|
|
final locale = Localizations.localeOf(context).toString();
|
|
final dateText = entry.createdAt != null
|
|
? DateFormat('d. MMMM y', locale).format(entry.createdAt!)
|
|
: '';
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (dialogContext) => AlertDialog(
|
|
title: Text(l10n.deleteDiagnosisQuestion(dateText)),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(dialogContext, false),
|
|
child: Text(l10n.cancel),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.pop(dialogContext, true),
|
|
child: Text(l10n.delete),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (confirmed == true) {
|
|
await ref
|
|
.read(plantDiagnosisServiceProvider)
|
|
.deleteDiagnosis(plantId, entry.id);
|
|
}
|
|
}
|
|
}
|