- Datenmodell: users/{uid}, households/{id} mit memberUids, plants/locations als Subcollections
- StreamProvider liefern Live-Daten, Repositories kapseln Schreibzugriffe
- Login-Screen mit Registrierung (legt Haushalt automatisch an), Logout in Einstellungen
- Router-Redirect bei Login/Logout, firestore.rules mit Haushalts-Prinzip
- Tests auf firebase_auth_mocks + fake_cloud_firestore umgestellt
- Verifiziert: analyze/test grün, Android-Debug-Build erfolgreich
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
95 lines
3.2 KiB
Dart
95 lines
3.2 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 '../../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)),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(l10n.locationsTitle)),
|
|
drawer: const AppDrawer(),
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
onPressed: () => _addLocation(context, ref),
|
|
icon: const Icon(Icons.add),
|
|
label: Text(l10n.addLocation),
|
|
),
|
|
body: body,
|
|
);
|
|
}
|
|
}
|