diff --git a/lib/app_upgrade_simple.dart b/lib/app_upgrade_simple.dart index 21279ab..6585e4f 100644 --- a/lib/app_upgrade_simple.dart +++ b/lib/app_upgrade_simple.dart @@ -1122,13 +1122,14 @@ mixin _UpgradeDialogLogic on State { /// /// 采用自定义解析器,逐字符扫描并使用递归解析子内容,从而支持任意嵌套。 List _parseRichText(String content, ColorScheme colorScheme) { - final result = _parseRichTextInternal(content, colorScheme, 0, null); + final styles = _RichTextStyles(colorScheme); + final result = _parseRichTextInternal(content, styles, 0, null); return result.spans; } _RichTextParseResult _parseRichTextInternal( String text, - ColorScheme colorScheme, + _RichTextStyles styles, int startIndex, String? endToken, ) { @@ -1140,7 +1141,7 @@ mixin _UpgradeDialogLogic on State { if (buffer.isEmpty) return; spans.add(TextSpan( text: buffer.toString(), - style: _defaultRichTextStyle(colorScheme), + style: styles.defaultStyle, )); buffer.clear(); } @@ -1157,13 +1158,13 @@ mixin _UpgradeDialogLogic on State { // [高亮] if (currentChar == '[') { - final innerResult = _parseRichTextInternal(text, colorScheme, index + 1, ']'); + final innerResult = _parseRichTextInternal(text, styles, index + 1, ']'); if (innerResult.closed) { flushBuffer(); final innerText = text.substring(index + 1, innerResult.nextIndex - 1); spans.addAll(_applyStyleToSpans( innerResult.spans, - _highlightTextStyle(colorScheme), + styles.highlightStyle, innerText, )); index = innerResult.nextIndex; @@ -1183,7 +1184,7 @@ mixin _UpgradeDialogLogic on State { final codeText = text.substring(index + 1, closingIndex); spans.add(TextSpan( text: codeText, - style: _codeTextStyle(colorScheme), + style: styles.codeStyle, )); index = closingIndex + 1; handled = true; @@ -1196,13 +1197,13 @@ mixin _UpgradeDialogLogic on State { } // **粗体** else if (text.startsWith('**', index)) { - final innerResult = _parseRichTextInternal(text, colorScheme, index + 2, '**'); + final innerResult = _parseRichTextInternal(text, styles, index + 2, '**'); if (innerResult.closed) { flushBuffer(); final innerText = text.substring(index + 2, innerResult.nextIndex - 2); spans.addAll(_applyStyleToSpans( innerResult.spans, - _boldTextStyle(colorScheme), + styles.boldStyle, innerText, )); index = innerResult.nextIndex; @@ -1216,13 +1217,13 @@ mixin _UpgradeDialogLogic on State { } // __斜体__ else if (text.startsWith('__', index)) { - final innerResult = _parseRichTextInternal(text, colorScheme, index + 2, '__'); + final innerResult = _parseRichTextInternal(text, styles, index + 2, '__'); if (innerResult.closed) { flushBuffer(); final innerText = text.substring(index + 2, innerResult.nextIndex - 2); spans.addAll(_applyStyleToSpans( innerResult.spans, - _italicTextStyle(colorScheme), + styles.italicStyle, innerText, )); index = innerResult.nextIndex; @@ -1244,51 +1245,6 @@ mixin _UpgradeDialogLogic on State { return _RichTextParseResult(spans, index, false); } - TextStyle _defaultRichTextStyle(ColorScheme colorScheme) { - return TextStyle( - fontSize: 13, - color: colorScheme.onSurface.withOpacity(0.8), - height: 1.4, - ); - } - - TextStyle _boldTextStyle(ColorScheme colorScheme) { - return TextStyle( - fontSize: 13, - color: colorScheme.onSurface.withOpacity(0.9), - height: 1.4, - fontWeight: FontWeight.bold, - ); - } - - TextStyle _italicTextStyle(ColorScheme colorScheme) { - return TextStyle( - fontSize: 13, - color: colorScheme.onSurface.withOpacity(0.9), - height: 1.4, - fontStyle: FontStyle.italic, - ); - } - - TextStyle _codeTextStyle(ColorScheme colorScheme) { - return TextStyle( - fontSize: 12, - color: colorScheme.primary, - height: 1.4, - fontFamily: 'monospace', - backgroundColor: colorScheme.primaryContainer.withOpacity(0.2), - ); - } - - TextStyle _highlightTextStyle(ColorScheme colorScheme) { - return TextStyle( - fontSize: 13, - color: colorScheme.primary, - height: 1.4, - fontWeight: FontWeight.w600, - ); - } - List _applyStyleToSpans(List spans, TextStyle style, String fallbackText) { if (spans.isEmpty) { return [ @@ -2218,6 +2174,46 @@ class _ToastWidget extends StatelessWidget { typedef BoolCallback = void Function(bool success); +class _RichTextStyles { + _RichTextStyles(ColorScheme colorScheme) + : defaultStyle = TextStyle( + fontSize: 13, + color: colorScheme.onSurface.withOpacity(0.8), + height: 1.4, + ), + boldStyle = TextStyle( + fontSize: 13, + color: colorScheme.onSurface.withOpacity(0.9), + height: 1.4, + fontWeight: FontWeight.bold, + ), + italicStyle = TextStyle( + fontSize: 13, + color: colorScheme.onSurface.withOpacity(0.9), + height: 1.4, + fontStyle: FontStyle.italic, + ), + codeStyle = TextStyle( + fontSize: 12, + color: colorScheme.primary, + height: 1.4, + fontFamily: 'monospace', + backgroundColor: colorScheme.primaryContainer.withOpacity(0.2), + ), + highlightStyle = TextStyle( + fontSize: 13, + color: colorScheme.primary, + height: 1.4, + fontWeight: FontWeight.w600, + ); + + final TextStyle defaultStyle; + final TextStyle boldStyle; + final TextStyle italicStyle; + final TextStyle codeStyle; + final TextStyle highlightStyle; +} + class _RichTextParseResult { final List spans; final int nextIndex;