forked from eatvio/eatvio_app
324 lines
16 KiB
Dart
324 lines
16 KiB
Dart
import 'package:cached_network_image/cached_network_image.dart';
|
||
import 'package:eatvio_app/core/app_config.dart';
|
||
import 'package:eatvio_app/core/locator_service.dart';
|
||
import 'package:eatvio_app/core/theme/custom_text_style.dart';
|
||
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||
import 'package:eatvio_app/core/utils/localization_utils.dart';
|
||
import 'package:eatvio_app/core/utils/toasts.dart';
|
||
import 'package:eatvio_app/core/widgets/custom_outlined_button.dart';
|
||
import 'package:eatvio_app/features/feed/presentation/bloc/recipes_cubit/recipes_cubit.dart';
|
||
import 'package:eatvio_app/features/recipe/models/my_recipe_model.dart';
|
||
import 'package:eatvio_app/features/recipe/presentation/cubit/my_recipes_cubit/my_recipes_cubit.dart';
|
||
import 'package:eatvio_app/generated/l10n.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:eatvio_app/features/create_recipe/presentation/bloc/create_recipe_cubit/create_recipe_cubit.dart';
|
||
import 'package:eatvio_app/features/create_recipe/presentation/pages/recipe_create_screen.dart';
|
||
import 'package:eatvio_app/core/widgets/markdown_html_widget.dart';
|
||
import 'package:flutter_svg/flutter_svg.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:share_plus/share_plus.dart';
|
||
|
||
class MyRecipeWidget extends StatefulWidget {
|
||
final MyRecipeModel recipe;
|
||
|
||
const MyRecipeWidget({super.key, required this.recipe});
|
||
|
||
@override
|
||
State<MyRecipeWidget> createState() => _MyRecipeWidgetState();
|
||
}
|
||
|
||
class _MyRecipeWidgetState extends State<MyRecipeWidget> {
|
||
late final int hours;
|
||
late final int minutes;
|
||
late final String timeTitle;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
hours = widget.recipe.cookingTime ~/ 60;
|
||
minutes = widget.recipe.cookingTime % 60;
|
||
if (hours > 0) {
|
||
timeTitle = '$hours ${getHourForm(hours)} $minutes ${getMinuteForm(minutes)}';
|
||
} else {
|
||
timeTitle = '$minutes ${getMinuteForm(minutes)}';
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final s = S.of(context);
|
||
return DecoratedBox(
|
||
decoration: BoxDecoration(borderRadius: BorderRadius.circular(12), color: Colors.white),
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Stack(
|
||
children: [
|
||
InkWell(
|
||
onTap: () async => GoRouter.of(context).pushNamed('aboutRecipesScreen', extra: {'recipe': widget.recipe.slug, 'navigateToComments': false}),
|
||
child: ClipRRect(
|
||
borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
|
||
child: CachedNetworkImage(
|
||
imageUrl: '${sl<AppConfig>().baseUrl}/storage/${widget.recipe.image}',
|
||
fit: BoxFit.fitWidth,
|
||
width: MediaQuery.of(context).size.width,
|
||
),
|
||
),
|
||
),
|
||
Positioned(
|
||
left: 8,
|
||
bottom: 8,
|
||
child: Wrap(
|
||
runSpacing: 8,
|
||
children: [
|
||
_iconButton('assets/icons/clock.svg', timeTitle),
|
||
const SizedBox(width: 8),
|
||
_iconButton('assets/icons/steps.svg', '${widget.recipe.steps?.length ?? 0} ${getStepForm(widget.recipe.steps?.length ?? 0)}'),
|
||
const SizedBox(width: 8),
|
||
_iconButton('assets/icons/calories.svg', '${widget.recipe.calories.toStringAsFixed(0)} ккал'),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
),
|
||
const SizedBox(height: 12),
|
||
Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Padding(
|
||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||
child: Text(widget.recipe.title!, style: TextStyles.textMSemibold.copyWith(color: appTheme.black)),
|
||
),
|
||
if (widget.recipe.description.isNotEmpty) ...[
|
||
const SizedBox(height: 12),
|
||
Padding(
|
||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||
child: Builder(
|
||
builder: (context) {
|
||
String description = widget.recipe.description.length > 200
|
||
? '${widget.recipe.description.substring(0, 200)}...'
|
||
: widget.recipe.description;
|
||
|
||
return MarkdownHtmlWidget(markdownText: description, fontSize: TextStyles.textSMedium.fontSize ?? 14, textColor: appTheme.black);
|
||
},
|
||
),
|
||
),
|
||
],
|
||
],
|
||
),
|
||
const SizedBox(height: 20),
|
||
Padding(
|
||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||
child: Column(
|
||
children: [
|
||
Row(
|
||
children: [
|
||
Expanded(
|
||
child: CustomOutlinedButton(
|
||
onPressed: () async {
|
||
final result = await sl<RecipesCubit>().deleteRecipe(widget.recipe.slug);
|
||
if (!result) {
|
||
showErrorToast('Не удалось удалить рецепт');
|
||
return;
|
||
}
|
||
showSuccessToast('Рецепт удален');
|
||
if (context.mounted) {
|
||
if (isDraft) {
|
||
context.read<MyRecipesCubit>().setDraftedState();
|
||
} else {
|
||
context.read<MyRecipesCubit>().getMyRecipes();
|
||
}
|
||
}
|
||
},
|
||
text: 'Удалить',
|
||
height: 52,
|
||
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.black),
|
||
buttonStyle: ButtonStyle(
|
||
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
|
||
backgroundColor: WidgetStatePropertyAll(appTheme.lightGray0),
|
||
side: const WidgetStatePropertyAll(BorderSide.none),
|
||
),
|
||
),
|
||
),
|
||
const SizedBox(width: 8),
|
||
Expanded(
|
||
child: isDraft
|
||
? CustomOutlinedButton(
|
||
onPressed: () async {
|
||
showDialog(
|
||
context: context,
|
||
barrierColor: Colors.black12,
|
||
builder: (context) => const Center(child: CircularProgressIndicator()),
|
||
);
|
||
final fullRecipe = await context.read<MyRecipesCubit>().getRecipeById(widget.recipe.slug);
|
||
if (context.mounted) {
|
||
Navigator.pop(context); // close dialog
|
||
await context.push(RecipeCreateScreen.path, extra: fullRecipe);
|
||
if (context.mounted) {
|
||
context.read<MyRecipesCubit>().setDraftedState();
|
||
}
|
||
}
|
||
},
|
||
height: 52,
|
||
buttonStyle: ButtonStyle(
|
||
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
|
||
backgroundColor: WidgetStatePropertyAll(appTheme.lightGray0),
|
||
side: const WidgetStatePropertyAll(BorderSide.none),
|
||
),
|
||
text: 'Редактировать',
|
||
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.black),
|
||
)
|
||
: PopupMenuButton<int>(
|
||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||
offset: const Offset(0, 50),
|
||
itemBuilder: (context) => [
|
||
if (!isDraft)
|
||
PopupMenuItem(
|
||
onTap: () async {
|
||
final box = context.findRenderObject() as RenderBox?;
|
||
await SharePlus.instance.share(
|
||
ShareParams(
|
||
uri: Uri.parse('\${sl<AppConfig>().baseUrl}/ru/recipe/\${widget.recipe.slug}'),
|
||
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
|
||
),
|
||
);
|
||
},
|
||
child: Row(
|
||
children: [
|
||
Icon(Icons.share, size: 20, color: appTheme.black),
|
||
const SizedBox(width: 8),
|
||
Text(S.of(context).share, style: TextStyles.textSMedium),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
child: CustomOutlinedButton(
|
||
onPressed: () {},
|
||
height: 52,
|
||
width: 52,
|
||
buttonStyle: ButtonStyle(
|
||
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
|
||
backgroundColor: WidgetStatePropertyAll(appTheme.blueA400),
|
||
side: const WidgetStatePropertyAll(BorderSide.none),
|
||
),
|
||
text: s.share,
|
||
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.whiteA700),
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
if (isDraft)
|
||
Padding(
|
||
padding: const EdgeInsets.only(top: 8),
|
||
child: SizedBox(
|
||
width: double.infinity,
|
||
child: CustomOutlinedButton(
|
||
onPressed: () async {
|
||
showDialog(
|
||
context: context,
|
||
barrierColor: Colors.black12,
|
||
builder: (context) => const Center(child: CircularProgressIndicator()),
|
||
);
|
||
final fullRecipe = await context.read<MyRecipesCubit>().getRecipeById(widget.recipe.slug);
|
||
final createCubit = sl<CreateRecipeCubit>();
|
||
await createCubit.recipeToEntity(fullRecipe);
|
||
|
||
if (context.mounted) {
|
||
Navigator.pop(context);
|
||
if (createCubit.validateAll()) {
|
||
final success = await createCubit.publishRecipe(true, widget.recipe.slug);
|
||
if (success && context.mounted) {
|
||
showSuccessToast('Черновик опубликован');
|
||
context.read<MyRecipesCubit>().setDraftedState();
|
||
} else {
|
||
showErrorToast('Не удалось опубликовать черновик');
|
||
}
|
||
} else {
|
||
showErrorToast('Заполните все необходимые поля для публикации рецепта');
|
||
if (context.mounted) {
|
||
await context.push(RecipeCreateScreen.path, extra: {
|
||
'recipe': fullRecipe,
|
||
'autoValidate': true,
|
||
});
|
||
if (context.mounted) {
|
||
context.read<MyRecipesCubit>().setDraftedState();
|
||
}
|
||
}
|
||
}
|
||
await createCubit.close();
|
||
}
|
||
},
|
||
height: 52,
|
||
buttonStyle: ButtonStyle(
|
||
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
|
||
backgroundColor: WidgetStatePropertyAll(appTheme.blueA400),
|
||
side: const WidgetStatePropertyAll(BorderSide.none),
|
||
),
|
||
text: 'Опубликовать',
|
||
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.whiteA700),
|
||
),
|
||
),
|
||
),
|
||
if (!isDraft)
|
||
Padding(
|
||
padding: const EdgeInsets.only(top: 8),
|
||
child: SizedBox(
|
||
width: double.infinity,
|
||
child: CustomOutlinedButton(
|
||
onPressed: () async {
|
||
final result = await context.read<MyRecipesCubit>().draftRecipe(widget.recipe.id);
|
||
if (result) {
|
||
showSuccessToast('Рецепт перенесен в черновики');
|
||
if (context.mounted) {
|
||
context.read<MyRecipesCubit>().getMyRecipes();
|
||
}
|
||
} else {
|
||
showErrorToast('Не удалось перенести в черновики');
|
||
}
|
||
},
|
||
height: 52,
|
||
buttonStyle: ButtonStyle(
|
||
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
|
||
backgroundColor: WidgetStatePropertyAll(appTheme.lightGray0),
|
||
side: const WidgetStatePropertyAll(BorderSide.none),
|
||
),
|
||
text: 'Снять с публикации',
|
||
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.black),
|
||
),
|
||
),
|
||
),
|
||
const SizedBox(height: 16),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
bool get isDraft => widget.recipe.state == 'drafted';
|
||
|
||
Container _iconButton(String iconPath, [String? text]) {
|
||
return Container(
|
||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||
decoration: BoxDecoration(color: appTheme.whiteA700, borderRadius: BorderRadius.circular(16)),
|
||
height: 32,
|
||
child: Row(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
SvgPicture.asset(iconPath),
|
||
if (text != null) const SizedBox(width: 8),
|
||
if (text != null)
|
||
Text(
|
||
text,
|
||
style: const TextStyle(fontSize: 12, color: Colors.black, fontWeight: FontWeight.w500),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|