import 'package:flutter/material.dart';
import '../../core/theme/app_colors.dart';
import '../../data/app_data_scope.dart';
import '../../models/event.dart';
import '../../repositories/event_repository.dart';
import '../../services/external_link_service.dart';
import '../../services/external_navigation_service.dart';
import '../../widgets/remote_image.dart';
import 'event_ui_text.dart';
class EventDetailPage extends StatefulWidget {
const EventDetailPage({super.key, required this.event});
final Event event;
@override
State<EventDetailPage> createState() => _EventDetailPageState();
}
class _EventDetailPageState extends State<EventDetailPage> {
late Event _event;
bool _loadingDetail = true;
EventActionUrls? _actions;
@override
void initState() {
super.initState();
_event = widget.event;
WidgetsBinding.instance.addPostFrameCallback((_) {
_loadDetail();
_loadActions();
});
}
Future<void> _loadActions() async {
try {
final actions =
await AppDataScope.read(context).eventActions(widget.event.id);
if (!mounted) return;
setState(() => _actions = actions);
} catch (_) {
// Keep any action URLs that arrived with the event feed as fallback.
}
}
Future<void> _loadDetail() async {
try {
final detail =
await AppDataScope.read(context).eventDetail(widget.event.id);
if (!mounted) return;
setState(() {
_event = detail;
_loadingDetail = false;
});
} catch (_) {
if (!mounted) return;
setState(() => _loadingDetail = false);
}
}
@override
Widget build(BuildContext context) {
final event = _event;
final lang = Localizations.localeOf(context).languageCode;
final t = EventUiText(lang);
final title = event.title.get(lang);
final short = event.shortDescription.get(lang);
final description = event.description.get(lang);
final dateLabel = eventDateRangeLabel(context, event);
final timeLabel = eventTimeRangeLabel(context, event);
final priceLabel = _priceLabel(event, t);
final ticketActionUrl =
_nonEmptyUrl(_actions?.ticketUrl ?? event.ticketUrl);
final websiteActionUrl =
_nonEmptyUrl(_actions?.websiteUrl ?? event.websiteUrl);
final onlineActionUrl =
_nonEmptyUrl(_actions?.onlineUrl ?? event.onlineUrl);
final organizerActionUrl =
_nonEmptyUrl(_actions?.organizerUrl ?? event.organizerUrl);
final hasRoute = event.latitude != null &&
event.longitude != null &&
(event.latitude!.abs() > 0.000001 || event.longitude!.abs() > 0.000001);
final locationValue = [event.venueName, event.formattedAddress]
.where((v) => v?.trim().isNotEmpty == true)
.join('\n');
return Scaffold(
appBar: AppBar(title: Text(t.event)),
body: ListView(
padding: const EdgeInsets.only(bottom: 36),
children: [
if (_loadingDetail) const LinearProgressIndicator(minHeight: 2),
RemoteImage(
url: event.imageUrl,
width: double.infinity,
height: 260,
fit: BoxFit.cover,
fallbackIcon: Icons.event_rounded,
semanticLabel: title,
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Wrap(
spacing: 8,
runSpacing: 8,
children: [
_Pill(icon: Icons.calendar_month_rounded, label: dateLabel),
if (!event.allDay && timeLabel.isNotEmpty)
_Pill(icon: Icons.schedule_rounded, label: timeLabel),
if (event.categories.isNotEmpty)
_Pill(
icon: Icons.local_activity_outlined,
label: event.categories.first.name,
),
],
),
const SizedBox(height: 18),
Text(
title,
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
fontWeight: FontWeight.w900,
height: 1.08,
),
),
if (short.isNotEmpty) ...[
const SizedBox(height: 10),
Text(
short,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: AppColors.mutedInk,
height: 1.45,
),
),
],
if (event.postponed || event.soldOut) ...[
const SizedBox(height: 14),
_StatusBanner(
text: event.postponed ? t.postponed : t.soldOut,
),
],
const SizedBox(height: 22),
_InfoRow(
icon: Icons.calendar_today_outlined,
title: t.date,
value: event.allDay
? '$dateLabel · ${t.allDayLower}'
: (timeLabel.isEmpty
? dateLabel
: '$dateLabel · $timeLabel'),
),
_InfoRow(
icon: Icons.location_on_outlined,
title: t.location,
value: locationValue,
),
if (priceLabel.isNotEmpty)
_InfoRow(
icon: Icons.confirmation_number_outlined,
title: t.admission,
value: priceLabel,
),
if (event.organizerName?.trim().isNotEmpty == true)
_InfoRow(
icon: Icons.apartment_rounded,
title: t.organizer,
value: event.organizerName!,
),
if (event.bookingRequired)
_InfoRow(
icon: Icons.event_available_outlined,
title: t.booking,
value: t.bookingRequired,
),
if (event.accessible)
_InfoRow(
icon: Icons.accessible_rounded,
title: t.accessibility,
value: t.accessible,
),
if (description.isNotEmpty) ...[
const SizedBox(height: 18),
Text(
t.about,
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(fontWeight: FontWeight.w900),
),
const SizedBox(height: 8),
Text(
description,
style: const TextStyle(height: 1.55, fontSize: 16),
),
],
if (ticketActionUrl != null ||
websiteActionUrl != null ||
onlineActionUrl != null ||
organizerActionUrl != null ||
hasRoute) ...[
const SizedBox(height: 24),
if (ticketActionUrl != null)
SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: () =>
const ExternalLinkService().open(ticketActionUrl),
icon: const Icon(Icons.confirmation_number_rounded),
label: Text(t.tickets),
),
),
if (websiteActionUrl != null) ...[
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: () =>
const ExternalLinkService().open(websiteActionUrl),
icon: const Icon(Icons.language_rounded),
label: Text(t.eventWebsite),
),
),
],
if (hasRoute) ...[
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: () => ExternalNavigationService.navigateTo(
latitude: event.latitude!,
longitude: event.longitude!,
label: title,
),
icon: const Icon(Icons.directions_rounded),
label: Text(t.routeThere),
),
),
],
if (onlineActionUrl != null) ...[
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: () =>
const ExternalLinkService().open(onlineActionUrl),
icon: const Icon(Icons.live_tv_rounded),
label: Text(t.joinOnline),
),
),
],
if (organizerActionUrl != null &&
organizerActionUrl != websiteActionUrl) ...[
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: () => const ExternalLinkService()
.open(organizerActionUrl),
icon: const Icon(Icons.language_rounded),
label: Text(t.organizerWebsite),
),
),
],
],
],
),
),
],
),
);
}
}
String? _nonEmptyUrl(String? value) {
final trimmed = value?.trim();
return trimmed == null || trimmed.isEmpty ? null : trimmed;
}
class _Pill extends StatelessWidget {
const _Pill({required this.icon, required this.label});
final IconData icon;
final String label;
@override
Widget build(BuildContext context) => Container(
padding: const EdgeInsets.symmetric(horizontal: 11, vertical: 8),
decoration: BoxDecoration(
color: AppColors.softRed,
borderRadius: BorderRadius.circular(999),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 17, color: AppColors.heartRed),
const SizedBox(width: 6),
Text(label, style: const TextStyle(fontWeight: FontWeight.w800)),
],
),
);
}
class _InfoRow extends StatelessWidget {
const _InfoRow(
{required this.icon, required this.title, required this.value});
final IconData icon;
final String title;
final String value;
@override
Widget build(BuildContext context) {
if (value.trim().isEmpty) return const SizedBox.shrink();
return Padding(
padding: const EdgeInsets.only(bottom: 15),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(icon, color: AppColors.heartRed),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title,
style: const TextStyle(fontWeight: FontWeight.w900)),
const SizedBox(height: 2),
Text(
value,
style:
const TextStyle(color: AppColors.mutedInk, height: 1.35),
),
],
),
),
],
),
);
}
}
class _StatusBanner extends StatelessWidget {
const _StatusBanner({required this.text});
final String text;
@override
Widget build(BuildContext context) => Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: AppColors.softRed,
borderRadius: BorderRadius.circular(14),
),
child: Text(
text,
style: const TextStyle(
fontWeight: FontWeight.w800,
color: AppColors.heartRed,
),
),
);
}
String _priceLabel(Event event, EventUiText t) {
if (event.free) return t.freeAdmission;
final min = event.priceMin;
final max = event.priceMax;
if (min == null && max == null) return '';
String money(double v) => v == v.roundToDouble()
? v.toStringAsFixed(0)
: v.toStringAsFixed(2).replaceAll('.', ',');
if (min != null && max != null && min != max) {
return '${money(min)}–${money(max)} ${event.currency}';
}
return '${money(min ?? max!)} ${event.currency}';
}

