Skip to content

Commit 31d9e0e

Browse files
committed
Version 0.1.0
1 parent bb6ac26 commit 31d9e0e

File tree

7 files changed

+270
-37
lines changed

7 files changed

+270
-37
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
## [0.0.1] - August 14, 2018.
1+
## [0.1.0] - August 14, 2018:
22

3-
* Add support for `body`, `div`, `b`, `i`, and `u`.
3+
* Renamed widget from `HtmlWidget` to `Html`
4+
* Adds support for `p`, `h1`, `h2`, `h3`, `h4`, `h5`, and `h6`.
5+
6+
## [0.0.1] - August 14, 2018:
7+
8+
* Adds support for `body`, `div`, `b`, `i`, and `u`.

README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
11
# flutter_html_widget
22

3-
A Flutter widget for rendering basic html tags as Flutter widgets.
3+
A Flutter widget for rendering static html tags as Flutter widgets.
44

55
## Installing:
66

77
Add the following to your `pubspec.yaml` file:
88

99
dependencies:
10-
flutter_html_widget: ^0.0.1
10+
flutter_html_widget: ^0.1.0
1111

1212
## Usage:
1313

14-
HtmlWidget(
15-
data: yourHTMLString,
16-
// Optional parameters
17-
// padding: EdgeInsetsGeometry.all(8.0),
18-
// backgroundColor: Colors.blue,
14+
Html(
15+
data: """
16+
<div>
17+
<h1>Demo Page</h1>
18+
<p>This is a fantastic nonexistent product that you should buy!</p>
19+
<h2>Pricing</h2>
20+
<p>Lorem ipsum <b>dolor</b> sit amet.</p>
21+
<h2>The Team</h2>
22+
<p>There isn't <i>really</i> a team...</p>
23+
<h2>Installation</h2>
24+
<p>You <u>cannot</u> install a nonexistent product!</p>
25+
</div>
26+
""",
27+
//Optional parameters:
28+
padding: EdgeInsets.all(8.0),
29+
backgroundColor: Colors.white70,
1930
)
2031

21-
## Supported HTML Tags:
32+
## Currently Supported HTML Tags:
2233

23-
* `b`
24-
* `body`
25-
* `div`
26-
* `i`
27-
* `u`
34+
* `b`
35+
* `body`
36+
* `div`
37+
* `i`
38+
* `p`
39+
* `u`
40+
* `h1` - `h6`
2841

2942
Here are a list of elements that this package will never support:
3043

31-
* `script`
44+
* `script`
3245

3346
## Why this package?
3447

example/main.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_html/flutter_html.dart';
3+
4+
void main() {
5+
runApp(
6+
MaterialApp(
7+
home: Scaffold(
8+
body: Html(
9+
data: """
10+
<div>
11+
<h1>Demo Page</h1>
12+
<p>This is a fantastic nonexistent product that you should buy!</p>
13+
<h2>Pricing</h2>
14+
<p>Lorem ipsum <b>dolor</b> sit amet.</p>
15+
<h2>The Team</h2>
16+
<p>There isn't <i>really</i> a team...</p>
17+
<h2>Installation</h2>
18+
<p>You <u>cannot</u> install a nonexistent product!</p>
19+
</div>
20+
""",
21+
//Optional parameters:
22+
padding: EdgeInsets.all(8.0),
23+
backgroundColor: Colors.white70,
24+
),
25+
),
26+
),
27+
);
28+
}

lib/flutter_html.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ library flutter_html;
33
import 'package:flutter/material.dart';
44
import 'package:flutter_html/html_parser.dart';
55

6-
7-
class HtmlWidget extends StatelessWidget {
8-
HtmlWidget({Key key, @required this.data, this.padding, this.backgroundColor}): super(key: key);
6+
class Html extends StatelessWidget {
7+
Html({Key key, @required this.data, this.padding, this.backgroundColor})
8+
: super(key: key);
99

1010
final String data;
1111
final EdgeInsetsGeometry padding;
@@ -22,5 +22,4 @@ class HtmlWidget extends StatelessWidget {
2222
),
2323
);
2424
}
25-
2625
}

lib/html_parser.dart

Lines changed: 152 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ import 'package:html/parser.dart' as parser;
33
import 'package:html/dom.dart' as dom;
44

