import 'dart:async'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../models/photo.dart'; import '../services/photo_service.dart'; class PhotoSlideshowWidget extends StatefulWidget { const PhotoSlideshowWidget({super.key}); @override State createState() => _PhotoSlideshowWidgetState(); } class _PhotoSlideshowWidgetState extends State { List _photos = []; int _currentIndex = 0; Timer? _timer; @override void initState() { super.initState(); _fetchPhotos(); } void _fetchPhotos() async { try { final photos = await Provider.of( context, listen: false, ).fetchPhotos(activeOnly: true); if (mounted) { setState(() { _photos = photos; }); _startSlideshow(); } } catch (e) { // Handle error } } void _startSlideshow() { _timer?.cancel(); if (_photos.length > 1) { _timer = Timer.periodic(const Duration(seconds: 30), (timer) { if (mounted) { setState(() { _currentIndex = (_currentIndex + 1) % _photos.length; }); } }); } } @override void dispose() { _timer?.cancel(); super.dispose(); } @override Widget build(BuildContext context) { if (_photos.isEmpty) { return Container( color: Colors.black, child: const Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.photo_library, size: 64, color: Colors.white24), SizedBox(height: 16), Text( 'No Photos Available', style: TextStyle(color: Colors.white54, fontSize: 24), ), ], ), ), ); } final currentPhoto = _photos[_currentIndex]; return Stack( fit: StackFit.expand, children: [ AnimatedSwitcher( duration: const Duration(milliseconds: 1000), child: Image.network( currentPhoto.url, key: ValueKey(currentPhoto.id), fit: BoxFit.cover, width: double.infinity, height: double.infinity, errorBuilder: (context, error, stackTrace) { return Container( color: Colors.grey[900], child: const Center( child: Icon( Icons.broken_image, color: Colors.white54, size: 48, ), ), ); }, ), ), // Gradient overlay for caption Positioned( left: 0, right: 0, bottom: 0, child: Container( decoration: const BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Colors.transparent, Colors.black87], ), ), padding: const EdgeInsets.all(24.0), child: Text( currentPhoto.caption, style: const TextStyle( color: Colors.white, fontSize: 20, fontWeight: FontWeight.w500, shadows: [ Shadow( color: Colors.black45, blurRadius: 4, offset: Offset(1, 1), ), ], ), textAlign: TextAlign.center, ), ), ), ], ); } }