init
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../labels.dart';
|
||||
import '../services/auth_controller.dart';
|
||||
import '../ui/feedback.dart';
|
||||
|
||||
class ServicePointFormScreen extends StatefulWidget {
|
||||
const ServicePointFormScreen({super.key});
|
||||
|
||||
@override
|
||||
State<ServicePointFormScreen> createState() => _ServicePointFormScreenState();
|
||||
}
|
||||
|
||||
class _ServicePointFormScreenState extends State<ServicePointFormScreen> {
|
||||
final _name = TextEditingController();
|
||||
final _address = TextEditingController();
|
||||
String? _district;
|
||||
final _phone = TextEditingController();
|
||||
final _notes = TextEditingController();
|
||||
bool _saving = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_name.dispose();
|
||||
_address.dispose();
|
||||
_phone.dispose();
|
||||
_notes.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
if (_district == null) {
|
||||
showError(context, 'Choose a district');
|
||||
return;
|
||||
}
|
||||
setState(() => _saving = true);
|
||||
try {
|
||||
await context.read<AuthController>().api.createServicePoint({
|
||||
'name': _name.text.trim(),
|
||||
'district': _district,
|
||||
'address': _address.text.trim(),
|
||||
'phone': _phone.text.trim(),
|
||||
'notes': _notes.text.trim(),
|
||||
});
|
||||
if (mounted) Navigator.pop(context, true);
|
||||
} catch (error) {
|
||||
if (mounted) showError(context, error);
|
||||
} finally {
|
||||
if (mounted) setState(() => _saving = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Add service point')),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
TextField(controller: _name, decoration: const InputDecoration(labelText: 'Name')),
|
||||
const SizedBox(height: 12),
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: _district,
|
||||
isExpanded: true,
|
||||
decoration: const InputDecoration(labelText: 'District'),
|
||||
items: [
|
||||
for (final district in sriLankaDistricts)
|
||||
DropdownMenuItem(value: district, child: Text(district)),
|
||||
],
|
||||
onChanged: (value) => setState(() => _district = value),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(controller: _address, decoration: const InputDecoration(labelText: 'Address')),
|
||||
const SizedBox(height: 12),
|
||||
TextField(controller: _phone, decoration: const InputDecoration(labelText: 'Phone')),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _notes,
|
||||
minLines: 2,
|
||||
maxLines: 4,
|
||||
decoration: const InputDecoration(labelText: 'Notes'),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FilledButton(
|
||||
onPressed: _saving ? null : _save,
|
||||
child: Text(_saving ? 'Saving' : 'Save service point'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user