import 'package:flutter/material.dart';
import '../../core/localization/app_localizations.dart';
import '../../core/theme/app_colors.dart';
import '../../models/language_learning_tour.dart';
import '../../models/localized_content.dart';
import '../../models/tour.dart';
import '../../models/transport_mode.dart';
import '../../widgets/tour_map.dart';
import '../../services/analytics_service.dart';
import 'learning_unit_page.dart';
class LanguageLearningTourRunPage extends StatefulWidget {
const LanguageLearningTourRunPage({super.key, required this.tour});
final LanguageLearningTour tour;
@override
State<LanguageLearningTourRunPage> createState() =>
_LanguageLearningTourRunPageState();
}
class _LanguageLearningTourRunPageState
extends State<LanguageLearningTourRunPage> {
int _stopIndex = 0;
@override
void initState() {
super.initState();
AnalyticsService.instance.track('learning_tour_run_start', properties: {
'learning_tour_id': widget.tour.definition.id,
'stop_count': widget.tour.stops.length
});
}
Tour _mapTour(BuildContext context) {
final l10n = AppLocalizations.of(context);
final lang = Localizations.localeOf(context).languageCode;
return Tour(
id: 'learning-${widget.tour.definition.id}',
title: LocalizedContent({lang: l10n.t(widget.tour.definition.titleKey)}),
subtitle:
LocalizedContent({lang: l10n.t(widget.tour.definition.subtitleKey)}),
category: 'learning',
durationMinutes: widget.tour.definition.durationMinutes,
distanceKm: 0,
highlight: false,
recommendedTransport: TransportMode.walking,
supportedTransportModes: const [TransportMode.walking],
stops: [
for (final stop in widget.tour.stops)
if (stop.location?.poiId != null)
TourStop(
poiId: stop.location!.poiId!,
title: LocalizedContent({lang: stop.title}),
guideText: LocalizedContent({
lang: stop.units
.map((u) => '${u.phraseDe} — ${u.help}')
.join('\n')
}),
walkMinutesFromPrevious: 0,
),
],
);
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final stop = widget.tour.stops[_stopIndex];
final mapTour = _mapTour(context);
final canNext = _stopIndex < widget.tour.stops.length - 1;
return Scaffold(
appBar: AppBar(title: Text(l10n.t(widget.tour.definition.titleKey))),
body: ListView(
padding: const EdgeInsets.fromLTRB(20, 8, 20, 110),
children: [
LinearProgressIndicator(
value: (_stopIndex + 1) / widget.tour.stops.length),
const SizedBox(height: 8),
Text(
l10n.tf('learningTourStopProgress', {
'current': '${_stopIndex + 1}',
'total': '${widget.tour.stops.length}'
}),
style: const TextStyle(
color: AppColors.mutedInk, fontWeight: FontWeight.w700),
),
const SizedBox(height: 16),
if (mapTour.stops.length >= 2)
TourMap(
tour: mapTour,
activeStopIndex: _stopIndex < mapTour.stops.length
? _stopIndex
: mapTour.stops.length - 1,
height: 260,
liveLocation: true,
showNavigationCard: true,
onStopSelected: (index) {
if (index >= 0 && index < widget.tour.stops.length) {
setState(() => _stopIndex = index);
}
},
),
const SizedBox(height: 20),
Text(stop.title,
style: Theme.of(context)
.textTheme
.headlineSmall
?.copyWith(fontWeight: FontWeight.w900)),
const SizedBox(height: 5),
Text(l10n.t('learningTourDoHere'),
style: const TextStyle(color: AppColors.mutedInk)),
const SizedBox(height: 12),
for (final unit in stop.units)
Card(
child: ListTile(
leading: const CircleAvatar(
backgroundColor: AppColors.softRed,
child: Icon(Icons.translate_rounded,
color: AppColors.heartRed)),
title: Text(unit.phraseDe,
style: const TextStyle(fontWeight: FontWeight.w900)),
subtitle: Text(unit.help,
maxLines: 2, overflow: TextOverflow.ellipsis),
trailing: const Icon(Icons.chevron_right_rounded),
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (_) => LearningUnitPage(unit: unit))),
),
),
],
),
bottomNavigationBar: SafeArea(
minimum: const EdgeInsets.all(16),
child: FilledButton.icon(
onPressed: canNext
? () {
AnalyticsService.instance.track('learning_tour_stop_advance',
properties: {
'learning_tour_id': widget.tour.definition.id,
'stop_index': _stopIndex
});
setState(() => _stopIndex++);
}
: () {
AnalyticsService.instance
.track('learning_tour_complete', properties: {
'learning_tour_id': widget.tour.definition.id,
'stop_count': widget.tour.stops.length
});
Navigator.of(context).pop();
},
icon:
Icon(canNext ? Icons.arrow_forward_rounded : Icons.check_rounded),
label: Text(
l10n.t(canNext ? 'learningTourNextStop' : 'learningTourFinish')),
),
),
);
}
}

