8000 Improve margins. Add HtmlPadding to handle more complicated paddings.… · Sub6Resources/flutter_html@18db545 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18db545

Browse files
committed
Improve margins. Add HtmlPadding to handle more complicated paddings. Update CssBoxWidget to handle rtl marker boxes
1 parent 15cb05e commit 18db545

File tree

9 files changed

+635
-162
lines changed

9 files changed

+635
-162
lines changed

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ class MyHomePageState extends State<MyHomePage> {
296296
backgroundColor: const Color.fromARGB(0x50, 0xee, 0xee, 0xee),
297297
),
298298
"th": Style(
299-
padding: const EdgeInsets.all(6),
299+
padding: HtmlPaddings.all(6),
300300
backgroundColor: Colors.grey,
301301
),
302302
"td": Style(
303-
padding: const EdgeInsets.all(6),
303+
padding: HtmlPaddings.all(6),
304304
border: const Border(bottom: BorderSide(color: Colors.grey)),
305305
),
306306
'h5': Style(maxLines: 2, textOverflow: TextOverflow.ellipsis),

lib/src/builtins/styled_element_builtin.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,22 @@ class StyledElementBuiltIn extends HtmlExtension {
338338
styledElement.style = Style(
339339
display: Display.block,
340340
listStyleType: ListStyleType.decimal,
341-
padding: const EdgeInsets.only(left: 40),
341+
padding: HtmlPaddings.only(inlineStart: 40),
342+
margin: Margins(
343+
blockStart: Margin(1, Unit.em),
344+
blockEnd: Margin(1, Unit.em),
345+
),
342346
);
343347
break;
344348
case "ul":
345349
styledElement.style = Style(
346350
display: Display.block,
347351
listStyleType: ListStyleType.disc,
348-
padding: const EdgeInsets.only(left: 40),
352+
padding: HtmlPaddings.only(inlineStart: 40),
353+
margin: Margins(
354+
blockStart: Margin(1, Unit.em),
355+
blockEnd: Margin(1, Unit.em),
356+
),
349357
);
350358
break;
351359
case "p":

lib/src/css_box_widget.dart

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ class CssBoxWidget extends StatelessWidget {
5656
? _generateMarkerBoxSpan(style)
5757
: null;
5858

59+
final directionality = _checkTextDirection(context, textDirection);
60+
final padding = style.padding?.toEdgeInsets(directionality);
61+
5962
return _CSSBoxRenderer(
6063
width: style.width ?? Width.auto(),
6164
height: style.height ?? Height.auto(),
62-
paddingSize: style.padding?.collapsedSize ?? Size.zero,
65+
paddingSize: padding?.collapsedSize ?? Size.zero,
6366
borderSize: style.border?.dimensions.collapsedSize ?? Size.zero,
6467
margins: style.margin ?? Margins.zero,
6568
display: style.display ?? Display.inline,
6669
childIsReplaced: childIsReplaced,
6770
emValue: _calculateEmValue(style, context),
68-
textDirection: _checkTextDirection(context, textDirection),
71+
textDirection: directionality,
6972
shrinkWrap: shrinkWrap,
7073
children: [
7174
Container(
@@ -74,7 +77,7 @@ class CssBoxWidget extends StatelessWidget {
7477
color: style.backgroundColor, //Colors the padding and content boxes
7578
),
7679
width: _shouldExpandToFillBlock() ? double.infinity : null,
77-
padding: style.padding ?? EdgeInsets.zero,
80+
padding: padding,
7881
child: top
7982
? child
8083
: MediaQuery(
@@ -253,10 +256,21 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
253256
}
254257

255258
Margins _preProcessMargins(Margins margins, bool shrinkWrap) {
256-
Margin leftMargin = margins.left ?? Margin.zero();
257-
Margin rightMargin = margins.right ?? Margin.zero();
258-
Margin topMargin = margins.top ?? Margin.zero();
259-
Margin bottomMargin = margins.bottom ?? Margin.zero();
259+
late Margin leftMargin;
260+
late Margin rightMargin;
261+
Margin topMargin = margins.top ?? margins.blockStart ?? Margin.zero();
262+
Margin bottomMargin = margins.bottom ?? margins.blockEnd ?? Margin.zero();
263+
264+
switch (textDirection) {
265+
case TextDirection.rtl:
266+
leftMargin = margins.left ?? margins.inlineEnd ?? Margin.zero();
267+
rightMargin = margins.right ?? margins.inlineStart ?? Margin.zero();
268+
break;
269+
case TextDirection.ltr:
270+
leftMargin = margins.left ?? margins.inlineStart ?? Margin.zero();
271+
rightMargin = margins.right ?? margins.inlineEnd ?? Margin.zero();
272+
break;
273+
}
260274

261275
//Preprocess margins to a pixel value
262276
leftMargin.normalize(emValue);
@@ -593,7 +607,20 @@ class _RenderCSSBox extends RenderBox
593607
final offsetHeight = distance -
594608
(markerBox.getDistanceToBaseline(TextBaseline.alphabetic) ??
595609
markerBox.size.height);
596-
markerBoxParentData.offset = Offset(-markerBox.size.width, offsetHeight);
610+
switch (_textDirection) {
611+
case TextDirection.rtl:
612+
markerBoxParentData.offset = Offset(
613+
child.size.width,
614+
offsetHeight,
615+
);
616+
break;
617+
case TextDirection.ltr:
618+
markerBoxParentData.offset = Offset(
619+
-markerBox.size.width,
620+
offsetHeight,
621+
);
622+
break;
623+
}
597624
}
598625
}
599626

@@ -701,10 +728,11 @@ class _RenderCSSBox extends RenderBox
701728
}
702729

703730
return Margins(
704-
left: marginLeft,
705-
right: marginRight,
706-
top: margins.top,
707-
bottom: margins.bottom);
731+
left: marginLeft,
732+
right: marginRight,
733+
top: margins.top,
734+
bottom: margins.bottom,
735+
);
708736
}
709737

710738
@override

0 commit comments

Comments
 (0)
0