wesal/frontend/lib/screens/pages/feed_page.dart
2025-07-23 13:50:49 +03:00

419 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import '../create_post_screen.dart';
import '../../services/post_service.dart';
import '../../models/post_models.dart';
import '../../utils/invitation_utils.dart';
class FeedPage extends StatefulWidget {
@override
_FeedPageState createState() => _FeedPageState();
}
class _FeedPageState extends State<FeedPage> {
bool _isRefreshing = false;
bool _isLoading = true;
List<Post> _posts = [];
String _errorMessage = '';
@override
void initState() {
super.initState();
_loadPosts();
}
Future<void> _loadPosts() async {
setState(() {
_isLoading = true;
_errorMessage = '';
});
try {
final result = await PostService.getAllPosts();
setState(() {
if (result['success']) {
_posts = result['posts'];
} else {
_errorMessage = result['message'] ?? 'Failed to load posts';
}
_isLoading = false;
});
} catch (e) {
setState(() {
_errorMessage = 'Network error: $e';
_isLoading = false;
});
}
}
final List<Map<String, dynamic>> mockPosts = [
{
'id': '1',
'user': {
'displayName': 'Abu Khalid (Aqeel)',
'avatar': 'A',
'avatar_color': Color(0xFF32B0A5),
},
'content': 'Free hasawi khalas dates! Drop by my office to grab some 😉',
'timestamp': '42 minutes ago',
'likes': 12,
'comments': 3,
'isLiked': false,
},
{
'id': '2',
'user': {
'displayName': 'Sarah Khalid',
'avatar': 'S',
'avatar_color': Color(0xFF4600B9),
},
'content':
'Alhamdulillah, I am happy to tell you I have been blessed with a baby ❤️',
'timestamp': '4 hours ago',
'likes': 28,
'comments': 7,
'isLiked': true,
},
{
'id': '3',
'user': {
'displayName': 'Omar Hassan',
'avatar': 'O',
'avatar_color': Color(0xFF6A4C93),
},
'content':
'The sunset view from my balcony tonight is absolutely breathtaking. Sometimes you just need to pause and appreciate the beauty around us.',
'timestamp': '1 day ago',
'likes': 45,
'comments': 12,
'isLiked': false,
},
{
'id': '4',
'user': {
'displayName': 'Fatima Al-Zahra',
'avatar': 'F',
'avatar_color': Color(0xFF32B0A5),
},
'content':
'Finished reading an incredible book today. "The Seven Habits of Highly Effective People" - highly recommend it to anyone looking for personal development!',
'timestamp': '2 days ago',
'likes': 19,
'comments': 5,
'isLiked': true,
},
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Feed', style: TextStyle(fontWeight: FontWeight.w600)),
backgroundColor: Colors.white,
foregroundColor: Color(0xFF6A4C93),
elevation: 0,
bottom: PreferredSize(
preferredSize: Size.fromHeight(1),
child: Container(height: 1, color: Colors.grey[200]),
),
automaticallyImplyLeading: false,
actions: [
IconButton(
onPressed: _loadPosts,
icon: Icon(Icons.refresh),
tooltip: 'Refresh',
),
],
),
body: RefreshIndicator(
onRefresh: _refreshFeed,
child: _buildBody(),
),
floatingActionButton: FloatingActionButton(
onPressed: _navigateToCreatePost,
backgroundColor: Color(0xFF6A4C93),
child: Icon(Icons.edit, color: Colors.white),
),
);
}
Widget _buildBody() {
if (_isLoading) {
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6A4C93)),
),
);
}
if (_errorMessage.isNotEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.error_outline,
size: 64,
color: Colors.grey[400],
),
SizedBox(height: 16),
Text(
_errorMessage,
style: TextStyle(
fontSize: 16,
color: Colors.grey[600],
),
textAlign: TextAlign.center,
),
SizedBox(height: 16),
ElevatedButton(
onPressed: _loadPosts,
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF6A4C93),
foregroundColor: Colors.white,
),
child: Text('Try Again'),
),
],
),
);
}
if (_posts.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.post_add,
size: 64,
color: Colors.grey[400],
),
SizedBox(height: 16),
Text(
'Nothing here..',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: Colors.grey[600],
),
),
SizedBox(height: 8),
Text(
'Create the first post!',
style: TextStyle(
fontSize: 14,
color: Colors.grey[500],
),
),
],
),
);
}
return ListView.builder(
padding: EdgeInsets.symmetric(vertical: 8),
itemCount: _posts.length,
itemBuilder: (context, index) {
return PostCard(
post: _posts[index],
);
},
);
}
Future<void> _refreshFeed() async {
await _loadPosts();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Feed refreshed!'),
backgroundColor: Color(0xFF6A4C93),
),
);
}
Future<void> _navigateToCreatePost() async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => CreatePostScreen()),
);
// If post was created successfully, refresh the feed
if (result == true) {
_loadPosts();
}
}
}
class PostCard extends StatefulWidget {
final Post post;
const PostCard({Key? key, required this.post})
: super(key: key);
@override
_PostCardState createState() => _PostCardState();
}
class _PostCardState extends State<PostCard> {
Color _getAvatarColor(String displayName) {
final colors = [
Color(0xFF32B0A5),
Color(0xFF4600B9),
Color(0xFF6A4C93),
Color(0xFFFF6347),
Color(0xFF32CD32),
Color(0xFF9932CC),
];
int hash = displayName.hashCode;
return colors[hash.abs() % colors.length];
}
String _getAvatarLetter(String displayName) {
return displayName.isNotEmpty ? displayName[0].toUpperCase() : '?';
}
@override
Widget build(BuildContext context) {
final creator = widget.post.creator;
final avatarColor = _getAvatarColor(creator.displayName);
final avatarLetter = _getAvatarLetter(creator.displayName);
final relativeTime = InvitationUtils.getRelativeTime(widget.post.creationDate);
return Container(
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: Offset(0, 2),
),
],
),
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
CircleAvatar(
radius: 20,
backgroundColor: avatarColor,
child: Text(
avatarLetter,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
),
SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
creator.displayName,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
color: Colors.black87,
),
),
SizedBox(height: 2),
Text(
relativeTime,
style: TextStyle(color: Colors.grey[600], fontSize: 12),
),
],
),
),
IconButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('More options pressed')),
);
},
icon: Icon(Icons.more_horiz, color: Colors.grey[600]),
),
],
),
SizedBox(height: 12),
Text(
widget.post.body,
style: TextStyle(
fontSize: 15,
height: 1.4,
color: Colors.black87,
),
),
SizedBox(height: 16),
Row(
children: [
IconButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Like feature coming soon!')),
);
},
icon: Icon(
Icons.favorite_border,
color: Colors.grey[600],
size: 24,
),
),
Text(
'${widget.post.likes}',
style: TextStyle(
color: Colors.grey[700],
fontWeight: FontWeight.w500,
),
),
SizedBox(width: 16),
IconButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Comments feature coming soon!')),
);
},
icon: Icon(
Icons.chat_bubble_outline,
color: Colors.grey[600],
size: 24,
),
),
Text(
'${widget.post.comments}',
style: TextStyle(
color: Colors.grey[700],
fontWeight: FontWeight.w500,
),
),
Spacer(),
IconButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Share feature coming soon!')),
);
},
icon: Icon(
Icons.share_outlined,
color: Colors.grey[600],
size: 24,
),
),
],
),
],
),
),
);
}
}