forked from eatvio/eatvio_app
68 lines
2.7 KiB
Dart
68 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/recipes_cubit/recipes_cubit.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/bloc/recipes_cubit/recipes_state.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/pages/feed_screen.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/widgets/recipes_widget.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:visibility_detector/visibility_detector.dart';
|
|
|
|
class Recipes extends StatelessWidget {
|
|
const Recipes({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<RecipesCubit>().loadRecipesList(),
|
|
child: BlocBuilder<RecipesCubit, RecipesState>(
|
|
builder: (BuildContext context, RecipesState state) {
|
|
if (state.isLoading) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.grey.shade200,
|
|
body: const Loader(),
|
|
);
|
|
}
|
|
if (state.errorMessage != null) {
|
|
return DataErrorWidget(
|
|
onReload: () async {
|
|
context.read<RecipesCubit>().loadRecipesList();
|
|
},
|
|
);
|
|
}
|
|
|
|
return NotificationListener<ScrollNotification>(
|
|
onNotification: (notification) {
|
|
loadNewContent(notification, context.read<RecipesCubit>().loadNextPage);
|
|
return false;
|
|
},
|
|
child: ListView.separated(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
shrinkWrap: true,
|
|
physics: const BouncingScrollPhysics(),
|
|
itemBuilder: (context, index) {
|
|
return VisibilityDetector(
|
|
key: ValueKey(index),
|
|
onVisibilityChanged: (info) {
|
|
final id = state.recipesList[index].id;
|
|
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.recipes);
|
|
},
|
|
child: RecipesWidget(
|
|
recipe: state.recipesList[index],
|
|
index: index,
|
|
),
|
|
);
|
|
},
|
|
separatorBuilder: (context, index) {
|
|
return const SizedBox(height: 8);
|
|
},
|
|
itemCount: state.recipesList.length,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|