fix styles on the add card page, move the parser to a separate file, change the styles of the page of one news item, add a parser for the subscription page, add the input field for the qr code

This commit is contained in:
2026-06-01 17:15:08 +03:00
parent 0b6757e26f
commit b9da1d3064
9 changed files with 300 additions and 307 deletions

View File

@@ -1,10 +1,8 @@
import 'package:flutter/material.dart';
import 'package:html/parser.dart' as html_parser;
import 'package:html/dom.dart' as dom;
import 'dart:convert';
import 'package:be_happy/di/service_locator.dart';
import '../../core/app_colors.dart';
import '../components/custom_app_bar.dart';
import '../components/content_parser.dart';
import '../../domain/usecase/get_news_by_id_usecase.dart';
class NewsDetailScreen extends StatefulWidget {
@@ -126,258 +124,17 @@ class _NewsDetailScreenState extends State<NewsDetailScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Текст новости: сначала проверяем textJson, потом text
if (news.textJson != null)
..._parseJsonText(news.textJson)
else if (news.text != null)
..._parseHtmlText(news.text),
// ✅ Используем вынесенный парсер
...ContentParser.parseContent(
textJson: news.textJson,
text: news.text,
),
],
),
);
}
List<Widget> _parseHtmlText(String htmlText) {
final parsedHtml = html_parser.parse(htmlText);
final List<dom.Node> elements = parsedHtml.body?.nodes ?? [];
return _parseHtmlElements(elements);
}
List<Widget> _parseHtmlElements(List<dom.Node> nodes) {
List<Widget> widgets = [];
for (final node in nodes) {
if (node is dom.Element) {
switch (node.localName) {
case 'h1':
case 'h2':
case 'h3':
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Text(
node.text,
style: const TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
);
break;
case 'p':
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Text(
node.text,
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
height: 1.5,
),
),
),
);
break;
case 'ul':
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: node.children
.where((element) => element is dom.Element && element.localName == 'li')
.map((dom.Element li) => Padding(
padding: const EdgeInsets.only(left: 16, top: 4, bottom: 4),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('',
style: TextStyle(
color: Colors.white70,
fontSize: 14,
)),
Expanded(
child: Text(
li.text,
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
height: 1.5,
),
),
),
],
),
))
.toList(),
),
),
);
break;
default:
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Text(
node.text,
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
),
),
),
);
}
} else if (node is dom.Text) {
widgets.add(
Text(
node.text,
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
),
),
);
}
}
return widgets;
}
// 🔹 Парсинг JSON-текста (новый метод)
List<Widget> _parseJsonText(String jsonString) {
try {
final dynamic data = jsonDecode(jsonString);
return _parseJsonNode(data);
} catch (e) {
return [
const Text(
'Ошибка отображения текста новости',
style: TextStyle(color: Colors.red, fontSize: 14),
),
];
}
}
List<Widget> _parseJsonNode(dynamic node) {
List<Widget> widgets = [];
if (node is List) {
// Если корень — массив, парсим каждый элемент
for (final item in node) {
widgets.addAll(_parseJsonNode(item));
}
} else if (node is Map<String, dynamic>) {
final type = node['type'] as String?;
final content = node['content'];
switch (type) {
case 'div':
if (content is List) {
widgets.addAll(_parseJsonNode(content));
}
break;
case 'h2':
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Text(
(content is List)
? content.join(' ')
: content?.toString() ?? '',
style: const TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
);
break;
case 'p':
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Text(
(content is List)
? content.join(' ')
: content?.toString() ?? '',
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
height: 1.5,
),
),
),
);
break;
case 'ul':
if (content is List) {
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: content
.where((item) => item is Map<String, dynamic>)
.map((item) => item as Map<String, dynamic>)
.where((item) => item['type'] == 'li')
.map((li) => Padding(
padding: const EdgeInsets.only(left: 16, top: 4, bottom: 4),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('',
style: TextStyle(
color: Colors.white70,
fontSize: 14,
)),
Expanded(
child: Text(
(li['content'] is List)
? li['content'].join(' ')
: li['content']?.toString() ?? '',
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
height: 1.5,
),
),
),
],
),
))
.toList(),
),
),
);
}
break;
default:
// Если тип неизвестен, просто выводим текст
widgets.add(
Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Text(
(content is List)
? content.join(' ')
: content?.toString() ?? type ?? 'unknown',
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
),
),
),
);
}
}
return widgets;
}
// 🔹 Удалены старые методы: _parseHtmlText, _parseHtmlElements, _parseJsonText, _parseJsonNode
String _formatDate(DateTime? date) {
if (date == null) return '';