md_widget.dart
2.02 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
part of 'gpt_markdown.dart';
/// It creates a markdown widget closed to each other.
class MdWidget extends StatefulWidget {
const MdWidget(
this.context,
this.exp,
this.includeGlobalComponents, {
super.key,
required this.config,
});
/// The expression to be displayed.
final String exp;
final BuildContext context;
/// Whether to include global components.
final bool includeGlobalComponents;
/// The configuration of the markdown widget.
final GptMarkdownConfig config;
@override
State<MdWidget> createState() => _MdWidgetState();
}
class _MdWidgetState extends State<MdWidget> {
List<InlineSpan> list = [];
@override
void initState() {
super.initState();
list = MarkdownComponent.generate(
widget.context,
widget.exp,
widget.config,
widget.includeGlobalComponents,
);
}
@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,
);
}
}
@override
Widget build(BuildContext context) {
// List<InlineSpan> list = MarkdownComponent.generate(
// context,
// widget.exp,
// widget.config,
// widget.includeGlobalComponents,
// );
return widget.config.getRich(
TextSpan(children: list, style: widget.config.style?.copyWith()),
);
}
}
/// A custom table column width.
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;
}
}