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
40 changes: 35 additions & 5 deletions lib/custom_widgets/code_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ import 'package:flutter/services.dart';
/// - Visual feedback when code is copied
/// - Themed colors that adapt to light/dark mode
class CodeField extends StatefulWidget {
const CodeField({super.key, required this.name, required this.codes});
const CodeField({
super.key,
required this.name,
required this.codes,
this.backgroundColor,
this.textColor,
});

final String name;
final String codes;

final Color? backgroundColor;
final Color? textColor;

@override
State<CodeField> createState() => _CodeFieldState();
}
Expand All @@ -27,7 +37,9 @@ class _CodeFieldState extends State<CodeField> {
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.onInverseSurface,
color:
widget.backgroundColor ??
Theme.of(context).colorScheme.onInverseSurface,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand All @@ -39,12 +51,21 @@ class _CodeFieldState extends State<CodeField> {
horizontal: 16.0,
vertical: 8,
),
child: Text(widget.name),
child: Text(
widget.name,
style: TextStyle(
color:
widget.textColor ??
Theme.of(context).colorScheme.onSurface,
),
),
),
const Spacer(),
TextButton.icon(
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.onSurface,
foregroundColor:
widget.textColor ??
Theme.of(context).colorScheme.onSurface,
textStyle: const TextStyle(fontWeight: FontWeight.normal),
),
onPressed: () async {
Expand All @@ -64,7 +85,14 @@ class _CodeFieldState extends State<CodeField> {
(_copied) ? Icons.done : Icons.content_paste,
size: 15,
),
label: Text((_copied) ? "Copied!" : "Copy code"),
label: Text(
(_copied) ? "Copied!" : "Copy code",
style: TextStyle(
color:
widget.textColor ??
Theme.of(context).colorScheme.onSurface,
),
),
),
],
),
Expand All @@ -75,6 +103,8 @@ class _CodeFieldState extends State<CodeField> {
child: Text(
widget.codes,
style: TextStyle(
color:
widget.textColor ?? Theme.of(context).colorScheme.onSurface,
fontFamily: 'JetBrainsMono',
package: "gpt_markdown",
),
Expand Down
7 changes: 7 additions & 0 deletions lib/custom_widgets/markdown_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class GptMarkdownConfig {
this.components,
this.inlineComponents,
this.tableBuilder,
this.addNewLineAfterH1 = true,
});

/// The direction of the text.
Expand Down Expand Up @@ -155,6 +156,9 @@ class GptMarkdownConfig {
/// The table builder.
final TableBuilder? tableBuilder;

/// Whether to add a new line after the h1 tag.
final bool addNewLineAfterH1;

/// A copy of the configuration with the specified parameters.
GptMarkdownConfig copyWith({
TextStyle? style,
Expand All @@ -177,6 +181,7 @@ class GptMarkdownConfig {
final List<MarkdownComponent>? components,
final List<MarkdownComponent>? inlineComponents,
final TableBuilder? tableBuilder,
final bool? addNewLineAfterH1,
}) {
return GptMarkdownConfig(
style: style ?? this.style,
Expand All @@ -199,6 +204,7 @@ class GptMarkdownConfig {
components: components ?? this.components,
inlineComponents: inlineComponents ?? this.inlineComponents,
tableBuilder: tableBuilder ?? this.tableBuilder,
addNewLineAfterH1: addNewLineAfterH1 ?? this.addNewLineAfterH1,
);
}

Expand All @@ -222,6 +228,7 @@ class GptMarkdownConfig {
maxLines == other.maxLines &&
overflow == other.overflow &&
followLinkColor == other.followLinkColor &&
addNewLineAfterH1 == other.addNewLineAfterH1 &&
// latexWorkaround == other.latexWorkaround &&
// components == other.components &&
// inlineComponents == other.inlineComponents &&
Expand Down
5 changes: 5 additions & 0 deletions lib/gpt_markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class GptMarkdown extends StatelessWidget {
this.components,
this.inlineComponents,
this.useDollarSignsForLatex = false,
this.addNewLineAfterH1 = true,
});

/// The direction of the text.
Expand Down Expand Up @@ -104,6 +105,9 @@ class GptMarkdown extends StatelessWidget {
/// The table builder.
final TableBuilder? tableBuilder;

/// Whether to add a new line after the h1 tag.
final bool addNewLineAfterH1;

/// The list of components.
/// ```dart
/// List<MarkdownComponent> components = [
Expand Down Expand Up @@ -207,6 +211,7 @@ class GptMarkdown extends StatelessWidget {
components: components,
inlineComponents: inlineComponents,
tableBuilder: tableBuilder,
addNewLineAfterH1: addNewLineAfterH1,
),
),
);
Expand Down
9 changes: 5 additions & 4 deletions lib/markdown_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,11 @@ class HTag extends BlockMd {
false,
)),
if (match.namedGroup('hash')!.length == 1) ...[
const TextSpan(
text: "\n ",
style: TextStyle(fontSize: 0, height: 0),
),
if (config.addNewLineAfterH1)
const TextSpan(
text: "\n ",
style: TextStyle(fontSize: 0, height: 0),
),
WidgetSpan(
child: CustomDivider(
height: theme.hrLineThickness,
Expand Down