forked from eatvio/eatvio_app
329 lines
16 KiB
Dart
329 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/enums/complained_content.dart';
|
|
import 'package:eatvio_app/core/enums/product_type.dart';
|
|
import 'package:eatvio_app/core/locator_service.dart';
|
|
import 'package:eatvio_app/core/widgets/bookmark_button.dart';
|
|
import 'package:eatvio_app/core/widgets/custom_popup.dart';
|
|
import 'package:eatvio_app/core/widgets/info_row.dart';
|
|
import 'package:eatvio_app/core/widgets/feed_icon_button.dart';
|
|
import 'package:eatvio_app/features/diets/presentation/widgets/info_bouble.dart';
|
|
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
|
|
import 'package:eatvio_app/core/theme/custom_text_style.dart';
|
|
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
|
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_cubit.dart';
|
|
import 'package:eatvio_app/generated/l10n.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter_html/flutter_html.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:share_plus/share_plus.dart';
|
|
import 'package:markdown/markdown.dart' as md;
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
class PlanWidget extends StatefulWidget {
|
|
final PlanModel plan;
|
|
final int index;
|
|
|
|
const PlanWidget({super.key, required this.plan, required this.index});
|
|
|
|
@override
|
|
State<PlanWidget> createState() => _PlanWidgetState();
|
|
}
|
|
|
|
class _PlanWidgetState extends State<PlanWidget> {
|
|
bool isMoreOptionsVisible = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = S.of(context);
|
|
return InkWell(
|
|
onTap: () => GoRouter.of(context).pushNamed('aboutPlanScreen', extra: {'slug': widget.plan.slug, 'index': widget.index, 'navigateToComments': false}),
|
|
child: Material(
|
|
borderRadius: BorderRadius.circular(12),
|
|
color: Colors.white,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Stack(
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
|
|
child: CachedNetworkImage(
|
|
imageUrl: '${sl<AppConfig>().baseUrl}/storage/${widget.plan.image}',
|
|
fit: BoxFit.cover,
|
|
width: MediaQuery.of(context).size.width,
|
|
height: 426,
|
|
),
|
|
),
|
|
Positioned(
|
|
left: 14,
|
|
child: BookmarkButton(
|
|
onTap: () async {
|
|
if (widget.plan.isFavorite) {
|
|
await context.read<PlansCubit>().removePlanFromFavorite(widget.plan.slug, widget.index);
|
|
} else {
|
|
await context.read<PlansCubit>().addPlanToFavorite(widget.plan.slug, widget.index);
|
|
}
|
|
},
|
|
color: appTheme.whiteA700,
|
|
isFavorite: widget.plan.isFavorite,
|
|
borderRadius: const BorderRadius.vertical(bottom: Radius.circular(12)),
|
|
width: 40,
|
|
height: 44,
|
|
size: 19,
|
|
),
|
|
),
|
|
Positioned(
|
|
right: 12,
|
|
top: 12,
|
|
child: SizedBox(
|
|
width: 32,
|
|
height: 24,
|
|
child: Material(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(4),
|
|
child: CustomPopup(
|
|
isAuthor: widget.plan.author.authorIsMe,
|
|
path: 'plan/${widget.plan.slug}',
|
|
id: widget.plan.id,
|
|
slug: widget.plan.slug,
|
|
complainedContentType: ComplainedContent.plan,
|
|
userSlug: widget.plan.author.authorSlug,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Positioned(
|
|
bottom: 16,
|
|
child: SizedBox(
|
|
width: MediaQuery.sizeOf(context).width,
|
|
child: InfoRow(plan: widget.plan, itemsBackgroundColor: appTheme.whiteA700, disableDescriptionOfTypes: true, type: ProductType.plan),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: Text(widget.plan.name, style: TextStyles.textMSemibold.copyWith(color: appTheme.black, fontSize: 18)),
|
|
),
|
|
if (widget.plan.details.isNotEmpty) ...[
|
|
const SizedBox(height: 12),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: Builder(
|
|
builder: (context) {
|
|
String details = widget.plan.details.length > 200
|
|
? '${widget.plan.details.substring(0, 200)}...'
|
|
: widget.plan.details;
|
|
// Автоматически преобразуем URL в markdown ссылки
|
|
// Сначала исправляем неправильный синтаксис markdown ссылок
|
|
final wrongLinkRegex = RegExp(r'\[(https?://[^\]]+|www\.[^\]]+)\]\(([^\)]+)\)');
|
|
details = details.replaceAllMapped(wrongLinkRegex, (match) {
|
|
final urlInText = match.group(1)!;
|
|
if (!urlInText.startsWith('http://') && !urlInText.startsWith('https://') && !urlInText.startsWith('www.')) {
|
|
return match.group(0)!;
|
|
}
|
|
final fullUrl = urlInText.startsWith('www.') ? 'https://$urlInText' : urlInText;
|
|
return '[$urlInText]($fullUrl)';
|
|
});
|
|
|
|
final linkRegex = RegExp(r'\[([^\]]+)\]\(([^\)]+)\)');
|
|
final existingLinks = <_LinkRange>[];
|
|
for (final linkMatch in linkRegex.allMatches(details)) {
|
|
existingLinks.add(_LinkRange(linkMatch.start, linkMatch.end));
|
|
}
|
|
final urlRegex = RegExp(r'(https?://[^\s\)]+|www\.[^\s\)]+)', caseSensitive: false);
|
|
String result = details;
|
|
int offset = 0;
|
|
for (final urlMatch in urlRegex.allMatches(details)) {
|
|
bool isInsideLink = existingLinks.any((link) =>
|
|
urlMatch.start >= link.start && urlMatch.end <= link.end);
|
|
if (!isInsideLink) {
|
|
final url = urlMatch.group(0)!;
|
|
final fullUrl = url.startsWith('www.') ? 'https://$url' : url;
|
|
final replacement = '[$url]($fullUrl)';
|
|
result = result.replaceRange(
|
|
urlMatch.start + offset,
|
|
urlMatch.end + offset,
|
|
replacement,
|
|
);
|
|
offset += replacement.length - url.length;
|
|
}
|
|
}
|
|
details = result;
|
|
|
|
// Гарантируем, что жирный текст будет распознан
|
|
final boldRegex = RegExp(r'\*\*([^*]+)\*\*');
|
|
details = details.replaceAllMapped(boldRegex, (match) {
|
|
return '<b>${match.group(1)}</b>';
|
|
});
|
|
|
|
return Html(
|
|
data: md.markdownToHtml(
|
|
details,
|
|
extensionSet: md.ExtensionSet.gitHubFlavored,
|
|
),
|
|
style: {
|
|
"body": Style(margin: Margins.zero, padding: HtmlPaddings.zero, fontSize: FontSize(16), lineHeight: const LineHeight(1.5)),
|
|
"p": Style(margin: Margins.only(bottom: 12), color: appTheme.black),
|
|
"b, strong": Style(fontWeight: FontWeight.bold, color: appTheme.black),
|
|
"h1, h2, h3, h4, h5, h6": Style(margin: Margins.only(top: 16, bottom: 8), color: appTheme.black, fontWeight: FontWeight.bold),
|
|
"h1": Style(fontSize: FontSize(24)),
|
|
"h2": Style(fontSize: FontSize(20)),
|
|
"h3": Style(fontSize: FontSize(18)),
|
|
"ul, ol": Style(margin: Margins.only(bottom: 12), padding: HtmlPaddings.only(left: 16)),
|
|
"li": Style(margin: Margins.only(bottom: 4), color: appTheme.black),
|
|
"strong, b": Style(fontWeight: FontWeight.bold, color: appTheme.black),
|
|
"em, i": Style(fontStyle: FontStyle.italic),
|
|
"code": Style(
|
|
backgroundColor: Colors.grey.shade100,
|
|
padding: HtmlPaddings.symmetric(horizontal: 4, vertical: 2),
|
|
fontFamily: 'monospace',
|
|
),
|
|
"pre": Style(backgroundColor: Colors.grey.shade100, padding: HtmlPaddings.all(12), margin: Margins.only(bottom: 12)),
|
|
"blockquote": Style(
|
|
padding: HtmlPaddings.only(left: 16),
|
|
margin: Margins.only(left: 8, bottom: 12),
|
|
fontStyle: FontStyle.italic,
|
|
color: Colors.grey.shade700,
|
|
),
|
|
"a": Style(color: Colors.blue, textDecoration: TextDecoration.underline),
|
|
"img": Style(width: Width(MediaQuery.of(context).size.width - 32)),
|
|
},
|
|
onLinkTap: (url, attributes, element) async {
|
|
if (url != null) {
|
|
final uri = Uri.parse(url);
|
|
if (await canLaunchUrl(uri)) {
|
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
|
}
|
|
}
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
if (widget.plan.details.length > 200)
|
|
const Padding(
|
|
padding: EdgeInsets.only(left: 16, top: 8),
|
|
child: Text(
|
|
'Показать ещё',
|
|
style: TextStyle(height: 1, color: Color(0xFF8B8B8B), fontSize: 14, fontWeight: FontWeight.w600),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
GestureDetector(
|
|
onTap: () {
|
|
GoRouter.of(context).pushNamed('profileScreen', extra: widget.plan.author.authorSlug);
|
|
},
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(left: 16.0),
|
|
child: ListTile(
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: Container(
|
|
width: 46,
|
|
height: 46,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
image: DecorationImage(image: NetworkImage(sl<AppConfig>().baseUrl + widget.plan.author.authorProfileIcon), fit: BoxFit.cover),
|
|
),
|
|
),
|
|
subtitle: Row(
|
|
children: [
|
|
InfoBouble(
|
|
radius: 12,
|
|
backgroundColor: appTheme.gray50,
|
|
padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 10),
|
|
children: [
|
|
Text('${widget.plan.author.authorPlaceInRating}-й в рейтинге', style: TextStyles.textXSSemibold.copyWith(color: appTheme.blueA400)),
|
|
],
|
|
),
|
|
const Spacer(),
|
|
Text(
|
|
'${widget.plan.packetsCount == 1 ? widget.plan.packet1Options?.price : '${s.from} ${widget.plan.minPriceOfOptions.toStringAsFixed(0)}'}₽',
|
|
style: TextStyles.headingXLSemibold.copyWith(color: appTheme.greenA700),
|
|
),
|
|
const SizedBox(width: 16),
|
|
],
|
|
),
|
|
title: Padding(
|
|
padding: const EdgeInsets.only(bottom: 4.0),
|
|
child: Text(widget.plan.author.authorName, style: TextStyles.textSSemibold.copyWith(color: appTheme.black)),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Padding(
|
|
padding: const EdgeInsets.only(left: 16),
|
|
child: Row(
|
|
children: [
|
|
FeedIconButton(
|
|
icon: widget.plan.wasLikedByMe ? Icons.favorite_rounded : Icons.favorite_border_rounded,
|
|
text: widget.plan.totalLikes.toString(),
|
|
iconColor: widget.plan.wasLikedByMe ? appTheme.blueA400 : null,
|
|
backgroundColor: widget.plan.wasLikedByMe ? appTheme.transparentBlue : null,
|
|
onTap: () async {
|
|
await context.read<PlansCubit>().likePlan(widget.plan.slug, widget.index);
|
|
},
|
|
),
|
|
const SizedBox(width: 12),
|
|
FeedIconButton(
|
|
icon: Icons.chat_bubble_outline,
|
|
text: widget.plan.totalComments.toString(),
|
|
onTap: () {
|
|
return GoRouter.of(
|
|
context,
|
|
).pushNamed('aboutPlanScreen', extra: {'slug': widget.plan.slug, 'index': widget.index, 'navigateToComments': true});
|
|
},
|
|
),
|
|
const SizedBox(width: 12),
|
|
FeedIconButton(
|
|
svgIcon: SvgPicture.asset('assets/icons/repost.svg'),
|
|
onTap: () async {
|
|
final box = context.findRenderObject() as RenderBox?;
|
|
final result = await SharePlus.instance.share(
|
|
ShareParams(
|
|
uri: Uri.parse('${sl<AppConfig>().baseUrl}/ru/diet/${widget.plan.id}'),
|
|
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
|
|
),
|
|
);
|
|
switch (result.status) {
|
|
case ShareResultStatus.success:
|
|
case ShareResultStatus.dismissed:
|
|
case ShareResultStatus.unavailable:
|
|
}
|
|
},
|
|
),
|
|
const Spacer(),
|
|
FeedIconButton(
|
|
svgIcon: SvgPicture.asset('assets/icons/eye2.svg'),
|
|
text: widget.plan.totalViews.toString(),
|
|
backgroundColor: Colors.transparent,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _LinkRange {
|
|
final int start;
|
|
final int end;
|
|
_LinkRange(this.start, this.end);
|
|
}
|