267 lines
11 KiB
Dart
267 lines
11 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:carousel_slider/carousel_slider.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
import 'package:tugas_akhir_supabase/screens/community/models/news_article.dart';
|
|
import 'package:tugas_akhir_supabase/core/theme/app_colors.dart';
|
|
|
|
class FeaturedNewsCarousel extends StatefulWidget {
|
|
final List<NewsArticle> featuredArticles;
|
|
|
|
const FeaturedNewsCarousel({super.key, required this.featuredArticles});
|
|
|
|
@override
|
|
_FeaturedNewsCarouselState createState() => _FeaturedNewsCarouselState();
|
|
}
|
|
|
|
class _FeaturedNewsCarouselState extends State<FeaturedNewsCarousel> {
|
|
int _currentIndex = 0;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (widget.featuredArticles.isEmpty) {
|
|
return SizedBox.shrink();
|
|
}
|
|
|
|
return Column(
|
|
children: [
|
|
// Section title
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
'Berita Utama',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.black87,
|
|
),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
// Navigate to all featured news
|
|
},
|
|
child: Text(
|
|
'Lihat Semua',
|
|
style: TextStyle(
|
|
color: AppColors.primary,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
// Carousel
|
|
CarouselSlider(
|
|
items:
|
|
widget.featuredArticles.map((article) {
|
|
return Builder(
|
|
builder: (BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: () async {
|
|
final uri = Uri.parse(article.sourceUrl);
|
|
if (await canLaunchUrl(uri)) {
|
|
await launchUrl(
|
|
uri,
|
|
mode: LaunchMode.externalApplication,
|
|
);
|
|
} else {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Tidak dapat membuka URL'),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
},
|
|
child: Container(
|
|
width: MediaQuery.of(context).size.width,
|
|
margin: EdgeInsets.symmetric(horizontal: 5.0),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(16),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.2),
|
|
spreadRadius: 1,
|
|
blurRadius: 5,
|
|
offset: Offset(0, 3),
|
|
),
|
|
],
|
|
),
|
|
child: Stack(
|
|
children: [
|
|
// Image
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(16),
|
|
child:
|
|
article.imageUrl != null
|
|
? CachedNetworkImage(
|
|
imageUrl: article.imageUrl!,
|
|
fit: BoxFit.cover,
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
placeholder:
|
|
(context, url) => Container(
|
|
color: Colors.grey[300],
|
|
child: Center(
|
|
child: CircularProgressIndicator(
|
|
valueColor:
|
|
AlwaysStoppedAnimation<
|
|
Color
|
|
>(AppColors.primary),
|
|
),
|
|
),
|
|
),
|
|
errorWidget:
|
|
(context, url, error) => Container(
|
|
color: Colors.grey[300],
|
|
child: Icon(
|
|
Icons.image_not_supported,
|
|
size: 50,
|
|
color: Colors.grey[600],
|
|
),
|
|
),
|
|
)
|
|
: Container(
|
|
color: Colors.grey[300],
|
|
child: Icon(
|
|
Icons.image_not_supported,
|
|
size: 50,
|
|
color: Colors.grey[600],
|
|
),
|
|
),
|
|
),
|
|
|
|
// Gradient overlay
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(16),
|
|
gradient: LinearGradient(
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
colors: [
|
|
Colors.transparent,
|
|
Colors.black.withOpacity(0.7),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
// Content
|
|
Positioned(
|
|
bottom: 0,
|
|
left: 0,
|
|
right: 0,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// Title
|
|
Text(
|
|
article.title,
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
shadows: [
|
|
Shadow(
|
|
blurRadius: 3.0,
|
|
color: Colors.black.withOpacity(
|
|
0.5,
|
|
),
|
|
offset: Offset(1, 1),
|
|
),
|
|
],
|
|
),
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
|
|
SizedBox(height: 8),
|
|
|
|
// Source and date
|
|
Row(
|
|
children: [
|
|
Icon(
|
|
Icons.source,
|
|
color: Colors.white70,
|
|
size: 14,
|
|
),
|
|
SizedBox(width: 4),
|
|
Expanded(
|
|
child: Text(
|
|
article.source,
|
|
style: TextStyle(
|
|
color: Colors.white70,
|
|
fontSize: 12,
|
|
),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}).toList(),
|
|
options: CarouselOptions(
|
|
height: 220,
|
|
viewportFraction: 0.9,
|
|
enlargeCenterPage: true,
|
|
enableInfiniteScroll: widget.featuredArticles.length > 1,
|
|
autoPlay: widget.featuredArticles.length > 1,
|
|
autoPlayInterval: Duration(seconds: 5),
|
|
autoPlayAnimationDuration: Duration(milliseconds: 800),
|
|
autoPlayCurve: Curves.fastOutSlowIn,
|
|
onPageChanged: (index, reason) {
|
|
setState(() {
|
|
_currentIndex = index;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
|
|
// Indicators
|
|
if (widget.featuredArticles.length > 1)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 12),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children:
|
|
widget.featuredArticles.asMap().entries.map((entry) {
|
|
return Container(
|
|
width: 8.0,
|
|
height: 8.0,
|
|
margin: EdgeInsets.symmetric(horizontal: 4.0),
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: (Theme.of(context).brightness == Brightness.dark
|
|
? Colors.white
|
|
: AppColors.primary)
|
|
.withOpacity(
|
|
_currentIndex == entry.key ? 1.0 : 0.4,
|
|
),
|
|
),
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|