Добавлено форматирование текста #9
@@ -1,192 +0,0 @@
|
||||
//
|
||||
// Generated file. Do not edit.
|
||||
// This file is generated from template in file `flutter_tools/lib/src/flutter_plugins.dart`.
|
||||
//
|
||||
|
||||
// @dart = 3.7
|
||||
|
||||
import 'dart:io'; // flutter_ignore: dart_io_import.
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:path_provider_android/path_provider_android.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:path_provider_foundation/path_provider_foundation.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:path_provider_linux/path_provider_linux.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:url_launcher_linux/url_launcher_linux.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:path_provider_foundation/path_provider_foundation.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter_secure_storage_windows/flutter_secure_storage_windows.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:path_provider_windows/path_provider_windows.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:url_launcher_windows/url_launcher_windows.dart';
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
class _PluginRegistrant {
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
static void register() {
|
||||
if (Platform.isAndroid) {
|
||||
try {
|
||||
FilePickerIO.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`file_picker` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
PathProviderAndroid.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`path_provider_android` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
} else if (Platform.isIOS) {
|
||||
try {
|
||||
FilePickerIO.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`file_picker` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
PathProviderFoundation.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`path_provider_foundation` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
} else if (Platform.isLinux) {
|
||||
try {
|
||||
FilePickerLinux.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`file_picker` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
PackageInfoPlusLinuxPlugin.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`package_info_plus` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
PathProviderLinux.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`path_provider_linux` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
SharePlusLinuxPlugin.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`share_plus` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
UrlLauncherLinux.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`url_launcher_linux` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
} else if (Platform.isMacOS) {
|
||||
try {
|
||||
FilePickerMacOS.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`file_picker` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
PathProviderFoundation.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`path_provider_foundation` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
} else if (Platform.isWindows) {
|
||||
try {
|
||||
FilePickerWindows.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`file_picker` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
FlutterSecureStorageWindows.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`flutter_secure_storage_windows` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
PackageInfoPlusWindowsPlugin.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`package_info_plus` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
PathProviderWindows.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`path_provider_windows` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
SharePlusWindowsPlugin.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`share_plus` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
UrlLauncherWindows.registerWith();
|
||||
} catch (err) {
|
||||
print(
|
||||
'`url_launcher_windows` threw an error: $err. '
|
||||
'The app may not function as expected until you remove this plugin from pubspec.yaml'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -514,10 +514,6 @@ pubspec_parse
|
||||
3.6
|
||||
file:///Users/vitalijnecitajlo/.pub-cache/hosted/pub.dev/pubspec_parse-1.5.0/
|
||||
file:///Users/vitalijnecitajlo/.pub-cache/hosted/pub.dev/pubspec_parse-1.5.0/lib/
|
||||
readmore
|
||||
3.0
|
||||
file:///Users/vitalijnecitajlo/.pub-cache/hosted/pub.dev/readmore-3.0.0/
|
||||
file:///Users/vitalijnecitajlo/.pub-cache/hosted/pub.dev/readmore-3.0.0/lib/
|
||||
recase
|
||||
2.12
|
||||
file:///Users/vitalijnecitajlo/.pub-cache/hosted/pub.dev/recase-4.1.0/
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -17,6 +17,7 @@ import 'package:remever/inject.dart';
|
||||
import 'package:remever/screens/dialogs/alert_dialog.dart';
|
||||
import 'package:remever/screens/dialogs/replace_diaog.dart';
|
||||
import 'package:remever/services/tickets/tickets_interface.dart';
|
||||
import 'package:remever/widgets/markdown_readmore_text.dart';
|
||||
|
||||
class TicketCard extends StatefulWidget {
|
||||
const TicketCard({
|
||||
@@ -159,15 +160,15 @@ class _TicketCardState extends State<TicketCard> {
|
||||
}
|
||||
|
||||
Widget _buildText(BuildContext context) {
|
||||
return ReadMoreText(
|
||||
_isRolled ? widget.ticket.answer : widget.ticket.question,
|
||||
trimMode: TrimMode.Line,
|
||||
final text = _isRolled ? widget.ticket.answer : widget.ticket.question;
|
||||
|
||||
return MarkdownReadMoreText(
|
||||
text,
|
||||
trimLines: 3,
|
||||
trimCollapsedText: '\nРазвернуть',
|
||||
trimExpandedText: '\nСвернуть',
|
||||
style: Regular16px().style,
|
||||
moreStyle: Regular12px().style.copyWith(color: AppColors.primary_blue),
|
||||
lessStyle: Regular12px().style.copyWith(color: AppColors.primary_blue),
|
||||
linkStyle: Regular12px().style.copyWith(color: AppColors.primary_blue),
|
||||
expandText: 'Развернуть',
|
||||
collapseText: 'Свернуть',
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -43,14 +44,106 @@ class _CrudCollectionFullscreenFieldState
|
||||
if (widget.content != null) {
|
||||
_controller.text = widget.content!;
|
||||
}
|
||||
|
||||
_controller.addListener(_handleTextChange);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.removeListener(_handleTextChange);
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String? _lastText;
|
||||
|
||||
void _handleTextChange() {
|
||||
final currentText = _controller.text;
|
||||
|
||||
// Если текст не изменился — выходим
|
||||
if (_lastText == currentText) return;
|
||||
|
||||
// Находим позицию курсора
|
||||
final selection = _controller.selection;
|
||||
if (!selection.isValid) return;
|
||||
|
||||
// Проверяем, был ли добавлен \n
|
||||
if (_lastText != null && currentText.length > _lastText!.length) {
|
||||
final addedChars = currentText.substring(_lastText!.length);
|
||||
if (addedChars == '\n') {
|
||||
_handleNewLineInserted(selection.start - 1);
|
||||
}
|
||||
}
|
||||
|
||||
_lastText = currentText;
|
||||
}
|
||||
|
||||
void _handleNewLineInserted(int newlinePosition) {
|
||||
final text = _controller.text;
|
||||
|
||||
// Находим начало текущей строки (до \n)
|
||||
final lineStart = text.lastIndexOf('\n', newlinePosition - 1) + 1;
|
||||
final currentLine = text.substring(lineStart, newlinePosition);
|
||||
|
||||
String? prefix;
|
||||
|
||||
// Проверяем, начинается ли строка с префикса списка
|
||||
if (currentLine.startsWith('• ')) {
|
||||
prefix = '• ';
|
||||
} else {
|
||||
final match = RegExp(r'^(\d+)\.\s').firstMatch(currentLine);
|
||||
if (match != null) {
|
||||
final number = int.parse(match.group(1)!) + 1;
|
||||
prefix = '$number. ';
|
||||
}
|
||||
}
|
||||
|
||||
// Если строка пустая (только префикс) — выходим из списка
|
||||
if (currentLine.trim().isEmpty && prefix != null) {
|
||||
_exitListAt(newlinePosition);
|
||||
return;
|
||||
}
|
||||
|
||||
// Если есть префикс — добавляем новую строку с префиксом
|
||||
if (prefix != null) {
|
||||
final insertPos = newlinePosition + 1;
|
||||
final newText =
|
||||
text.substring(0, insertPos) + prefix + text.substring(insertPos);
|
||||
_controller.text = newText;
|
||||
|
||||
// Перемещаем курсор после префикса
|
||||
_controller.selection = TextSelection.collapsed(
|
||||
offset: insertPos + prefix.length,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Иначе — просто оставляем \n
|
||||
_controller.selection = TextSelection.collapsed(
|
||||
offset: newlinePosition + 1,
|
||||
);
|
||||
}
|
||||
|
||||
void _exitListAt(int newlinePosition) {
|
||||
final text = _controller.text;
|
||||
final insertPos = newlinePosition + 1;
|
||||
|
||||
// Удаляем префикс из новой строки
|
||||
final lineStart = text.lastIndexOf('\n', newlinePosition - 1) + 1;
|
||||
final currentLine = text.substring(lineStart, newlinePosition);
|
||||
final trimmed = currentLine.trim();
|
||||
|
||||
String replacement = '\n';
|
||||
if (trimmed.startsWith('•') || RegExp(r'^\d+\.').hasMatch(trimmed)) {
|
||||
replacement = '\n'; // просто перенос
|
||||
}
|
||||
|
||||
final newText =
|
||||
text.substring(0, insertPos) + replacement + text.substring(insertPos);
|
||||
_controller.text = newText;
|
||||
_controller.selection = TextSelection.collapsed(offset: insertPos);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return KeyboardSizeProvider(
|
||||
@@ -109,20 +202,142 @@ class _CrudCollectionFullscreenFieldState
|
||||
top: BorderSide(color: AppColors.gray, width: 1.w),
|
||||
),
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
const WSpace(8),
|
||||
_buildPasteButton(),
|
||||
_buildCopyButton(),
|
||||
_buildBoldButton(),
|
||||
_buildH1Button(),
|
||||
_buildBulletListButton(),
|
||||
_buildNumberedListButton(),
|
||||
_buildSubmitButton(),
|
||||
const WSpace(8),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBoldButton() {
|
||||
return _MenuButton(
|
||||
icon: Icon(Icons.format_bold),
|
||||
onTap: () => _wrapSelection('**', '**'),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildH1Button() {
|
||||
return _MenuButton(
|
||||
icon: Icon(Icons.format_italic_outlined),
|
||||
onTap: () => _insertAtLineStart('# '),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBulletListButton() {
|
||||
return _MenuButton(
|
||||
icon: Icon(Icons.list),
|
||||
onTap: () => _insertBulletList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildNumberedListButton() {
|
||||
return _MenuButton(
|
||||
icon: Icon(Icons.list_alt_outlined),
|
||||
onTap: () => _insertNumberedList(),
|
||||
);
|
||||
}
|
||||
|
||||
void _wrapSelection(String before, String after) {
|
||||
final text = _controller.text;
|
||||
final selection = _controller.selection;
|
||||
|
||||
if (!selection.isValid) {
|
||||
_controller.text = '$before${selection.textInside(text)}$after';
|
||||
_controller.selection = TextSelection.collapsed(
|
||||
offset: _controller.text.length - after.length,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final newText = text.replaceRange(
|
||||
selection.start,
|
||||
selection.end,
|
||||
'$before${selection.textInside(text)}$after',
|
||||
);
|
||||
_controller.text = newText;
|
||||
|
||||
final cursorPos =
|
||||
selection.start + before.length + selection.textInside(text).length;
|
||||
_controller.selection = TextSelection.collapsed(offset: cursorPos);
|
||||
}
|
||||
|
||||
void _insertAtLineStart(String prefix) {
|
||||
final text = _controller.text;
|
||||
final selection = _controller.selection;
|
||||
int start = selection.start;
|
||||
|
||||
// Находим начало строки
|
||||
int lineStart = text.lastIndexOf('\n', start - 1) + 1;
|
||||
if (lineStart == 0 && start == 0) lineStart = 0;
|
||||
|
||||
final newText = text.replaceRange(lineStart, lineStart, prefix);
|
||||
_controller.text = newText;
|
||||
|
||||
_controller.selection = TextSelection.collapsed(
|
||||
offset: lineStart + prefix.length + (selection.end - selection.start),
|
||||
);
|
||||
}
|
||||
|
||||
void _insertBulletList() {
|
||||
_insertListPrefix('• ');
|
||||
}
|
||||
|
||||
void _insertNumberedList() {
|
||||
_insertListPrefix('1. ');
|
||||
}
|
||||
|
||||
void _insertListPrefix(String prefix) {
|
||||
final text = _controller.text;
|
||||
final selection = _controller.selection;
|
||||
|
||||
String insertText;
|
||||
int newCursorPos;
|
||||
|
||||
if (selection.isCollapsed) {
|
||||
// Вставляем в позицию курсора
|
||||
insertText = prefix;
|
||||
newCursorPos = selection.start + prefix.length;
|
||||
} else {
|
||||
// Оборачиваем выделение
|
||||
final selected = selection.textInside(text);
|
||||
final lines = selected.split('\n').where((l) => l.isNotEmpty).toList();
|
||||
|
||||
final prefixed = lines
|
||||
.mapIndexed((index, line) {
|
||||
if (prefix == '• ') return '$prefix$line';
|
||||
final num = index + 1;
|
||||
return '$num. $line';
|
||||
})
|
||||
.join('\n');
|
||||
insertText = prefixed;
|
||||
newCursorPos = selection.start + prefixed.length;
|
||||
}
|
||||
|
||||
final newText = text.replaceRange(
|
||||
selection.start,
|
||||
selection.end,
|
||||
insertText,
|
||||
);
|
||||
_controller.text = newText;
|
||||
_controller.selection = TextSelection.collapsed(offset: newCursorPos);
|
||||
}
|
||||
|
||||
Widget _buildPasteButton() {
|
||||
return GestureDetector(
|
||||
onTap: _onPasteTap,
|
||||
@@ -205,6 +420,7 @@ class _CrudCollectionFullscreenFieldState
|
||||
maxLines: 99,
|
||||
maxLength: 250,
|
||||
cursorColor: AppColors.danger,
|
||||
textInputAction: TextInputAction.newline,
|
||||
decoration: const InputDecoration.collapsed(
|
||||
hintText: 'Введите содержимое',
|
||||
hintStyle: TextStyle(color: AppColors.gray),
|
||||
@@ -289,3 +505,18 @@ class _CrudCollectionFullscreenFieldState
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _MenuButton extends StatelessWidget {
|
||||
final Widget icon;
|
||||
final VoidCallback onTap;
|
||||
|
||||
const _MenuButton({required this.icon, required this.onTap});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(padding: const EdgeInsets.all(8).r, child: icon),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:remever/common/widgets/w_if.dart';
|
||||
import 'package:remever/common/widgets/wspace.dart';
|
||||
import 'package:remever/components/extensions/context.dart';
|
||||
import 'package:remever/database/database.dart';
|
||||
import 'package:remever/widgets/markdown_text.dart';
|
||||
import 'package:remever/widgets/primary_button.dart';
|
||||
|
||||
class InfoDialog extends StatelessWidget {
|
||||
@@ -60,15 +61,24 @@ class InfoDialog extends StatelessWidget {
|
||||
return Flexible(
|
||||
child: SingleChildScrollView(
|
||||
controller: ScrollController(),
|
||||
child: AppTypography(
|
||||
collection.desc,
|
||||
type: Regular14px(),
|
||||
maxLines: 9999,
|
||||
),
|
||||
child: MarkdownText(collection.desc),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Widget _buildText() {
|
||||
// return Flexible(
|
||||
// child: SingleChildScrollView(
|
||||
// controller: ScrollController(),
|
||||
// child: AppTypography(
|
||||
// collection.desc,
|
||||
// type: Regular14px(),
|
||||
// maxLines: 9999,
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
|
||||
///
|
||||
/// Название коллекции
|
||||
///
|
||||
|
||||
168
lib/widgets/markdown_readmore_text.dart
Normal file
168
lib/widgets/markdown_readmore_text.dart
Normal file
@@ -0,0 +1,168 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class MarkdownReadMoreText extends StatefulWidget {
|
||||
final String text;
|
||||
final int trimLines;
|
||||
final TextStyle style;
|
||||
final TextStyle linkStyle;
|
||||
final String expandText;
|
||||
final String collapseText;
|
||||
|
||||
const MarkdownReadMoreText(
|
||||
this.text, {
|
||||
super.key,
|
||||
this.trimLines = 3,
|
||||
required this.style,
|
||||
required this.linkStyle,
|
||||
this.expandText = 'Развернуть',
|
||||
this.collapseText = 'Свернуть',
|
||||
});
|
||||
|
||||
@override
|
||||
State<MarkdownReadMoreText> createState() => _MarkdownReadMoreTextState();
|
||||
}
|
||||
|
||||
class _MarkdownReadMoreTextState extends State<MarkdownReadMoreText> {
|
||||
bool _isExpanded = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final spans = _parseMarkdown(widget.text);
|
||||
final fullText = TextSpan(children: spans, style: widget.style);
|
||||
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final textPainter = TextPainter(
|
||||
text: fullText,
|
||||
maxLines: widget.trimLines,
|
||||
textDirection: TextDirection.ltr,
|
||||
)..layout(maxWidth: constraints.maxWidth);
|
||||
|
||||
final isOverflowed = textPainter.didExceedMaxLines;
|
||||
|
||||
if (!isOverflowed) {
|
||||
return SelectableText.rich(fullText);
|
||||
}
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SelectableText.rich(
|
||||
_isExpanded ? fullText : _getTrimmedSpan(textPainter, spans),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _isExpanded = !_isExpanded),
|
||||
child: Text(
|
||||
_isExpanded ? widget.collapseText : widget.expandText,
|
||||
style: widget.linkStyle,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
TextSpan _getTrimmedSpan(TextPainter painter, List<TextSpan> spans) {
|
||||
final trimmedText = StringBuffer();
|
||||
final remainingSpans = <TextSpan>[];
|
||||
|
||||
int charCount = 0;
|
||||
final maxChars =
|
||||
painter
|
||||
.getPositionForOffset(Offset(painter.width, painter.height))
|
||||
.offset;
|
||||
|
||||
for (final span in spans) {
|
||||
final spanText = span.text ?? '';
|
||||
if (charCount + spanText.length <= maxChars) {
|
||||
remainingSpans.add(span);
|
||||
charCount += spanText.length;
|
||||
} else {
|
||||
final remaining = maxChars - charCount;
|
||||
if (remaining > 0) {
|
||||
remainingSpans.add(
|
||||
TextSpan(text: spanText.substring(0, remaining), style: span.style),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return TextSpan(children: remainingSpans, style: widget.style);
|
||||
}
|
||||
|
||||
List<TextSpan> _parseMarkdown(String text) {
|
||||
final lines = text.split('\n');
|
||||
final spans = <TextSpan>[];
|
||||
|
||||
for (var line in lines) {
|
||||
final trimmed = line.trim();
|
||||
|
||||
// H1
|
||||
if (trimmed.startsWith('# ') && trimmed.length > 2) {
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: '${trimmed.substring(2)}\n',
|
||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Нумерованный список
|
||||
if (RegExp(r'^\d+\.\s').hasMatch(trimmed)) {
|
||||
final match = RegExp(r'^(\d+)\.\s(.+)').firstMatch(trimmed);
|
||||
if (match != null) {
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: '${match.group(0)!}\n',
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Маркированный список
|
||||
if (trimmed.startsWith('• ') && trimmed.length > 2) {
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: '• ${trimmed.substring(2)}\n',
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Жирный текст
|
||||
final boldRegex = RegExp(r'\*\*([^*]+)\*\*');
|
||||
var remaining = line;
|
||||
var boldProcessed = <TextSpan>[];
|
||||
|
||||
int lastEnd = 0;
|
||||
for (final match in boldRegex.allMatches(line)) {
|
||||
if (match.start > lastEnd) {
|
||||
boldProcessed.add(
|
||||
TextSpan(text: line.substring(lastEnd, match.start)),
|
||||
);
|
||||
}
|
||||
boldProcessed.add(
|
||||
TextSpan(
|
||||
text: match.group(1),
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
);
|
||||
lastEnd = match.end;
|
||||
}
|
||||
if (lastEnd < line.length) {
|
||||
boldProcessed.add(TextSpan(text: line.substring(lastEnd)));
|
||||
}
|
||||
|
||||
boldProcessed.add(const TextSpan(text: '\n'));
|
||||
spans.addAll(boldProcessed);
|
||||
}
|
||||
|
||||
return spans;
|
||||
}
|
||||
}
|
||||
95
lib/widgets/markdown_text.dart
Normal file
95
lib/widgets/markdown_text.dart
Normal file
@@ -0,0 +1,95 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:remever/common/resources.dart';
|
||||
|
||||
class MarkdownText extends StatelessWidget {
|
||||
final String text;
|
||||
final TextStyle? baseStyle;
|
||||
|
||||
const MarkdownText(this.text, {super.key, this.baseStyle});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SelectableText.rich(
|
||||
TextSpan(
|
||||
children: _parseMarkdown(text),
|
||||
style:
|
||||
baseStyle ??
|
||||
const TextStyle(fontSize: 14, color: AppColors.body_text),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<TextSpan> _parseMarkdown(String text) {
|
||||
final lines = text.split('\n');
|
||||
final spans = <TextSpan>[];
|
||||
|
||||
for (var line in lines) {
|
||||
final trimmed = line.trim();
|
||||
|
||||
// H1
|
||||
if (trimmed.startsWith('# ') && trimmed.length > 2) {
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: '${trimmed.substring(2)}\n',
|
||||
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Нумерованный список
|
||||
if (RegExp(r'^\d+\.\s').hasMatch(trimmed)) {
|
||||
final match = RegExp(r'^(\d+)\.\s(.+)').firstMatch(trimmed);
|
||||
if (match != null) {
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: '${match.group(0)!}\n',
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Маркированный список
|
||||
if (trimmed.startsWith('• ') && trimmed.length > 2) {
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: '${trimmed.substring(2)}\n',
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Жирный текст
|
||||
final boldRegex = RegExp(r'\*\*([^*]+)\*\*');
|
||||
var remaining = line;
|
||||
var boldProcessed = <TextSpan>[];
|
||||
|
||||
int lastEnd = 0;
|
||||
for (final match in boldRegex.allMatches(line)) {
|
||||
if (match.start > lastEnd) {
|
||||
boldProcessed.add(
|
||||
TextSpan(text: line.substring(lastEnd, match.start)),
|
||||
);
|
||||
}
|
||||
boldProcessed.add(
|
||||
TextSpan(
|
||||
text: match.group(1),
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
);
|
||||
lastEnd = match.end;
|
||||
}
|
||||
if (lastEnd < line.length) {
|
||||
boldProcessed.add(TextSpan(text: line.substring(lastEnd)));
|
||||
}
|
||||
|
||||
boldProcessed.add(const TextSpan(text: '\n'));
|
||||
spans.addAll(boldProcessed);
|
||||
}
|
||||
|
||||
return spans;
|
||||
}
|
||||
}
|
||||
@@ -1053,14 +1053,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.5.0"
|
||||
readmore:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: readmore
|
||||
sha256: e8fca2bd397b86342483b409e2ec26f06560a5963aceaa39b27f30722b506187
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.0"
|
||||
recase:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -33,7 +33,6 @@ dependencies:
|
||||
fps_widget: ^1.0.1+2
|
||||
provider: ^6.1.2
|
||||
freezed: ^2.5.8
|
||||
readmore: ^3.0.0
|
||||
fluttertoast: ^8.2.12
|
||||
pin_code_fields: ^8.0.1
|
||||
modal_bottom_sheet: ^3.0.0
|
||||
|
||||
Reference in New Issue
Block a user