Skip to content

Commit c68c59d

Browse files
committed
Add additional CSS styles
1 parent 753db68 commit c68c59d

File tree

7 files changed

+176
-28
lines changed

7 files changed

+176
-28
lines changed

example/ios/Flutter/Flutter.podspec

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# NOTE: This podspec is NOT to be published. It is only used as a local source!
3+
#
4+
5+
Pod::Spec.new do |s|
6+
s.name = 'Flutter'
7+
s.version = '1.0.0'
8+
s.summary = 'High-performance, high-fidelity mobile apps.'
9+
s.description = <<-DESC
10+
Flutter provides an easy and productive way to build and deploy high-performance mobile apps for Android and iOS.
11+
DESC
12+
s.homepage = 'https://flutter.io'
13+
s.license = { :type => 'MIT' }
14+
s.author = { 'Flutter Dev Team' => 'flutter-dev@googlegroups.com' }
15+
s.source = { :git => 'https://github.com/flutter/engine', :tag => s.version.to_s }
16+
s.ios.deployment_target = '8.0'
17+
s.vendored_frameworks = 'Flutter.framework'
18+
end

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const htmlData = """
4646
</p>
4747
<h3>Support for <code>sub</code>/<code>sup</code></h3>
4848
Solve for <var>x<sub>n</sub></var>: log<sub>2</sub>(<var>x</var><sup>2</sup>+<var>n</var>) = 9<sup>3</sup>
49-
<p>One of the most common equations in all of physics is <var>E</var>=<var>m</var><var>c</var><sup>2</sup>.</p>
49+
<p>One of the most <span>common</span> equations in all of physics is <var>E</var>=<var>m</var><var>c</var><sup>2</sup>.</p>
5050
<h3>Table support:</h3>
5151
<table>
5252
<colgroup>

lib/html_parser.dart

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import 'dart:collection';
22
import 'dart:math';
33

4+
import 'package:csslib/parser.dart' as cssparser;
5+
import 'package:csslib/visitor.dart' as css;
6+
import 'package:flutter/material.dart';
47
import 'package:flutter_html/flutter_html.dart';
8+
import 'package:flutter_html/src/html_elements.dart';
59
import 'package:flutter_html/src/layout_element.dart';
610
import 'package:flutter_html/src/utils.dart';
711
import 'package:flutter_html/style.dart';
8-
import 'package:flutter/material.dart';
9-
import 'package:csslib/visitor.dart' as css;
1012
import 'package:html/dom.dart' as dom;
11-
import 'package:flutter_html/src/html_elements.dart';
1213
import 'package:html/parser.dart' as htmlparser;
13-
import 'package:csslib/parser.dart' as cssparser;
1414

1515
typedef OnTap = void Function(String url);
1616
typedef CustomRender = Widget Function(
@@ -67,7 +67,7 @@ class HtmlParser extends StatelessWidget {
6767
cleanedTree,
6868
);
6969

70-
return RichText(text: parsedTree);
70+
return StyledText(textSpan: parsedTree, style: cleanedTree.style);
7171
}
7272

7373
/// [parseHTML] converts a string of HTML to a DOM document using the dart `html` library.
@@ -281,14 +281,15 @@ class HtmlParser extends StatelessWidget {
281281
Padding(
282282
padding: EdgeInsets.only(
283283
left: 30), //TODO derive this from list padding.
284-
child: RichText(
285-
text: TextSpan(
284+
child: StyledText(
285+
textSpan: TextSpan(
286286
children: tree.children
287287
?.map((tree) => parseTree(newContext, tree))
288288
?.toList() ??
289289
[],
290290
style: newContext.style.generateTextStyle(),
291291
),
292+
style: newContext.style,
292293
),
293294
)
294295
],
@@ -317,14 +318,15 @@ class HtmlParser extends StatelessWidget {
317318
},
318319
),
319320
},
320-
child: RichText(
321-
text: TextSpan(
321+
child: StyledText(
322+
textSpan: TextSpan(
322323
style: newContext.style.generateTextStyle(),
323324
children: tree.children
324325
.map((tree) => parseTree(newContext, tree))
325326
.toList() ??
326327
[],
327328
),
329+
style: newContext.style,
328330
),
329331
),
330332
);
@@ -349,14 +351,15 @@ class HtmlParser extends StatelessWidget {
349351
return WidgetSpan(
350352
child: Transform.translate(
351353
offset: Offset(0, verticalOffset),
352-
child: RichText(
353-
text: TextSpan(
354+
child: StyledText(
355+
textSpan: TextSpan(
354356
style: newContext.style.generateTextStyle(),
355357
children: tree.children
356358
.map((tree) => parseTree(newContext, tree))
357359
.toList() ??
358360
[],
359361
),
362+
style: newContext.style,
360363
),
361364
),
362365
);
@@ -690,12 +693,36 @@ class ContainerSpan extends StatelessWidget {
690693
margin: style?.margin,
691694
alignment: shrinkWrap ? null : style?.alignment,
692695
child: child ??
693-
RichText(
694-
text: TextSpan(
696+
StyledText(
697+
textSpan: TextSpan(
695698
style: newContext.style.generateTextStyle(),
696699
children: children,
697700
),
701+
style: newContext.style,
698702
),
699703
);
700704
}
701705
}
706+
707+
class StyledText extends StatelessWidget {
708+
final InlineSpan textSpan;
709+
final Style style;
710+
711+
const StyledText({
712+
this.textSpan,
713+
this.style,
714+
});
715+
716+
@override
717+
Widget build(BuildContext context) {
718+
return SizedBox(
719+
width: style.display == Display.BLOCK || style.display == Display.LIST_ITEM? double.infinity: null,
720+
child: Text.rich(
721+
textSpan,
722+
style: style.generateTextStyle(),
723+
textAlign: style.textAlign,
724+
textDirection: style.direction,
725+
),
726+
);
727+
}
728+
}

lib/src/layout_element.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ class TableRowLayoutElement extends LayoutElement {
125125
color: c.style.backgroundColor,
126126
border: c.style.border,
127127
),
128-
child: RichText(
129-
text: context.parser.parseTree(context, c),
128+
child: StyledText(
129+
textSpan: context.parser.parseTree(context, c),
130+
style: c.style,
130131
)));
131132
}
132133
return null;

lib/src/styled_element.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_html/style.dart';
33
import 'package:html/dom.dart' as dom;
4-
//TODO(Sub6Resources) don't use the internal code of the html package as it may change unexpectedly.
4+
//TODO(Sub6Resources): don't use the internal code of the html package as it may change unexpectedly.
55
import 'package:html/src/query_selector.dart';
66

77
/// A [StyledElement] applies a style to all of its children.
@@ -85,7 +85,7 @@ StyledElement parseStyledElement(
8585
? TextDirection.rtl
8686
: TextDirection.ltr;
8787
styledElement.style = Style(
88-
textDirection: textDirection,
88+
direction: textDirection,
8989
);
9090
break;
9191
case "big":

0 commit comments

Comments
 (0)