75 lines
2.3 KiB
Dart
75 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../model/extensions/controller_context.dart';
|
|
import '../model/location.dart';
|
|
import '../service/controllers/location_controller.dart';
|
|
import '../widgets/dialogs/create_location_dialog.dart';
|
|
|
|
class LocationsOverviewPage extends StatefulWidget {
|
|
static const routeName = '/locations';
|
|
const LocationsOverviewPage({super.key});
|
|
|
|
@override
|
|
State<LocationsOverviewPage> createState() => _LocationsOverviewPageState();
|
|
}
|
|
|
|
class _LocationsOverviewPageState extends State<LocationsOverviewPage> {
|
|
List<Location> locations = [];
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
locations = context.controller<LocationController>().locations;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text('Manage Locations')),
|
|
body: Padding(
|
|
padding: EdgeInsetsGeometry.symmetric(
|
|
horizontal: MediaQuery.of(context).size.width * 0.05,
|
|
),
|
|
child: ListView.builder(
|
|
itemBuilder: listViewBuilder,
|
|
itemCount: context.controller<LocationController>().locations.length,
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: onAddLocationButtonPressed,
|
|
child: Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget listViewBuilder(BuildContext context, int index) {
|
|
final location = locations.elementAt(index);
|
|
final String subtitle = location.address.isEmpty
|
|
? location.coordinates.toString()
|
|
: location.address;
|
|
|
|
return ListTile(
|
|
title: Text(location.name),
|
|
subtitle: Text(subtitle),
|
|
onTap: () => onEditLocationButtonPressed(location),
|
|
);
|
|
}
|
|
|
|
void onAddLocationButtonPressed() async {
|
|
final result = await showDialog<Location?>(
|
|
context: context,
|
|
builder: (context) => CreateLocationDialog(),
|
|
barrierDismissible: false,
|
|
);
|
|
if (mounted && result != null) {
|
|
context.controller<LocationController>().addLocation(result);
|
|
}
|
|
}
|
|
|
|
void onEditLocationButtonPressed(Location location) async {
|
|
final result = await showDialog<Location?>(
|
|
context: context,
|
|
builder: (context) => CreateLocationDialog(initialLocation: location),
|
|
barrierDismissible: false,
|
|
);
|
|
if (mounted && result != null) {
|
|
context.controller<LocationController>().updateLocation(location, result);
|
|
}
|
|
}
|
|
}
|