179 lines
5.3 KiB
Dart
179 lines
5.3 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../models/announcement.dart';
|
|
import '../services/announcement_service.dart';
|
|
|
|
class AnnouncementWidget extends StatefulWidget {
|
|
const AnnouncementWidget({super.key});
|
|
|
|
@override
|
|
State<AnnouncementWidget> createState() => _AnnouncementWidgetState();
|
|
}
|
|
|
|
class _AnnouncementWidgetState extends State<AnnouncementWidget> {
|
|
final PageController _pageController = PageController();
|
|
Timer? _scrollTimer;
|
|
Timer? _refreshTimer;
|
|
int _currentPage = 0;
|
|
List<Announcement> _announcements = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchAnnouncements();
|
|
_startAutoRefresh();
|
|
}
|
|
|
|
void _startAutoRefresh() {
|
|
_refreshTimer = Timer.periodic(const Duration(seconds: 30), (timer) {
|
|
_fetchAnnouncements();
|
|
});
|
|
}
|
|
|
|
void _fetchAnnouncements() async {
|
|
try {
|
|
final data = await Provider.of<AnnouncementService>(
|
|
context,
|
|
listen: false,
|
|
).fetchAnnouncements(activeOnly: true);
|
|
if (mounted) {
|
|
setState(() {
|
|
_announcements = data;
|
|
});
|
|
_startAutoScroll();
|
|
}
|
|
} catch (e) {
|
|
// Handle error
|
|
}
|
|
}
|
|
|
|
void _startAutoScroll() {
|
|
_scrollTimer?.cancel();
|
|
if (_announcements.length > 1) {
|
|
_scrollTimer = Timer.periodic(const Duration(seconds: 10), (timer) {
|
|
if (_pageController.hasClients) {
|
|
_currentPage++;
|
|
if (_currentPage >= _announcements.length) {
|
|
_currentPage = 0;
|
|
}
|
|
_pageController.animateToPage(
|
|
_currentPage,
|
|
duration: const Duration(milliseconds: 800),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_scrollTimer?.cancel();
|
|
_refreshTimer?.cancel();
|
|
_pageController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_announcements.isEmpty) {
|
|
// Show default placeholder if no announcements
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).cardTheme.color,
|
|
borderRadius: BorderRadius.circular(16),
|
|
border: Border.all(color: Colors.white10),
|
|
),
|
|
padding: const EdgeInsets.all(16),
|
|
child: const Center(
|
|
child: Text(
|
|
'우리 집에 오신 것을 환영합니다! 좋은 하루 되세요.',
|
|
style: TextStyle(color: Colors.white70, fontSize: 18),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(
|
|
context,
|
|
).cardTheme.color, // Slightly lighter/distinct background
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Stack(
|
|
children: [
|
|
PageView.builder(
|
|
controller: _pageController,
|
|
itemCount: _announcements.length,
|
|
itemBuilder: (context, index) {
|
|
final item = _announcements[index];
|
|
return Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Icon(
|
|
Icons.campaign,
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
size: 32,
|
|
),
|
|
const SizedBox(width: 12),
|
|
Text(
|
|
item.title,
|
|
style: Theme.of(context)
|
|
.textTheme
|
|
.headlineSmall
|
|
?.copyWith(
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
item.content,
|
|
style: Theme.of(
|
|
context,
|
|
).textTheme.bodyLarge?.copyWith(color: Colors.white),
|
|
maxLines: 3,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
// Page Indicator
|
|
if (_announcements.length > 1)
|
|
Positioned(
|
|
bottom: 16,
|
|
right: 16,
|
|
child: Row(
|
|
children: List.generate(_announcements.length, (index) {
|
|
return AnimatedContainer(
|
|
duration: const Duration(milliseconds: 300),
|
|
margin: const EdgeInsets.symmetric(horizontal: 4),
|
|
width: 8,
|
|
height: 8,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: _currentPage == index
|
|
? Theme.of(context).colorScheme.secondary
|
|
: Colors.white24,
|
|
),
|
|
);
|
|
}),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|