forked from eatvio/eatvio_app
61 lines
3.0 KiB
Dart
61 lines
3.0 KiB
Dart
import 'package:eatvio_app/core/utils/pagination_utils.dart';
|
|
import 'package:eatvio_app/core/widgets/data_error_widget.dart';
|
|
import 'package:eatvio_app/core/widgets/loader.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_cubit.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_state.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/bloc/profile_cubit/profile_cubit.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/pages/feed_screen.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/widgets/blog_post_widget.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:visibility_detector/visibility_detector.dart';
|
|
|
|
class BlogPosts extends StatelessWidget {
|
|
const BlogPosts({super.key, required this.handleVisibility});
|
|
final Function({required double visibleFraction, required int id, required ViewableContentType type}) handleVisibility;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return RefreshIndicator(
|
|
onRefresh: () => context.read<FeedCubit>().loadFeedBlogPostList(currentUserId: context.read<FeedProfileCubit>().state.profile?.id),
|
|
child: BlocBuilder<FeedCubit, FeedState>(
|
|
builder: (BuildContext context, FeedState state) {
|
|
if (state is FeedStateLoaded) {
|
|
return NotificationListener<ScrollNotification>(
|
|
onNotification: (notification) {
|
|
loadNewContent(notification, () => context.read<FeedCubit>().loadNextPage(currentUserId: context.read<FeedProfileCubit>().state.profile?.id));
|
|
return false;
|
|
},
|
|
child: ListView.separated(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
physics: const BouncingScrollPhysics(),
|
|
itemBuilder: (context, index) {
|
|
if (index == state.feedBlogPostList.length) {
|
|
return const Loader();
|
|
}
|
|
final post = state.feedBlogPostList[index];
|
|
return VisibilityDetector(
|
|
key: ValueKey(post.id),
|
|
onVisibilityChanged: (info) {
|
|
handleVisibility(visibleFraction: info.visibleFraction, id: post.id, type: ViewableContentType.posts);
|
|
},
|
|
child: BlogPostWidget(post: post),
|
|
);
|
|
},
|
|
separatorBuilder: (context, index) => const SizedBox(height: 8),
|
|
itemCount: state.isLoading ? state.feedBlogPostList.length + 1 : state.feedBlogPostList.length,
|
|
),
|
|
);
|
|
} else if (state is FeedStateError) {
|
|
return DataErrorWidget(
|
|
onReload: () => context.read<FeedCubit>().loadFeedBlogPostList(currentUserId: context.read<FeedProfileCubit>().state.profile?.id),
|
|
);
|
|
} else {
|
|
return Scaffold(backgroundColor: Colors.grey.shade200, body: const Loader());
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|