94 lines
3.0 KiB
Dart
94 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../app_theme.dart';
|
|
import '../../model/latlng.dart';
|
|
import '../../model/location.dart';
|
|
import '../../service/validators.dart';
|
|
|
|
class CreateLocationDialog extends StatefulWidget {
|
|
const CreateLocationDialog({super.key, this.initialLocation});
|
|
final Location? initialLocation;
|
|
|
|
@override
|
|
State<CreateLocationDialog> createState() => _CreateLocationDialogState();
|
|
}
|
|
|
|
class _CreateLocationDialogState extends State<CreateLocationDialog> {
|
|
final nameController = TextEditingController();
|
|
final addressController = TextEditingController();
|
|
final coordinatesController = TextEditingController();
|
|
final formKey = GlobalKey<FormState>(debugLabel: 'Create Location Form');
|
|
|
|
@override
|
|
void initState() {
|
|
if (widget.initialLocation != null) {
|
|
nameController.text = widget.initialLocation!.name;
|
|
addressController.text = widget.initialLocation!.address;
|
|
coordinatesController.text = widget.initialLocation!.coordinates
|
|
.toString();
|
|
}
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return AlertDialog(
|
|
actions: [
|
|
TextButton(onPressed: onCancelPressed, child: Text('Cancel')),
|
|
TextButton(onPressed: onSavePressed, child: Text('Save')),
|
|
],
|
|
title: Text('Create Location'),
|
|
content: Form(
|
|
key: formKey,
|
|
child: Column(
|
|
spacing: AppTheme.formColumnSpacing,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextFormField(
|
|
autofocus: true,
|
|
textInputAction: TextInputAction.next,
|
|
controller: nameController,
|
|
keyboardType: TextInputType.text,
|
|
decoration: InputDecoration(labelText: 'Name'),
|
|
validator: notEmptyValidator,
|
|
),
|
|
TextFormField(
|
|
textInputAction: TextInputAction.next,
|
|
controller: addressController,
|
|
keyboardType: TextInputType.streetAddress,
|
|
decoration: InputDecoration(labelText: 'Address (optional)'),
|
|
),
|
|
TextFormField(
|
|
textInputAction: TextInputAction.done,
|
|
controller: coordinatesController,
|
|
onFieldSubmitted: (_) => onSavePressed(),
|
|
keyboardType: TextInputType.numberWithOptions(),
|
|
decoration: InputDecoration(
|
|
labelText: 'Coordinates',
|
|
hint: Text('25.5892, 50.5051662'),
|
|
),
|
|
validator: (value) {
|
|
return notEmptyValidator(value) ?? coordinatesValidator(value);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void onCancelPressed() => Navigator.of(context).pop();
|
|
|
|
void onSavePressed() {
|
|
if (formKey.currentState!.validate()) {
|
|
Navigator.of(context).pop(
|
|
Location(
|
|
name: nameController.text,
|
|
coordinates: LatLng.fromString(coordinatesController.text),
|
|
address: addressController.text,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|