forked from eatvio/eatvio_app
58 lines
2.7 KiB
Dart
58 lines
2.7 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/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(),
|
|
child: BlocBuilder<FeedCubit, FeedState>(
|
|
builder: (BuildContext context, FeedState state) {
|
|
if (state is FeedStateLoaded) {
|
|
return NotificationListener<ScrollNotification>(
|
|
onNotification: (notification) {
|
|
loadNewContent(notification, context.read<FeedCubit>().loadNextPage);
|
|
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();
|
|
}
|
|
return VisibilityDetector(
|
|
onVisibilityChanged: (info) {
|
|
final id = state.feedBlogPostList[index].id;
|
|
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.posts);
|
|
},
|
|
key: ValueKey(index),
|
|
child: BlogPostWidget(index: index),
|
|
);
|
|
},
|
|
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());
|
|
} else {
|
|
return Scaffold(backgroundColor: Colors.grey.shade200, body: const Loader());
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|