152 lines
4.7 KiB
Dart
152 lines
4.7 KiB
Dart
import "package:flutter/material.dart";
|
|
import "package:provider/provider.dart";
|
|
|
|
import "../app/app_router.dart";
|
|
import "../app/app_state.dart";
|
|
import "../models/restaurant.dart";
|
|
import "../services/api.dart";
|
|
|
|
class RestaurantSelectScreen extends StatefulWidget {
|
|
const RestaurantSelectScreen({super.key});
|
|
|
|
@override
|
|
State<RestaurantSelectScreen> createState() => _RestaurantSelectScreenState();
|
|
}
|
|
|
|
class _RestaurantSelectScreenState extends State<RestaurantSelectScreen> {
|
|
late Future<List<Restaurant>> _future;
|
|
String? _debugLastRaw;
|
|
int? _debugLastStatus;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_future = _load();
|
|
}
|
|
|
|
Future<List<Restaurant>> _load() async {
|
|
// Fetch raw first so we can show real output if empty/mismatched keys.
|
|
final raw = await Api.listRestaurantsRaw();
|
|
_debugLastRaw = raw.rawBody;
|
|
_debugLastStatus = raw.statusCode;
|
|
|
|
// Then parse strictly (will throw with helpful details).
|
|
return Api.listRestaurants();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Select Restaurant"),
|
|
),
|
|
body: FutureBuilder<List<Restaurant>>(
|
|
future: _future,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (snapshot.hasError) {
|
|
return _ErrorPane(
|
|
title: "Restaurants Load Failed",
|
|
message: "${snapshot.error}",
|
|
statusCode: _debugLastStatus,
|
|
raw: _debugLastRaw,
|
|
onRetry: () => setState(() => _future = _load()),
|
|
);
|
|
}
|
|
|
|
final items = snapshot.data ?? const <Restaurant>[];
|
|
if (items.isEmpty) {
|
|
return _ErrorPane(
|
|
title: "No Restaurants Returned",
|
|
message: "The API returned an empty list. We need to confirm the endpoint + JSON keys.",
|
|
statusCode: _debugLastStatus,
|
|
raw: _debugLastRaw,
|
|
onRetry: () => setState(() => _future = _load()),
|
|
);
|
|
}
|
|
|
|
return ListView.separated(
|
|
itemCount: items.length,
|
|
separatorBuilder: (_, __) => const Divider(height: 1),
|
|
itemBuilder: (context, i) {
|
|
final r = items[i];
|
|
return ListTile(
|
|
title: Text(r.name),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () {
|
|
context.read<AppState>().setBusiness(r.businessId);
|
|
Navigator.of(context).pushNamed(AppRoutes.servicePointSelect);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ErrorPane extends StatelessWidget {
|
|
final String title;
|
|
final String message;
|
|
final int? statusCode;
|
|
final String? raw;
|
|
final VoidCallback onRetry;
|
|
|
|
const _ErrorPane({
|
|
required this.title,
|
|
required this.message,
|
|
required this.statusCode,
|
|
required this.raw,
|
|
required this.onRetry,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final rawText = raw ?? "(no body captured)";
|
|
final showRaw = rawText.length > 0;
|
|
|
|
return SingleChildScrollView(
|
|
child: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(18),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w800)),
|
|
const SizedBox(height: 10),
|
|
Text(message, textAlign: TextAlign.center),
|
|
const SizedBox(height: 10),
|
|
Text("HTTP Status: ${statusCode ?? "-"}", textAlign: TextAlign.center),
|
|
const SizedBox(height: 14),
|
|
if (showRaw) ...[
|
|
const Text("Raw Response:", style: TextStyle(fontWeight: FontWeight.w700)),
|
|
const SizedBox(height: 6),
|
|
Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(12),
|
|
decoration: BoxDecoration(
|
|
border: Border.all(color: Colors.white24),
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: Text(
|
|
rawText,
|
|
style: const TextStyle(fontFamily: "monospace", fontSize: 12),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
],
|
|
FilledButton(
|
|
onPressed: onRetry,
|
|
child: const Text("Retry"),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|