md_widget.dart
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:gpt_markdown/markdown_component.dart';
/// It creates a markdown widget closed to each other.
class MdWidget extends StatelessWidget {
const MdWidget(
this.exp, {
super.key,
this.style,
this.textDirection = TextDirection.ltr,
this.onLinkTab,
this.textAlign,
this.textScaler,
this.latexWorkaround,
this.latexBuilder,
this.followLinkColor = false,
this.codeBuilder,
});
final String exp;
final TextDirection textDirection;
final TextStyle? style;
final TextAlign? textAlign;
final TextScaler? textScaler;
final void Function(String url, String title)? onLinkTab;
final String Function(String tex)? latexWorkaround;
final Widget Function(BuildContext context, String tex,TextStyle textStyle, bool inline)?
latexBuilder;
final bool followLinkColor;
final Widget Function(BuildContext context, String name, String code)?
codeBuilder;
@override
Widget build(BuildContext context) {
List<InlineSpan> list = [];
list.addAll(
MarkdownComponent.generate(
context,
exp.replaceAllMapped(
RegExp(
r"\\\[(.*?)\\\]|(\\begin.*?\\end{.*?})",
multiLine: true,
dotAll: true,
), (match) {
//
String body = (match[1] ?? match[2])?.replaceAll("\n", " ") ?? "";
return "\\[$body\\]";
}),
style,
textDirection,
onLinkTab,
latexWorkaround,
latexBuilder,
codeBuilder,
),
);
return Text.rich(
TextSpan(
children: list,
style: style?.copyWith(),
),
textDirection: textDirection,
textScaler: textScaler,
textAlign: textAlign,
);
}
}
class CustomTableColumnWidth extends TableColumnWidth {
@override
double maxIntrinsicWidth(Iterable<RenderBox> cells, double containerWidth) {
double width = 50;
for (var each in cells) {
each.layout(const BoxConstraints(), parentUsesSize: true);
width = max(width, each.size.width);
}
return min(containerWidth, width);
}
@override
double minIntrinsicWidth(Iterable<RenderBox> cells, double containerWidth) {
return 50;
}
}