Skip to content

Commit bcee9b3

Browse files
committed
WIP: Fix directionality TODO
1 parent bb9145e commit bcee9b3

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

lib/src/css_box_widget.dart

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class CSSBoxWidget extends StatelessWidget {
99
this.key,
1010
required this.child,
1111
required this.style,
12+
this.textDirection,
1213
this.childIsReplaced = false,
1314
this.shrinkWrap = false,
1415
}): super(key: key);
@@ -18,6 +19,7 @@ class CSSBoxWidget extends StatelessWidget {
1819
this.key,
1920
required List<InlineSpan> children,
2021
required this.style,
22+
this.textDirection,
2123
this.childIsReplaced = false,
2224
this.shrinkWrap = false,
2325
bool selectable = false,
@@ -44,6 +46,11 @@ class CSSBoxWidget extends StatelessWidget {
4446
/// Note that this style will only apply to this box, and will not cascade to its child.
4547
final Style style;
4648

49+
/// Sets the direction the text of this widget should flow. If unset or null,
50+
/// the nearest Directionality ancestor is used as a default. If that cannot
51+
/// be found, this Widget's renderer will raise an assertion.
52+
final TextDirection? textDirection;
53+
4754
/// Indicates whether this child is a replaced element that manages its own width
4855
/// (e.g. img, video, iframe, audio, etc.)
4956
final bool childIsReplaced;
@@ -63,6 +70,7 @@ class CSSBoxWidget extends StatelessWidget {
6370
display: style.display ?? Display.BLOCK,
6471
childIsReplaced: childIsReplaced,
6572
emValue: _calculateEmValue(style, context),
73+
textDirection: _checkTextDirection(context, textDirection),
6674
shrinkWrap: shrinkWrap,
6775
child: Container(
6876
decoration: BoxDecoration(
@@ -129,6 +137,16 @@ class CSSBoxWidget extends StatelessWidget {
129137
!childIsReplaced &&
130138
!shrinkWrap;
131139
}
140+
141+
TextDirection _checkTextDirection(BuildContext context, TextDirection? direction) {
142+
final textDirection = direction ?? Directionality.maybeOf(context);
143+
144+
assert(textDirection != null,
145+
"CSSBoxWidget needs either a Directionality ancestor or a provided textDirection",
146+
);
147+
148+
return textDirection!;
149+
}
132150
}
133151

134152
class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
@@ -141,6 +159,7 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
141159
required this.height,
142160
required this.borderSize,
143161
required this.paddingSize,
162+
required this.textDirection,
144163
required this.childIsReplaced,
145164
required this.emValue,
146165
required this.shrinkWrap,
@@ -164,6 +183,9 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
164183
/// The collapsed size of the element's padding
165184
final Size paddingSize;
166185

186+
/// The direction for this widget's text to flow.
187+
final TextDirection textDirection;
188+
167189
/// Whether or not the child being rendered is a replaced element
168190
/// (this changes the rules for rendering)
169191
final bool childIsReplaced;
@@ -184,6 +206,7 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
184206
margins: _preProcessMargins(margins, shrinkWrap),
185207
borderSize: borderSize,
186208
paddingSize: paddingSize,
209+
textDirection: textDirection,
187210
childIsReplaced: childIsReplaced,
188211
shrinkWrap: shrinkWrap,
189212
);
@@ -198,6 +221,7 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
198221
..margins = _preProcessMargins(margins, shrinkWrap)
199222
..borderSize = borderSize
200223
..paddingSize = paddingSize
224+
..textDirection = textDirection
201225
..childIsReplaced = childIsReplaced
202226
..shrinkWrap = shrinkWrap;
203227
}
@@ -257,6 +281,7 @@ class _RenderCSSBox extends RenderBox
257281
required Margins margins,
258282
required Size borderSize,
259283
required Size paddingSize,
284+
required TextDirection textDirection,
260285
required bool childIsReplaced,
261286
required bool shrinkWrap,
262287
}) : _display = display,
@@ -265,6 +290,7 @@ class _RenderCSSBox extends RenderBox
265290
_margins = margins,
266291
_borderSize = borderSize,
267292
_paddingSize = paddingSize,
293+
_textDirection = textDirection,
268294
_childIsReplaced = childIsReplaced,
269295
_shrinkWrap = shrinkWrap;
270296

@@ -322,6 +348,15 @@ class _RenderCSSBox extends RenderBox
322348
markNeedsLayout();
323349
}
324350

351+
TextDirection _textDirection;
352+
353+
TextDirection get textDirection => _textDirection;
354+
355+
set textDirection(TextDirection textDirection) {
356+
_textDirection = textDirection;
357+
markNeedsLayout();
358+
}
359+
325360
bool _childIsReplaced;
326361

327362
bool get childIsReplaced => _childIsReplaced;
@@ -387,8 +422,6 @@ class _RenderCSSBox extends RenderBox
387422
@override
388423
double? computeDistanceToActualBaseline(TextBaseline baseline) {
389424
return firstChild?.getDistanceToActualBaseline(baseline);
390-
return defaultComputeDistanceToHighestActualBaseline(baseline);
391-
//TODO TODO TODO
392425
}
393426

394427
@override
@@ -556,13 +589,26 @@ class _RenderCSSBox extends RenderBox
556589

557590
// If all values are non-auto, the box is overconstrained.
558591
// One of the margins will need to be adjusted so that the
559-
// entire width is taken
592+
// entire width of the containing block is used.
560593
if (!widthIsAuto && !marginLeftIsAuto && !marginRightIsAuto && !shrinkWrap && !childIsReplaced) {
561-
//TODO ignore either left or right margin based on directionality of parent widgets.
562-
//For now, assume ltr, and just ignore the right margin.
563-
final difference =
564-
containingBlockSize.width - childSize.width - marginLeft.value;
565-
marginRight = Margin(difference);
594+
//Ignore either left or right margin based on textDirection.
595+
596+
switch(textDirection) {
597+
case TextDirection.rtl:
598+
final difference = containingBlockSize.width
599+
- childSize.width
600+
- marginRight.value;
601+
marginLeft = Margin(difference);
602+
break;
603+
case TextDirection.ltr:
604+
final difference = containingBlockSize.width
605+
- childSize.width
606+
- marginLeft.value;
607+
marginRight = Margin(difference);
608+
break;
609+
}
610+
611+
566612
}
567613

568614
// If there is exactly one value specified as auto, compute it value from the equality (our widths are already set)

0 commit comments

Comments
 (0)