133 lines
4.0 KiB
Dart
133 lines
4.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../labels.dart';
|
|
import '../models/models.dart';
|
|
import '../services/auth_controller.dart';
|
|
import '../ui/feedback.dart';
|
|
import 'service_point_form_screen.dart';
|
|
|
|
class ServicePointsScreen extends StatefulWidget {
|
|
const ServicePointsScreen({super.key});
|
|
|
|
@override
|
|
State<ServicePointsScreen> createState() => _ServicePointsScreenState();
|
|
}
|
|
|
|
class _ServicePointsScreenState extends State<ServicePointsScreen> {
|
|
final _search = TextEditingController();
|
|
String? _district;
|
|
List<ServicePoint>? _points;
|
|
bool _loading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_load();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_search.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
try {
|
|
final points = await context.read<AuthController>().api.servicePoints(
|
|
district: _district,
|
|
search: _search.text.trim(),
|
|
);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_points = points;
|
|
_loading = false;
|
|
});
|
|
} catch (error) {
|
|
if (!mounted) return;
|
|
setState(() => _loading = false);
|
|
showError(context, error);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final role = context.watch<AuthController>().user?.role;
|
|
final points = _points ?? [];
|
|
if (_loading && _points == null) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
return Scaffold(
|
|
floatingActionButton: role == 'dealer'
|
|
? null
|
|
: FloatingActionButton.extended(
|
|
onPressed: () async {
|
|
final created = await Navigator.of(context).push<bool>(
|
|
MaterialPageRoute(builder: (_) => const ServicePointFormScreen()),
|
|
);
|
|
if (created == true) _load();
|
|
},
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('Add point'),
|
|
),
|
|
body: RefreshIndicator(
|
|
onRefresh: _load,
|
|
child: ListView(
|
|
padding: const EdgeInsets.fromLTRB(16, 12, 16, 96),
|
|
children: [
|
|
DropdownButtonFormField<String>(
|
|
initialValue: _district,
|
|
isExpanded: true,
|
|
decoration: const InputDecoration(labelText: 'District'),
|
|
items: [
|
|
const DropdownMenuItem(value: null, child: Text('All districts')),
|
|
for (final district in sriLankaDistricts)
|
|
DropdownMenuItem(value: district, child: Text(district)),
|
|
],
|
|
onChanged: (value) {
|
|
setState(() => _district = value);
|
|
_load();
|
|
},
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _search,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Search',
|
|
hintText: 'Name, repair work, phone',
|
|
),
|
|
onSubmitted: (_) => _load(),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: FilledButton(onPressed: _load, child: const Text('Search')),
|
|
),
|
|
const SizedBox(height: 12),
|
|
if (points.isEmpty)
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 32),
|
|
child: Center(child: Text('No service points match.')),
|
|
),
|
|
for (final point in points)
|
|
Card(
|
|
child: ListTile(
|
|
title: Text(point.name),
|
|
subtitle: Text(
|
|
[
|
|
point.district,
|
|
if (point.address != null && point.address!.isNotEmpty) point.address,
|
|
if (point.phone != null && point.phone!.isNotEmpty) point.phone,
|
|
if (point.notes != null && point.notes!.isNotEmpty) point.notes,
|
|
].join('\n'),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|