97 lines
3.0 KiB
Dart
97 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../labels.dart';
|
|
import '../services/auth_controller.dart';
|
|
import '../ui/feedback.dart';
|
|
|
|
class AddDealerScreen extends StatefulWidget {
|
|
const AddDealerScreen({super.key});
|
|
|
|
@override
|
|
State<AddDealerScreen> createState() => _AddDealerScreenState();
|
|
}
|
|
|
|
class _AddDealerScreenState extends State<AddDealerScreen> {
|
|
final _name = TextEditingController();
|
|
final _code = TextEditingController();
|
|
final _address = TextEditingController();
|
|
final _phone = TextEditingController();
|
|
String? _district;
|
|
bool _saving = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_name.dispose();
|
|
_code.dispose();
|
|
_address.dispose();
|
|
_phone.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _save() async {
|
|
setState(() => _saving = true);
|
|
try {
|
|
await context.read<AuthController>().api.createDealer({
|
|
'name': _name.text.trim(),
|
|
'code': _code.text.trim(),
|
|
'district': _district ?? '',
|
|
'address': _address.text.trim(),
|
|
'phone': _phone.text.trim(),
|
|
});
|
|
if (!mounted) return;
|
|
showNotice(context, 'Dealer and login created');
|
|
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 dealer')),
|
|
body: ListView(
|
|
padding: const EdgeInsets.all(16),
|
|
children: [
|
|
TextField(controller: _name, decoration: const InputDecoration(labelText: 'Name')),
|
|
const SizedBox(height: 12),
|
|
TextField(controller: _code, decoration: const InputDecoration(labelText: 'Code')),
|
|
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,
|
|
keyboardType: TextInputType.phone,
|
|
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
|
|
decoration: const InputDecoration(
|
|
labelText: 'Contact phone',
|
|
hintText: '0715127342',
|
|
helperText: 'This number becomes the dealer login',
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
FilledButton(
|
|
onPressed: _saving ? null : _save,
|
|
child: Text(_saving ? 'Saving' : 'Create dealer'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|