import 'package:carousel_slider/carousel_slider.dart'; import 'package:flutter/material.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:url_launcher/url_launcher.dart'; import '../models/OfferModel.dart'; class NetworkImageCarousel extends StatefulWidget { final List offers; // Changed from imageUrls to offers final double height; final bool autoPlay; final Duration autoPlayInterval; final bool showIndicators; final BorderRadius? borderRadius; final BoxFit fit; const NetworkImageCarousel({ Key? key, required this.offers, // Changed parameter this.height = 200.0, this.autoPlay = true, this.autoPlayInterval = const Duration(seconds: 10), this.showIndicators = true, this.borderRadius, this.fit = BoxFit.cover, }) : super(key: key); @override State createState() => _NetworkImageCarouselState(); } class _NetworkImageCarouselState extends State { int _currentIndex = 0; final CarouselSliderController _carouselController = CarouselSliderController(); // Function to open link Future _openLink(String link) async { if (link.isEmpty || link == "https://beem-it.tn") { // Default link, don't open return; } final Uri url = Uri.parse(link); try { if (await canLaunchUrl(url)) { await launchUrl( url, mode: LaunchMode.externalApplication, // Open in browser ); } else { throw 'Could not launch $link'; } } catch (e) { print('Error opening link: $e'); } } @override Widget build(BuildContext context) { // Filter only enabled offers final enabledOffers = widget.offers.where((offer) => offer.status == 'ENABLED').toList(); if (enabledOffers.isEmpty) { return SizedBox(height: widget.height); // Empty space if no offers } return Stack( alignment: Alignment.center, children: [ // Carousel CarouselSlider( carouselController: _carouselController, options: CarouselOptions( height: widget.height, autoPlay: widget.autoPlay, autoPlayInterval: widget.autoPlayInterval, enlargeCenterPage: true, viewportFraction: 1.0, onPageChanged: (index, reason) { setState(() { _currentIndex = index; }); }, ), items: enabledOffers.map((offer) { return Builder( builder: (BuildContext context) { return GestureDetector( onTap: () => _openLink(offer.link), // Open link on tap child: Container( width: MediaQuery.of(context).size.width, margin: const EdgeInsets.symmetric(horizontal: 5.0), decoration: BoxDecoration( borderRadius: widget.borderRadius ?? BorderRadius.circular(12), ), child: ClipRRect( borderRadius: widget.borderRadius ?? BorderRadius.circular(12), child: CachedNetworkImage( imageUrl: offer.image.url, // Get URL from FileModel fit: widget.fit, placeholder: (context, url) => Container( color: Colors.grey[200], child: const Center( child: CircularProgressIndicator(), ), ), errorWidget: (context, url, error) => Container( color: Colors.grey[300], child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.error, color: Colors.red, size: 50, ), const SizedBox(height: 8), Text( offer.title, style: const TextStyle( color: Colors.black54, fontSize: 14, ), ), ], ), ), ), ), ), ); }, ); }).toList(), ), // Indicators if (widget.showIndicators && enabledOffers.length > 1) Positioned( bottom: 10, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: enabledOffers.asMap().entries.map((entry) { return GestureDetector( onTap: () => _carouselController.animateToPage(entry.key), child: Container( width: _currentIndex == entry.key ? 12 : 8, height: 8, margin: const EdgeInsets.symmetric(horizontal: 4.0), decoration: BoxDecoration( borderRadius: BorderRadius.circular(4), color: _currentIndex == entry.key ? Colors.white : Colors.white.withOpacity(0.4), ), ), ); }).toList(), ), ), ], ); } }