import 'package:flutter/material.dart';
import '../../core/localization/app_localizations.dart';
import '../../core/theme/app_colors.dart';
import '../../data/app_data_scope.dart';
import '../../models/ad_campaign.dart';
import '../../services/campaign_action_service.dart';
import '../../services/analytics_service.dart';
import '../../widgets/campaign_creative_view.dart';
import '../../widgets/media_gallery.dart';
class CampaignLandingPage extends StatefulWidget {
const CampaignLandingPage({super.key, required this.campaign});
final AdCampaign campaign;
@override
State<CampaignLandingPage> createState() => _CampaignLandingPageState();
}
class _CampaignLandingPageState extends State<CampaignLandingPage> {
late AdCampaign _campaign;
bool _loadingDetail = true;
@override
void initState() {
super.initState();
_campaign = widget.campaign;
AnalyticsService.instance.track('campaign_landing_view',
properties: {'campaign_id': widget.campaign.id});
WidgetsBinding.instance.addPostFrameCallback((_) => _loadDetail());
}
Future<void> _loadDetail() async {
try {
final detail =
await AppDataScope.read(context).campaignDetail(_campaign.id);
if (!mounted) return;
setState(() {
_campaign = detail;
_loadingDetail = false;
});
} catch (_) {
if (!mounted) return;
setState(() => _loadingDetail = false);
}
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final campaign = _campaign;
final lang = Localizations.localeOf(context).languageCode;
final headline = campaign.headline.get(lang).trim();
final body = campaign.body.get(lang).trim();
final hasAction = campaign.target.hasTarget ||
campaign.landingPageUrl?.trim().isNotEmpty == true;
return Scaffold(
appBar: AppBar(title: Text(l10n.t('partnerOffer'))),
body: ListView(
padding: const EdgeInsets.fromLTRB(20, 12, 20, 40),
children: [
Row(
children: [
Text(
l10n.t('advertisement'),
style: const TextStyle(
color: AppColors.heartRed,
fontWeight: FontWeight.w900,
letterSpacing: 1.4,
),
),
const Spacer(),
Flexible(
child: Text(
campaign.partnerName,
textAlign: TextAlign.end,
overflow: TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.labelLarge
?.copyWith(fontWeight: FontWeight.w800),
),
),
],
),
const SizedBox(height: 16),
LayoutBuilder(
builder: (context, constraints) {
final height = (constraints.maxWidth * 0.62).clamp(220.0, 520.0);
return CampaignCreativeView(
campaign: campaign,
height: height,
borderRadius: BorderRadius.circular(26),
semanticLabel:
headline.isEmpty ? campaign.partnerName : headline,
);
},
),
if (_loadingDetail) ...[
const SizedBox(height: 10),
const LinearProgressIndicator(minHeight: 2),
],
if (campaign.galleryUrls.isNotEmpty) ...[
const SizedBox(height: 28),
MediaGallery(
urls: campaign.galleryUrls,
title: l10n.t('moreImages'),
),
],
if (headline.isNotEmpty) ...[
const SizedBox(height: 22),
Text(
headline,
style: Theme.of(context)
.textTheme
.headlineSmall
?.copyWith(fontWeight: FontWeight.w900),
),
],
if (body.isNotEmpty) ...[
const SizedBox(height: 12),
Text(body, style: Theme.of(context).textTheme.bodyLarge),
],
if (hasAction) ...[
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: () => CampaignActionService.open(context, campaign),
icon: const Icon(Icons.arrow_outward_rounded),
label: Text(
campaign.buttonLabel.get(lang).trim().isEmpty
? l10n.t('readMore')
: campaign.buttonLabel.get(lang),
),
),
),
],
],
),
);
}
}