55
class HtmlParser {
6-
static const _supportedElements = ["b", "body", "div", "i", "u"];
6+
static const _supportedElements = [
7+
"b",
8+
"body",
9+
"div",
10+
"h1",
11+
"h2",
12+
"h3",
13+
"h4",
14+
"h5",
15+
"h6",
16+
"i",
17+
"p",
18+
"u"
19+
];
720

821
///Parses an html string and returns a list of widgets that represent the body of your html document.
922
static List<Widget> parse(String data) {
@@ -18,7 +31,7 @@ class HtmlParser {
1831
if (node is dom.Element) {
1932
print("Found ${node.localName}");
2033
if (!_supportedElements.contains(node.localName)) {
21-
return Placeholder();
34+
return Container();
2235
}
2336
switch (node.localName) {
2437
case "b":
@@ -30,17 +43,78 @@ class HtmlParser {
3043
case "body":
3144
return Column(
3245
children: _parseNodeList(node.nodes),
46+
crossAxisAlignment: CrossAxisAlignment.start,
3347
);
3448
case "div":
3549
return Column(
3650
children: _parseNodeList(node.nodes),
51+
crossAxisAlignment: CrossAxisAlignment.start,
3752
);
53+
case "h1":
54+
return RichText(
55+
text: TextSpan(
56+
children: _parseInlineElement(node),
57+
style: TextStyle(
58+
fontSize: 28.0,
59+
fontWeight: FontWeight.bold,
60+
),
61+
));
62+
case "h2":
63+
return RichText(
64+
text: TextSpan(
65+
children: _parseInlineElement(node),
66+
style: TextStyle(
67+
fontSize: 21.0,
68+
fontWeight: FontWeight.bold,
69+
),
70+
));
71+
case "h3":
72+
return RichText(
73+
text: TextSpan(
74+
children: _parseInlineElement(node),
75+
style: TextStyle(
76+
fontSize: 16.0,
77+
fontWeight: FontWeight.bold,
78+
),
79+
));
80+
case "h4":
81+
return RichText(
82+
text: TextSpan(
83+
children: _parseInlineElement(node),
84+
style: TextStyle(
85+
fontSize: 14.0,
86+
fontWeight: FontWeight.bold,
87+
),
88+
));
89+
case "h5":
90+
return RichText(
91+
text: TextSpan(
92+
children: _parseInlineElement(node),
93+
style: TextStyle(
94+
fontSize: 12.0,
95+
fontWeight: FontWeight.bold,
96+
),
97+
));
98+
case "h6":
99+
return RichText(
100+
text: TextSpan(
101+
children: _parseInlineElement(node),
102+
style: TextStyle(
103+
fontSize: 10.0,
104+
fontWeight: FontWeight.bold,
105+
),
106+
));
38107
case "i":
39108
return RichText(
40109
text: TextSpan(
41110
children: _parseInlineElement(node),
42111
style: HtmlTextStyles.italics,
43112
));
113+
case "p":
114+
return RichText(
115+
text: TextSpan(
116+
children: _parseInlineElement(node),
117+
));
44118
case "u":
45119
return RichText(
46120
text: TextSpan(
@@ -49,10 +123,13 @@ class HtmlParser {
49123
));
50124
}
51125
} else if (node is dom.Text) {
126+
if(node.text.trim() == "") {
127+
return Container();
128+
}
52129
print("Plain Text Node: '${node.text}'");
53130
return Text(node.text);
54131
}
55-
return Placeholder();
132+
return Container();
56133
}
57134

58135
static List<Widget> _parseNodeList(List<dom.Node> nodeList) {
@@ -61,7 +138,18 @@ class HtmlParser {
61138
}).toList();
62139
}
63140

64-
static const _supportedInlineElements = ["b", "i", "u"];
141+
static const _supportedInlineElements = [
142+
"b",
143+
"h1",
144+
"h2",
145+
"h3",
146+
"h4",
147+
"h5",
148+
"h6",
149+
"i",
150+
"p",
151+
"u"
152+
];
65153

66154
static List<TextSpan> _parseInlineElement(dom.Element element) {
67155
List<TextSpan> textSpanList = new List<TextSpan>();
@@ -74,16 +162,70 @@ class HtmlParser {
74162
} else {
75163
switch (node.localName) {
76164
case "b":
77-
textSpanList.add(
78-
TextSpan(style: HtmlTextStyles.bold, children: _parseInlineElement(node)));
165+
textSpanList.add(TextSpan(
166+
style: HtmlTextStyles.bold,
167+
children: _parseInlineElement(node)));
168+
break;
169+
case "h1":
170+
textSpanList.add(TextSpan(
171+
style: TextStyle(
172+
fontSize: 28.0,
173+
fontWeight: FontWeight.bold,
174+
),
175+
));
176+
break;
177+
case "h2":
178+
textSpanList.add(TextSpan(
179+
style: TextStyle(
180+
fontSize: 21.0,
181+
fontWeight: FontWeight.bold,
182+
),
183+
));
184+
break;
185+
case "h3":
186+
textSpanList.add(TextSpan(
187+
style: TextStyle(
188+
fontSize: 16.0,
189+
fontWeight: FontWeight.bold,
190+
),
191+
));
192+
break;
193+
case "h4":
194+
textSpanList.add(TextSpan(
195+
style: TextStyle(
196+
fontSize: 14.0,
197+
fontWeight: FontWeight.bold,
198+
),
199+
));
200+
break;
201+
case "h5":
202+
textSpanList.add(TextSpan(
203+
style: TextStyle(
204+
fontSize: 12.0,
205+
fontWeight: FontWeight.bold,
206+
),
207+
));
208+
break;
209+
case "h6":
210+
textSpanList.add(TextSpan(
211+
style: TextStyle(
212+
fontSize: 10.0,
213+
fontWeight: FontWeight.bold,
214+
),
215+
));
79216
break;
80217
case "i":
81-
textSpanList.add(
82-
TextSpan(style: HtmlTextStyles.italics, children: _parseInlineElement(node)));
218+
textSpanList.add(TextSpan(
219+
style: HtmlTextStyles.italics,
220+
children: _parseInlineElement(node)));
221+
break;
222+
case "p":
223+
textSpanList.add(TextSpan(children: _parseInlineElement(node)));
83224
break;
84225
case "u":
85-
textSpanList.add(
86-
TextSpan(style: HtmlTextStyles.underline, children: _parseInlineElement(node)));
226+
textSpanList.add(TextSpan(
227+
style: HtmlTextStyles.underline,
228+
children: _parseInlineElement(node)));
87229
break;
88230
}
89231
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: flutter_html
22
description: A Flutter widget for rendering static html tags as Flutter widgets.
3-
version: 0.0.1
3+
version: 0.1.0
44
author: Matthew Whitaker <sub6resources@gmail.com>
5-
homepage: https://github.com/Sub6Resources/flutter_html_widget
5+
homepage: https://github.com/Sub6Resources/flutter_html
66

77
environment:
88
sdk: '>=1.19.0 <3.0.0'

0 commit comments

Comments
 (0)