112 lines
3.3 KiB
Dart
112 lines
3.3 KiB
Dart
import "package:flutter/material.dart";
|
|
import "package:provider/provider.dart";
|
|
|
|
import "../app/app_router.dart";
|
|
import "../app/app_state.dart";
|
|
import "../models/service_point.dart";
|
|
import "../services/api.dart";
|
|
|
|
class ServicePointSelectScreen extends StatefulWidget {
|
|
const ServicePointSelectScreen({super.key});
|
|
|
|
@override
|
|
State<ServicePointSelectScreen> createState() => _ServicePointSelectScreenState();
|
|
}
|
|
|
|
class _ServicePointSelectScreenState extends State<ServicePointSelectScreen> {
|
|
Future<List<ServicePoint>>? _future;
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
final businessId = context.read<AppState>().selectedBusinessId;
|
|
if (businessId == null) return;
|
|
_future ??= Api.listServicePoints(businessId: businessId);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final businessId = context.watch<AppState>().selectedBusinessId;
|
|
|
|
if (businessId == null) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text("Select Service Point")),
|
|
body: const Center(child: Text("No restaurant selected.")),
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Select Service Point"),
|
|
),
|
|
body: FutureBuilder<List<ServicePoint>>(
|
|
future: _future,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (snapshot.hasError) {
|
|
return _ErrorPane(
|
|
message: "Failed to load service points.\n${snapshot.error}",
|
|
onRetry: () => setState(() => _future = Api.listServicePoints(businessId: businessId)),
|
|
);
|
|
}
|
|
|
|
final items = snapshot.data ?? const <ServicePoint>[];
|
|
if (items.isEmpty) {
|
|
return _ErrorPane(
|
|
message: "No service points returned.",
|
|
onRetry: () => setState(() => _future = Api.listServicePoints(businessId: businessId)),
|
|
);
|
|
}
|
|
|
|
return ListView.separated(
|
|
itemCount: items.length,
|
|
separatorBuilder: (_, __) => const Divider(height: 1),
|
|
itemBuilder: (context, i) {
|
|
final sp = items[i];
|
|
return ListTile(
|
|
title: Text(sp.name),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () {
|
|
context.read<AppState>().setServicePoint(sp.servicePointId);
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
AppRoutes.orderHome,
|
|
(route) => false,
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ErrorPane extends StatelessWidget {
|
|
final String message;
|
|
final VoidCallback onRetry;
|
|
|
|
const _ErrorPane({required this.message, required this.onRetry});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(18),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(message, textAlign: TextAlign.center),
|
|
const SizedBox(height: 12),
|
|
FilledButton(
|
|
onPressed: onRetry,
|
|
child: const Text("Retry"),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|