md_widget.dart
4.9 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:tex_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.followLinkColor = false});
final String exp;
final TextDirection textDirection;
final TextStyle? style;
final void Function(String url, String title)? onLinkTab;
final bool followLinkColor;
/// To convert markdown text to html text.
static String toHtml(String text) {
final RegExp table = RegExp(
r"^(((\|[^\n\|]+\|)((([^\n\|]+\|)+)?))(\n(((\|[^\n\|]+\|)(([^\n\|]+\|)+)?)))+)?$",
);
if (table.hasMatch(text)) {
final String value = text.trim().splitMapJoin(
RegExp(r'^\||\|\n\||\|$'),
onMatch: (p0) => "\n",
onNonMatch: (p0) {
if (p0.trim().isEmpty) {
return "";
}
// return p0;
return '<tr>${p0.trim().splitMapJoin(
'|',
onMatch: (p0) {
return "";
},
onNonMatch: (p0) {
return '<td>$p0</td>';
},
)}</tr>';
},
);
return '''
<table border="1" cellspacing="0">
$value
</table>
''';
}
return MarkdownComponent.toHtml(text);
}
@override
Widget build(BuildContext context) {
List<InlineSpan> list = [];
exp.trim().splitMapJoin(
RegExp(r"\n\n+"),
onMatch: (p0) {
list.add(
TextSpan(
text: "\n\n",
style: TextStyle(
fontSize: 6,
color: style?.color,
),
),
);
return "";
},
onNonMatch: (eachLn) {
final RegExp table = RegExp(
r"^(((\|[^\n\|]+\|)((([^\n\|]+\|)+)?))(\n(((\|[^\n\|]+\|)(([^\n\|]+\|)+)?)))+)$",
);
if (table.hasMatch(eachLn.trim())) {
final List<Map<int, String>> value = eachLn
.trim()
.split('\n')
.map<Map<int, String>>(
(e) => e
.split('|')
.where((element) => element.isNotEmpty)
.toList()
.asMap(),
)
.toList();
int maxCol = 0;
for (final each in value) {
if (maxCol < each.keys.length) {
maxCol = each.keys.length;
}
}
list.addAll(
[
TextSpan(
text: "\n ",
style: TextStyle(height: 0, fontSize: 0, color: style?.color),
),
WidgetSpan(
child: Table(
textDirection: textDirection,
defaultColumnWidth: CustomTableColumnWidth(),
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
border: TableBorder.all(
width: 1,
color: Theme.of(context).colorScheme.onSurface,
),
children: value
.map<TableRow>(
(e) => TableRow(
children: List.generate(
maxCol,
(index) => Center(
child: MdWidget(
(e[index] ?? "").trim(),
textDirection: textDirection,
onLinkTab: onLinkTab,
style: style,
),
),
),
),
)
.toList(),
),
),
TextSpan(
text: "\n ",
style: TextStyle(height: 0, fontSize: 0, color: style?.color),
),
],
);
} else {
list.addAll(
MarkdownComponent.generate(
context,
eachLn.trim(),
style,
textDirection,
onLinkTab,
),
);
}
return "";
},
);
return Text.rich(
TextSpan(
children: list,
style: style,
),
textDirection: textDirection,
);
}
}
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;
}
}