import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
import '../models/ad_campaign.dart';
import 'remote_image.dart';
class CampaignCreativeView extends StatelessWidget {
const CampaignCreativeView({
super.key,
required this.campaign,
this.height,
this.borderRadius,
this.semanticLabel,
});
final AdCampaign campaign;
final double? height;
final BorderRadius? borderRadius;
final String? semanticLabel;
@override
Widget build(BuildContext context) {
final radius = borderRadius ?? BorderRadius.zero;
final fallback = RemoteImage(
url: campaign.fallbackImageUrl ?? campaign.imageUrl,
width: double.infinity,
height: height,
fit: BoxFit.cover,
borderRadius: radius,
fallbackIcon: Icons.campaign_rounded,
fallbackIconSize: 52,
semanticLabel: semanticLabel ?? campaign.partnerName,
);
Widget child;
if (campaign.hasLottie) {
child = LayoutBuilder(
builder: (context, constraints) {
final mediaSize = MediaQuery.sizeOf(context);
final width = constraints.hasBoundedWidth
? constraints.maxWidth
: mediaSize.width;
final resolvedHeight = height ??
(constraints.hasBoundedHeight
? constraints.maxHeight
: mediaSize.height);
final lottieUrl = campaign.lottieUrlForSize(
Size(width, resolvedHeight),
);
if (lottieUrl == null) return fallback;
return ColoredBox(
color: campaign.backgroundColor,
child: Lottie.network(
lottieUrl,
width: double.infinity,
height: height,
fit: campaign.lottieFit,
repeat: true,
animate: true,
errorBuilder: (_, __, ___) => fallback,
),
);
},
);
} else {
child = fallback;
}
if (radius == BorderRadius.zero) return child;
return ClipRRect(borderRadius: radius, child: child);
}
}

