Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.3"
version: "1.1.4"
http:
dependency: transitive
description:
Expand Down Expand Up @@ -195,10 +195,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
url: "https://pub.dev"
source: hosted
version: "1.16.0"
version: "1.17.0"
nested:
dependency: transitive
description:
Expand Down Expand Up @@ -288,10 +288,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55
url: "https://pub.dev"
source: hosted
version: "0.7.6"
version: "0.7.7"
tuple:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion lib/custom_widgets/code_field.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gpt_markdown/gpt_markdown.dart';

/// A widget that displays code with syntax highlighting and a copy button.
///
Expand Down Expand Up @@ -27,7 +28,7 @@ class _CodeFieldState extends State<CodeField> {
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.onInverseSurface,
color: GptMarkdownTheme.of(context).codeBlockBackgroundColor,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down
68 changes: 37 additions & 31 deletions lib/markdown_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ class HrLine extends BlockMd {
) {
var thickness = GptMarkdownTheme.of(context).hrLineThickness;
var color = GptMarkdownTheme.of(context).hrLineColor;
return CustomDivider(
height: thickness,
color: config.style?.color ?? color,
);
return CustomDivider(height: thickness, color: color);
}
}

Expand Down Expand Up @@ -474,25 +471,22 @@ class HighlightedText extends InlineMd {
);
}

var style =
config.style?.copyWith(
fontWeight: FontWeight.bold,
background:
Paint()
..color = GptMarkdownTheme.of(context).highlightColor
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round,
) ??
TextStyle(
fontWeight: FontWeight.bold,
background:
Paint()
..color = GptMarkdownTheme.of(context).highlightColor
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round,
);

return TextSpan(text: highlightedText, style: style);
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 5),
decoration: BoxDecoration(
color: GptMarkdownTheme.of(context).highlightColor,
borderRadius: BorderRadius.circular(4),
),
child: Text(
highlightedText,
style:
config.style?.copyWith(color: Colors.grey, fontSize: 13) ??
const TextStyle(color: Colors.grey, fontSize: 13),
),
),
);
}
}

Expand Down Expand Up @@ -849,19 +843,31 @@ class ATagMd extends InlineMd {

var ending = text.substring(urlEnd + 1);

var theme = GptMarkdownTheme.of(context);
var linkConfig = config.copyWith(
style:
config.style?.copyWith(
color: theme.linkColor,
decorationColor: theme.linkColor,
) ??
TextStyle(color: theme.linkColor, decorationColor: theme.linkColor),
);

var endingSpans = MarkdownComponent.generate(
context,
ending,
config,
false,
);
var theme = GptMarkdownTheme.of(context);

var linkTextSpan = TextSpan(
children: MarkdownComponent.generate(context, linkText, config, false),
style: config.style?.copyWith(
color: theme.linkColor,
decorationColor: theme.linkColor,
children: MarkdownComponent.generate(
context,
linkText,
linkConfig,
false,
),
style: linkConfig.style,
);

// Use custom builder if provided
Expand Down Expand Up @@ -1103,7 +1109,7 @@ class TableMd extends BlockMd {
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
border: TableBorder.all(
width: 1,
color: Theme.of(context).colorScheme.onSurface,
color: GptMarkdownTheme.of(context).hrLineColor,
),
children:
value
Expand All @@ -1122,9 +1128,9 @@ class TableMd extends BlockMd {
(hasHeader && entry.key == 0)
? BoxDecoration(
color:
Theme.of(
GptMarkdownTheme.of(
context,
).colorScheme.surfaceContainerHighest,
).codeBlockBackgroundColor,
)
: null,
children: List.generate(maxCol, (index) {
Expand Down
27 changes: 13 additions & 14 deletions lib/md_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,29 @@ class MdWidget extends StatefulWidget {
class _MdWidgetState extends State<MdWidget> {
List<InlineSpan> list = [];
@override
void initState() {
super.initState();
list = MarkdownComponent.generate(
widget.context,
widget.exp,
widget.config,
widget.includeGlobalComponents,
);
void didChangeDependencies() {
super.didChangeDependencies();
_updateList();
}

@override
void didUpdateWidget(covariant MdWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.exp != widget.exp ||
!oldWidget.config.isSame(widget.config)) {
list = MarkdownComponent.generate(
context,
widget.exp,
widget.config,
widget.includeGlobalComponents,
);
_updateList();
}
}

void _updateList() {
list = MarkdownComponent.generate(
context,
widget.exp,
widget.config,
widget.includeGlobalComponents,
);
}

@override
Widget build(BuildContext context) {
// List<InlineSpan> list = MarkdownComponent.generate(
Expand Down
17 changes: 17 additions & 0 deletions lib/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
required this.hrLineColor,
required this.linkColor,
required this.linkHoverColor,
required this.codeBlockBackgroundColor,
});

/// A factory constructor for `GptMarkdownThemeData`.
Expand All @@ -30,6 +31,7 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
Color? hrLineColor,
Color? linkColor,
Color? linkHoverColor,
Color? codeBlockBackgroundColor,
}) {
ThemeData themeData = switch (brightness) {
Brightness.light => ThemeData.light(),
Expand Down Expand Up @@ -68,6 +70,7 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
hrLineColor: hrLineColor,
linkColor: linkColor,
linkHoverColor: linkHoverColor,
codeBlockBackgroundColor: codeBlockBackgroundColor,
);
}

Expand All @@ -87,6 +90,7 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
hrLineColor: theme.colorScheme.outline,
linkColor: Colors.blue,
linkHoverColor: Colors.red,
codeBlockBackgroundColor: theme.colorScheme.onInverseSurface,
);
}

Expand Down Expand Up @@ -121,6 +125,9 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
/// The color of the link when hovering.
Color linkHoverColor;

/// The background color of the code block.
Color codeBlockBackgroundColor;

/// A method to copy the `GptMarkdownThemeData`.
@override
GptMarkdownThemeData copyWith({
Expand All @@ -135,6 +142,7 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
Color? hrLineColor,
Color? linkColor,
Color? linkHoverColor,
Color? codeBlockBackgroundColor,
}) {
return GptMarkdownThemeData._(
highlightColor: highlightColor ?? this.highlightColor,
Expand All @@ -148,6 +156,8 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
hrLineColor: hrLineColor ?? this.hrLineColor,
linkColor: linkColor ?? this.linkColor,
linkHoverColor: linkHoverColor ?? this.linkHoverColor,
codeBlockBackgroundColor:
codeBlockBackgroundColor ?? this.codeBlockBackgroundColor,
);
}

Expand All @@ -173,6 +183,13 @@ class GptMarkdownThemeData extends ThemeExtension<GptMarkdownThemeData> {
linkColor: Color.lerp(linkColor, other.linkColor, t) ?? linkColor,
linkHoverColor:
Color.lerp(linkHoverColor, other.linkHoverColor, t) ?? linkHoverColor,
codeBlockBackgroundColor:
Color.lerp(
codeBlockBackgroundColor,
other.codeBlockBackgroundColor,
t,
) ??
codeBlockBackgroundColor,
);
}
}
Expand Down
Loading