Skip to content

Commit 41405ff

Browse files
authored
Add RelativeRect.fromDirectional factory (#107059)
Add `fromDirectional` factory to `RelativeRect` for when an app needs to consider both TextDirection.ltr and TextDirection.rtl. ``` RelativeRect.fromDirectional( textDirection: textDirection, start: 10.0, top: 20.0, end: 30.0, bottom: 40.0, ); ``` Addresses flutter/flutter#107058.
1 parent 118248b commit 41405ff

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/flutter/lib/src/rendering/stack.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ class RelativeRect {
5656
);
5757
}
5858

59+
/// Creates a RelativeRect from horizontal position using `start` and `end`
60+
/// rather than `left` and `right`.
61+
///
62+
/// If `textDirection` is [TextDirection.rtl], then the `start` argument is
63+
/// used for the [right] property and the `end` argument is used for the
64+
/// [left] property. Otherwise, if `textDirection` is [TextDirection.ltr],
65+
/// then the `start` argument is used for the [left] property and the `end`
66+
/// argument is used for the [right] property.
67+
factory RelativeRect.fromDirectional({
68+
required TextDirection textDirection,
69+
required double start,
70+
required double top,
71+
required double end,
72+
required double bottom,
73+
}) {
74+
double left;
75+
double right;
76+
switch (textDirection) {
77+
case TextDirection.rtl:
78+
left = end;
79+
right = start;
80+
break;
81+
case TextDirection.ltr:
82+
left = start;
83+
right = end;
84+
break;
85+
}
86+
87+
return RelativeRect.fromLTRB(left, top, right, bottom);
88+
}
89+
5990
/// A rect that covers the entire container.
6091
static const RelativeRect fill = RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0);
6192

packages/flutter/test/rendering/relative_rect_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ void main() {
1010
const RelativeRect r = RelativeRect.fromLTRB(10.0, 20.0, 30.0, 40.0);
1111
expect(r, RelativeRect.fromSize(const Rect.fromLTWH(10.0, 20.0, 0.0, 0.0), const Size(40.0, 60.0)));
1212
});
13+
test('RelativeRect.fromDirectional', () {
14+
final RelativeRect r1 = RelativeRect.fromDirectional(
15+
textDirection: TextDirection.ltr,
16+
start: 10.0,
17+
top: 20.0,
18+
end: 30.0,
19+
bottom: 40.0,
20+
);
21+
final RelativeRect r2 = RelativeRect.fromDirectional(
22+
textDirection: TextDirection.rtl,
23+
start: 10.0,
24+
top: 20.0,
25+
end: 30.0,
26+
bottom: 40.0,
27+
);
28+
expect(r1, const RelativeRect.fromLTRB(10.0, 20.0, 30.0, 40.0));
29+
expect(r2, const RelativeRect.fromLTRB(30.0, 20.0, 10.0, 40.0));
30+
});
1331
test('RelativeRect.shift', () {
1432
const RelativeRect r1 = RelativeRect.fromLTRB(10.0, 20.0, 30.0, 40.0);
1533
final RelativeRect r2 = r1.shift(const Offset(5.0, 50.0));

0 commit comments

Comments
 (0)