Files
eatvio_app/lib/features/feed/presentation/widgets/plans.dart
alex-chevski 0df474b2b5 fix: plans - like and bookmark in consultant profile (#582)
- decouple PlanWidget from feed PlansCubit via owner-provided callbacks
- add like/favorite actions to profile PlansCubit: slug lookup, optimistic
  update, rollback of the affected plan, per-slug debounce, isClosed guards
- pass slug through like/favorite/comments events; guard out-of-range index
  in feed plans handlers and deep-link about_plan sync
- capture post like intent before await in profile blog post widget
- replace null force-unwraps in profile screen, guard indexOf == -1
- tests: profile plan like/bookmark, slug sync, bounds guards, #700 regression
2026-11-20 16:30:13 +03:00

76 lines
3.6 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/plans_cubit/plans_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_state.dart';
import 'package:eatvio_app/features/feed/presentation/pages/feed_screen.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/filter_badge.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/plan_widget.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/plans_filter_bottom_sheet.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:visibility_detector/visibility_detector.dart';
class Plans extends StatelessWidget {
const Plans({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<PlansCubit>().loadPlans(),
child: BlocBuilder<PlansCubit, PlansState>(
builder: (BuildContext context, PlansState state) {
if (state.isLoading) {
return Scaffold(backgroundColor: Colors.grey.shade200, body: const Loader());
} else if (state.errorMessage != null) {
return DataErrorWidget(onReload: () => context.read<PlansCubit>().loadPlans());
}
return Stack(
children: [
const SizedBox.expand(),
NotificationListener<ScrollNotification>(
onNotification: (notification) {
loadNewContent(notification, context.read<PlansCubit>().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.plans[index].id;
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.plans);
},
child: PlanWidget(
plan: state.plans[index],
index: index,
onLikeTap: (slug) => context.read<PlansCubit>().likePlan(slug, index),
onToggleFavorite: (slug, isFavorite) => isFavorite
? context.read<PlansCubit>().removePlanFromFavorite(slug, index)
: context.read<PlansCubit>().addPlanToFavorite(slug, index),
onPlanChanged: () => context.read<PlansCubit>().loadPlans(),
),
);
},
separatorBuilder: (context, index) {
return const SizedBox(height: 8);
},
itemCount: state.plans.length,
),
),
Positioned(
right: 0,
bottom: 74,
child: FilterBadge(filterBottomSheet: PlansFilterBottomSheet(plansCubit: context.read<PlansCubit>())),
),
],
);
},
),
);
}
}