leafittome/lib/features/locations/presentation/locations_screen.dart
cschlaefke 1b2c880c89 V2: Haushalt teilen – Einladungscodes, Mitglieder- und Sitter-Rolle, sichtbare Bestätigungen
- Einladen per einmaligem 6-stelligem Code (7 Tage gültig), wahlweise als Mitglied oder Pflanzen-Sitter
- Beitritt über Cloud Function joinHousehold (Transaktion, Admin-Rechte)
- Security Rules: Sitter dürfen an Pflanzen nur Bestätigungs-Felder ändern; invites nur erstellen, nie lesen
- Haushalts-Screen: Mitgliederliste mit Rollen, Einladen, Beitreten mit Wechsel-Warnung
- UI-Gating: Sitter sehen keine Anlegen-/Bearbeiten-/Löschen-Aktionen
- Bestätigungen speichern den Namen (lastWateredBy/lastFertilizedBy), Anzeige im Profil
- Tests: Members-Map im Seed, neuer Sitter-Test

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-19 19:14:34 +02:00

101 lines
3.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/widgets/app_drawer.dart';
import '../../../l10n/generated/app_localizations.dart';
import '../../household/data/household_providers.dart';
import '../../household/domain/household.dart';
import '../../plants/data/plants_provider.dart';
import '../data/locations_provider.dart';
class LocationsScreen extends ConsumerWidget {
const LocationsScreen({super.key});
Future<void> _addLocation(BuildContext context, WidgetRef ref) async {
final l10n = AppLocalizations.of(context);
final controller = TextEditingController();
final name = await showDialog<String>(
context: context,
builder: (dialogContext) => AlertDialog(
title: Text(l10n.addLocation),
content: TextField(
controller: controller,
autofocus: true,
decoration: InputDecoration(
labelText: l10n.locationNameLabel,
border: const OutlineInputBorder(),
),
onSubmitted: (value) => Navigator.pop(dialogContext, value),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: Text(l10n.cancel),
),
FilledButton(
onPressed: () => Navigator.pop(dialogContext, controller.text),
child: Text(l10n.save),
),
],
),
);
if (name != null && name.trim().isNotEmpty) {
ref.read(locationRepositoryProvider).addLocation(name.trim());
}
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final l10n = AppLocalizations.of(context);
final locationsAsync = ref.watch(locationsProvider);
final locations = locationsAsync.value ?? const [];
final plants = ref.watch(plantsProvider).value ?? const [];
final Widget body;
if (locationsAsync.isLoading && !locationsAsync.hasValue) {
body = const Center(child: CircularProgressIndicator());
} else if (locations.isEmpty) {
body = Center(
child: Text(
l10n.locationsEmpty,
style: Theme.of(context).textTheme.bodyLarge,
),
);
} else {
body = ListView.separated(
padding: const EdgeInsets.fromLTRB(0, 8, 0, 96),
itemCount: locations.length,
separatorBuilder: (_, _) => const Divider(height: 1),
itemBuilder: (context, index) {
final location = locations[index];
final plantCount = plants
.where((plant) => plant.locationId == location.id)
.length;
return ListTile(
leading: const Icon(Icons.place),
title: Text(
location.name,
style: Theme.of(context).textTheme.titleMedium,
),
subtitle: Text(l10n.locationPlantCount(plantCount)),
);
},
);
}
final canEdit = ref.watch(myRoleProvider) == HouseholdRole.member;
return Scaffold(
appBar: AppBar(title: Text(l10n.locationsTitle)),
drawer: const AppDrawer(),
floatingActionButton: canEdit
? FloatingActionButton.extended(
onPressed: () => _addLocation(context, ref),
icon: const Icon(Icons.add),
label: Text(l10n.addLocation),
)
: null,
body: body,
);
}
}