@@ -9,6 +9,7 @@ class CSSBoxWidget extends StatelessWidget {
9
9
this .key,
10
10
required this .child,
11
11
required this .style,
12
+ this .textDirection,
12
13
this .childIsReplaced = false ,
13
14
this .shrinkWrap = false ,
14
15
}): super (key: key);
@@ -18,6 +19,7 @@ class CSSBoxWidget extends StatelessWidget {
18
19
this .key,
19
20
required List <InlineSpan > children,
20
21
required this .style,
22
+ this .textDirection,
21
23
this .childIsReplaced = false ,
22
24
this .shrinkWrap = false ,
23
25
bool selectable = false ,
@@ -44,6 +46,11 @@ class CSSBoxWidget extends StatelessWidget {
44
46
/// Note that this style will only apply to this box, and will not cascade to its child.
45
47
final Style style;
46
48
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
+
47
54
/// Indicates whether this child is a replaced element that manages its own width
48
55
/// (e.g. img, video, iframe, audio, etc.)
49
56
final bool childIsReplaced;
@@ -63,6 +70,7 @@ class CSSBoxWidget extends StatelessWidget {
63
70
display: style.display ?? Display .BLOCK ,
64
71
childIsReplaced: childIsReplaced,
65
72
emValue: _calculateEmValue (style, context),
73
+ textDirection: _checkTextDirection (context, textDirection),
66
74
shrinkWrap: shrinkWrap,
67
75
child: Container (
68
76
decoration: BoxDecoration (
@@ -129,6 +137,16 @@ class CSSBoxWidget extends StatelessWidget {
129
137
! childIsReplaced &&
130
138
! shrinkWrap;
131
139
}
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
+ }
132
150
}
133
151
134
152
class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
@@ -141,6 +159,7 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
141
159
required this .height,
142
160
required this .borderSize,
143
161
required this .paddingSize,
162
+ required this .textDirection,
144
163
required this .childIsReplaced,
145
164
required this .emValue,
146
165
required this .shrinkWrap,
@@ -164,6 +183,9 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
164
183
/// The collapsed size of the element's padding
165
184
final Size paddingSize;
166
185
186
+ /// The direction for this widget's text to flow.
187
+ final TextDirection textDirection;
188
+
167
189
/// Whether or not the child being rendered is a replaced element
168
190
/// (this changes the rules for rendering)
169
191
final bool childIsReplaced;
@@ -184,6 +206,7 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
184
206
margins: _preProcessMargins (margins, shrinkWrap),
185
207
borderSize: borderSize,
186
208
paddingSize: paddingSize,
209
+ textDirection: textDirection,
187
210
childIsReplaced: childIsReplaced,
188
211
shrinkWrap: shrinkWrap,
189
212
);
@@ -198,6 +221,7 @@ class _CSSBoxRenderer extends MultiChildRenderObjectWidget {
198
221
..margins = _preProcessMargins (margins, shrinkWrap)
199
222
..borderSize = borderSize
200
223
..paddingSize = paddingSize
224
+ ..textDirection = textDirection
201
225
..childIsReplaced = childIsReplaced
202
226
..shrinkWrap = shrinkWrap;
203
227
}
@@ -257,6 +281,7 @@ class _RenderCSSBox extends RenderBox
257
281
required Margins margins,
258
282
required Size borderSize,
259
283
required Size paddingSize,
284
+ required TextDirection textDirection,
260
285
required bool childIsReplaced,
261
286
required bool shrinkWrap,
262
287
}) : _display = display,
@@ -265,6 +290,7 @@ class _RenderCSSBox extends RenderBox
265
290
_margins = margins,
266
291
_borderSize = borderSize,
267
292
_paddingSize = paddingSize,
293
+ _textDirection = textDirection,
268
294
_childIsReplaced = childIsReplaced,
269
295
_shrinkWrap = shrinkWrap;
270
296
@@ -322,6 +348,15 @@ class _RenderCSSBox extends RenderBox
322
348
markNeedsLayout ();
323
349
}
324
350
351
+ TextDirection _textDirection;
352
+
353
+ TextDirection get textDirection => _textDirection;
354
+
355
+ set textDirection (TextDirection textDirection) {
356
+ _textDirection = textDirection;
357
+ markNeedsLayout ();
358
+ }
359
+
325
360
bool _childIsReplaced;
326
361
327
362
bool get childIsReplaced => _childIsReplaced;
@@ -387,8 +422,6 @@ class _RenderCSSBox extends RenderBox
387
422
@override
388
423
double ? computeDistanceToActualBaseline (TextBaseline baseline) {
389
424
return firstChild? .getDistanceToActualBaseline (baseline);
390
- return defaultComputeDistanceToHighestActualBaseline (baseline);
391
- //TODO TODO TODO
392
425
}
393
426
394
427
@override
@@ -556,13 +589,26 @@ class _RenderCSSBox extends RenderBox
556
589
557
590
// If all values are non-auto, the box is overconstrained.
558
591
// 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.
560
593
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
+
566
612
}
567
613
568
614
// If there is exactly one value specified as auto, compute it value from the equality (our widths are already set)
0 commit comments