Repository navigation
Expand file tree
/
Copy pathGSYMarkdownWidget.dart
More file actions
196 lines (176 loc) · 6.24 KB
/
Copy pathGSYMarkdownWidget.dart
File metadata and controls
196 lines (176 loc) · 6.24 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
import 'package:gsy_github_app_flutter/widget/SyntaxHighLighter.dart';
/**
* 代码详情
* Created by guoshuyu
* Date: 2018-07-24
*/
class GSYMarkdownWidget extends StatelessWidget {
static const int DARK_WHITE = 0;
static const int DARK_LIGHT = 1;
static const int DARK_THEME = 2;
final String markdownData;
final int style;
GSYMarkdownWidget({this.markdownData = "", this.style = DARK_WHITE});
_getCommonSheet(BuildContext context, Color codeBackground) {
MarkdownStyleSheet markdownStyleSheet =
MarkdownStyleSheet.fromTheme(Theme.of(context));
return markdownStyleSheet
.copyWith(
codeblockDecoration: new BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4.0)),
color: codeBackground,
border: new Border.all(
color: Color(GSYColors.subTextColor), width: 0.3)))
.copyWith(
blockquoteDecoration: new BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4.0)),
color: Color(GSYColors.subTextColor),
border: new Border.all(
color: Color(GSYColors.subTextColor), width: 0.3)),
blockquote: GSYConstant.smallTextWhite);
}
_getStyleSheetDark(BuildContext context) {
return _getCommonSheet(context, Color.fromRGBO(40, 44, 52, 1.00)).copyWith(
p: GSYConstant.smallTextWhite,
h1: GSYConstant.largeLargeTextWhite,
h2: GSYConstant.largeTextWhiteBold,
h3: GSYConstant.normalTextMitWhiteBold,
h4: GSYConstant.middleTextWhite,
h5: GSYConstant.smallTextWhite,
h6: GSYConstant.smallTextWhite,
em: const TextStyle(fontStyle: FontStyle.italic),
strong: GSYConstant.middleTextWhiteBold,
code: GSYConstant.smallSubText,
);
}
_getStyleSheetWhite(BuildContext context) {
return _getCommonSheet(context, Color.fromRGBO(40, 44, 52, 1.00)).copyWith(
p: GSYConstant.smallText,
h1: GSYConstant.largeLargeText,
h2: GSYConstant.largeTextBold,
h3: GSYConstant.normalTextBold,
h4: GSYConstant.middleText,
h5: GSYConstant.smallText,
h6: GSYConstant.smallText,
strong: GSYConstant.middleTextBold,
code: GSYConstant.smallSubText,
);
}
_getStyleSheetTheme(BuildContext context) {
return _getCommonSheet(context, Color.fromRGBO(40, 44, 52, 1.00)).copyWith(
p: GSYConstant.smallTextWhite,
h1: GSYConstant.largeLargeTextWhite,
h2: GSYConstant.largeTextWhiteBold,
h3: GSYConstant.normalTextMitWhiteBold,
h4: GSYConstant.middleTextWhite,
h5: GSYConstant.smallTextWhite,
h6: GSYConstant.smallTextWhite,
em: const TextStyle(fontStyle: FontStyle.italic),
strong: GSYConstant.middleTextWhiteBold,
code: GSYConstant.smallSubText,
);
}
_getBackgroundColor(context) {
Color background = Color(GSYColors.white);
switch (style) {
case DARK_LIGHT:
background = Color(GSYColors.primaryLightValue);
break;
case DARK_THEME:
background = Theme.of(context).primaryColor;
break;
}
return background;
}
_getStyle(BuildContext context) {
var styleSheet = _getStyleSheetWhite(context);
switch (style) {
case DARK_LIGHT:
styleSheet = _getStyleSheetDark(context);
break;
case DARK_THEME:
styleSheet = _getStyleSheetTheme(context);
break;
}
return styleSheet;
}
_getMarkDownData(String markdownData) {
///优化图片显示
RegExp exp = new RegExp(r'!\[.*\]\((.+)\)');
RegExp expImg = new RegExp("<img.*?(?:>|\/>)");
RegExp expSrc = new RegExp("src=[\'\"]?([^\'\"]*)[\'\"]?");
Iterable<Match> tags = exp.allMatches(markdownData);
String mdDataCode = markdownData;
if (tags != null && tags.length > 0) {
for (Match m in tags) {
String imageMatch = m.group(0);
if (imageMatch != null && !imageMatch.contains(".svg")) {
String match = imageMatch.replaceAll("\)", "?raw=true)");
if (!match.contains(".svg") && match.contains("http")) {
///增加点击
String src = match
.replaceAll(new RegExp(r'!\[.*\]\('), "")
.replaceAll(")", "");
String actionMatch = "[$match]($src)";
match = actionMatch;
} else {
match = "";
}
mdDataCode = mdDataCode.replaceAll(m.group(0), match);
}
}
}
///优化img标签的src资源
tags = expImg.allMatches(markdownData);
if (tags != null && tags.length > 0) {
for (Match m in tags) {
String imageTag = m.group(0);
String match = imageTag;
if (imageTag != null) {
Iterable<Match> srcTags = expSrc.allMatches(imageTag);
for (Match srcMatch in srcTags) {
String srcString = srcMatch.group(0);
if (srcString != null && srcString.contains("http")) {
String newSrc = srcString.substring(
srcString.indexOf("http"), srcString.length - 1) +
"?raw=true";
///增加点击
match = "[]($newSrc)";
}
}
}
mdDataCode = mdDataCode.replaceAll(imageTag, match);
}
}
return mdDataCode;
}
@override
Widget build(BuildContext context) {
return Container(
color: _getBackgroundColor(context),
padding: EdgeInsets.all(5.0),
child: SingleChildScrollView(
child: new MarkdownBody(
styleSheet: _getStyle(context),
syntaxHighlighter: new GSYHighlighter(),
data: _getMarkDownData(markdownData),
onTapLink: (String source) {
CommonUtils.launchUrl(context, source);
},
),
),
);
}
}
class GSYHighlighter extends SyntaxHighlighter {
@override
TextSpan format(String source) {
String showSource = source.replaceAll("<", "<");
showSource = showSource.replaceAll(">", ">");
return new DartSyntaxHighlighter().format(showSource);
}
}