Skip to content

Commit eb87940

Browse files
committed
增加readme的图片处理
1 parent f484778 commit eb87940

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

RECORD.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,35 @@ _debugUltimatePreviousSiblingOf
2424
GlobalKey可以记录state,但是需要跟随build创建
2525
https://stackoverflow.com/questions/49862572
2626

27+
```
28+
if you use AutomaticKeepAliveClientMixin to keep alive page item,you can't write like this:
29+
30+
PageView(
31+
controller: _pageController,
32+
onPageChanged: (index) {
33+
onPageChanged(index);
34+
},
35+
children: [NewsList(),Text('page1'),Text('page2'),Text('page3')],
36+
),
37+
38+
you should write like this:
39+
40+
List _list = [
41+
NewsList(),
42+
Text('page1'),
43+
Text('page2'),
44+
Text('page3')
45+
];
46+
PageView(
47+
controller: _pageController,
48+
onPageChanged: (index) {
49+
onPageChanged(index);
50+
},
51+
children:_list,
52+
),
53+
```
54+
55+
2756
### webView
2857

2958
https://github.com/flutter/flutter/issues/19030 没有webview,残念

lib/widget/GSYMarkdownWidget.dart

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,47 @@ class GSYMarkdownWidget extends StatelessWidget {
108108
return styleSheet;
109109
}
110110

111+
_getMarkDownData(String markdownData) {
112+
///优化图片显示
113+
RegExp exp = new RegExp(r'!\[.*\]\((.+)\)');
114+
Iterable<Match> tags = exp.allMatches(markdownData);
115+
String mdDataCode = markdownData;
116+
if (tags != null && tags.length > 0) {
117+
for (Match m in tags) {
118+
String imageMatch = m.group(0);
119+
if (imageMatch != null) {
120+
String match = m.group(0).replaceAll("\)", "?raw=true)");
121+
mdDataCode = mdDataCode.replaceAll(m.group(0), match);
122+
}
123+
}
124+
}
125+
126+
///优化img标签的src资源
127+
RegExp expImg = new RegExp("<img.*?(?:>|\/>)");
128+
RegExp expSrc = new RegExp("src=[\'\"]?([^\'\"]*)[\'\"]?");
129+
tags = expImg.allMatches(markdownData);
130+
if (tags != null && tags.length > 0) {
131+
for (Match m in tags) {
132+
String imageTag = m.group(0);
133+
String match = imageTag;
134+
if (imageTag != null) {
135+
Iterable<Match> srcTags = expSrc.allMatches(imageTag);
136+
for (Match srcMatch in srcTags) {
137+
String srcString = srcMatch.group(0);
138+
if (srcString != null && srcString.contains("http")) {
139+
String newSrc = srcString.substring(srcString.indexOf("http"), srcString.length - 1) + "?raw=true";
140+
match = "![]($newSrc)";
141+
}
142+
}
143+
}
144+
mdDataCode = mdDataCode.replaceAll(imageTag, match);
145+
}
146+
}
147+
148+
149+
return mdDataCode;
150+
}
151+
111152
@override
112153
Widget build(BuildContext context) {
113154
return Container(
@@ -117,7 +158,7 @@ class GSYMarkdownWidget extends StatelessWidget {
117158
child: new MarkdownBody(
118159
styleSheet: _getStyle(context),
119160
syntaxHighlighter: new GSYHighlighter(),
120-
data: markdownData,
161+
data: _getMarkDownData(markdownData),
121162
onTapLink: (String source) {
122163
CommonUtils.launchUrl(context, source);
123164
},

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dependencies:
1515
flutter_redux: ^0.5.2
1616
device_info: ^0.2.1
1717
connectivity: ^0.3.1
18-
flutter_markdown: ^0.1.5
18+
flutter_markdown: ^0.1.6
1919
url_launcher: ^3.0.3
2020
share: ^0.5.2
2121
flutter_spinkit: ^2.0.0

0 commit comments

Comments
 (0)