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 _addLocation(BuildContext context, WidgetRef ref) async { final l10n = AppLocalizations.of(context); final controller = TextEditingController(); final name = await showDialog( 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(locationsProvider.notifier).addLocation(name.trim()); } } @override Widget build(BuildContext context, WidgetRef ref) { final l10n = AppLocalizations.of(context); final locations = ref.watch(locationsProvider); final plants = ref.watch(plantsProvider); 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: locations.isEmpty ? Center( child: Text( l10n.locationsEmpty, style: Theme.of(context).textTheme.bodyLarge, ), ) : 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)), ); }, ), ); } }