177 lines
5.4 KiB
Dart
177 lines
5.4 KiB
Dart
// lib/screens/restaurant_select_screen.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 "../models/service_point.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 {
|
|
final raw = await Api.listRestaurantsRaw();
|
|
_debugLastRaw = raw.rawBody;
|
|
_debugLastStatus = raw.statusCode;
|
|
return Api.listRestaurants();
|
|
}
|
|
|
|
Future<void> _selectBusinessAndContinue(Restaurant r) async {
|
|
// Set selected business
|
|
context.read<AppState>().setBusiness(r.businessId);
|
|
|
|
// Go pick service point, and WAIT for a selection.
|
|
final sp = await Navigator.of(context).pushNamed(
|
|
AppRoutes.servicePointSelect,
|
|
);
|
|
|
|
if (!mounted) return;
|
|
|
|
if (sp is ServicePoint) {
|
|
// We have a service point selection.
|
|
// TODO: If AppState has a setter for service point, set it here.
|
|
// Example (only if it exists): context.read<AppState>().setServicePoint(sp);
|
|
|
|
// Navigate forward to your next screen.
|
|
// If your router has a specific route const, use it here.
|
|
// The most likely is AppRoutes.orderHome.
|
|
try {
|
|
Navigator.of(context).pushNamed(AppRoutes.orderHome);
|
|
} catch (_) {
|
|
// If orderHome route doesn't exist yet, do nothing.
|
|
// (Still fixed: we no longer "just bounce back" with no forward action.)
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Select Business"),
|
|
),
|
|
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: "Businesses 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 Businesses Returned",
|
|
message: "The API returned an empty list.",
|
|
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: () => _selectBusinessAndContinue(r),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
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.isNotEmpty;
|
|
|
|
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"),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|