Skip to content

Commit f392917

Browse files
committed
Start moving from class-based styling to Style-based styling
1 parent 2f6d4b5 commit f392917

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

PRE_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ This changelog highlights changes as we work to implement version 1.0.0.
2121
* The default text style now matches the app's Material `TextTheme.body1` (Fixes [#18](https://github.com/Sub6Resources/flutter_html/issues/18)).
2222
* Fixed quite a few issues with `img`
2323
* Added a fancy new `style` attribute (this should be used in place of the deprecated styling parameters).
24+
* Added an even fancier new `css` attribute that takes a CSS string and applies those styles to your widgets.
2425

2526

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# flutter_html
22
[![pub package](https://img.shields.io/pub/v/flutter_html.svg)](https://pub.dev/packages/flutter_html)
3+
[![codecov](https://codecov.io/gh/Sub6Resources/flutter_html/branch/master/graph/badge.svg)](https://codecov.io/gh/Sub6Resources/flutter_html)
34
[![CircleCI](https://circleci.com/gh/Sub6Resources/flutter_html.svg?style=svg)](https://circleci.com/gh/Sub6Resources/flutter_html)
45

56
A Flutter widget for rendering html and css as Flutter widgets.

lib/html_parser.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HtmlParser extends StatelessWidget {
3636
cleanedTree,
3737
);
3838

39-
return Text.rich(parsedTree);
39+
return RichText(text: parsedTree);
4040
}
4141

4242
/// [parseHTML] converts a string to a DOM document using the dart `html` library.
@@ -91,8 +91,9 @@ class HtmlParser extends StatelessWidget {
9191

9292
static StyledElement applyCSS(StyledElement tree, css.StyleSheet sheet) {
9393
sheet.topLevels.forEach((treeNode) {
94-
if(treeNode is css.RuleSet) {
95-
print(treeNode.selectorGroup.selectors.first.simpleSelectorSequences.first.simpleSelector.name);
94+
if (treeNode is css.RuleSet) {
95+
print(treeNode.selectorGroup.selectors.first.simpleSelectorSequences
96+
.first.simpleSelector.name);
9697
}
9798
});
9899

lib/style.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:flutter_html/block_element.dart';
33

44
class Style {
5+
Display display;
56
TextStyle textStyle;
67
bool preserveWhitespace;
78
int baselineOffset;
@@ -11,6 +12,7 @@ class Style {
1112
Block block;
1213

1314
Style({
15+
this.display,
1416
this.textStyle,
1517
this.preserveWhitespace,
1618
this.baselineOffset,
@@ -32,6 +34,7 @@ class Style {
3234
Block mergedBlock = block?.merge(other.block);
3335

3436
return copyWith(
37+
display: other.display,
3538
textStyle: mergedTextStyle,
3639
preserveWhitespace: other.preserveWhitespace,
3740
baselineOffset: other.baselineOffset,
@@ -43,6 +46,7 @@ class Style {
4346
}
4447

4548
Style copyWith({
49+
Display display,
4650
TextStyle textStyle,
4751
bool preserveWhitespace,
4852
int baselineOffset,
@@ -52,6 +56,7 @@ class Style {
5256
Block block,
5357
}) {
5458
return Style(
59+
display: display ?? this.display,
5560
textStyle: textStyle ?? this.textStyle,
5661
preserveWhitespace: preserveWhitespace ?? this.preserveWhitespace,
5762
baselineOffset: baselineOffset ?? this.baselineOffset,
@@ -62,3 +67,9 @@ class Style {
6267
);
6368
}
6469
}
70+
71+
enum Display {
72+
BLOCK,
73+
INLINE,
74+
INLINE_BLOCK,
75+
}

test/html_parser_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_html/html_elements.dart';
33
import 'package:flutter_html/html_parser.dart';
4+
import 'package:flutter_html/style.dart';
45
import 'package:flutter_test/flutter_test.dart';
56
import 'package:flutter_html/flutter_html.dart';
67

@@ -90,4 +91,25 @@ void testNewParser() {
9091
expect(audioContentElement.src, hasLength(2), reason: "Not enough sources...");
9192
}
9293
});
94+
95+
test("Test style merging", () {
96+
Style style1 = Style(
97+
display: Display.BLOCK,
98+
textStyle: TextStyle(fontWeight: FontWeight.bold),
99+
);
100+
101+
Style style2 = Style(
102+
before: "* ",
103+
textDirection: TextDirection.rtl,
104+
textStyle: TextStyle(fontStyle: FontStyle.italic),
105+
);
106+
107+
Style finalStyle = style1.merge(style2);
108+
109+
expect(finalStyle.display, equals(Display.BLOCK));
110+
expect(finalStyle.before, equals("* "));
111+
expect(finalStyle.textDirection, equals(TextDirection.rtl));
112+
expect(finalStyle.textStyle.fontStyle, equals(FontStyle.italic));
113+
expect(finalStyle.textStyle.fontWeight, equals(FontWeight.bold));
114+
});
93115
}

0 commit comments

Comments
 (0)