diff --git a/packages/flutter/lib/src/rendering/stack.dart b/packages/flutter/lib/src/rendering/stack.dart index 2ee3a918f5..69b67f7407 100644 --- a/packages/flutter/lib/src/rendering/stack.dart +++ b/packages/flutter/lib/src/rendering/stack.dart @@ -56,6 +56,37 @@ class RelativeRect { ); } + /// Creates a RelativeRect from horizontal position using `start` and `end` + /// rather than `left` and `right`. + /// + /// If `textDirection` is [TextDirection.rtl], then the `start` argument is + /// used for the [right] property and the `end` argument is used for the + /// [left] property. Otherwise, if `textDirection` is [TextDirection.ltr], + /// then the `start` argument is used for the [left] property and the `end` + /// argument is used for the [right] property. + factory RelativeRect.fromDirectional({ + required TextDirection textDirection, + required double start, + required double top, + required double end, + required double bottom, + }) { + double left; + double right; + switch (textDirection) { + case TextDirection.rtl: + left = end; + right = start; + break; + case TextDirection.ltr: + left = start; + right = end; + break; + } + + return RelativeRect.fromLTRB(left, top, right, bottom); + } + /// A rect that covers the entire container. static const RelativeRect fill = RelativeRect.fromLTRB(0.0, 0.0, 0.0, 0.0); diff --git a/packages/flutter/test/rendering/relative_rect_test.dart b/packages/flutter/test/rendering/relative_rect_test.dart index b998ef4f7b..cd78de6511 100644 --- a/packages/flutter/test/rendering/relative_rect_test.dart +++ b/packages/flutter/test/rendering/relative_rect_test.dart @@ -10,6 +10,24 @@ void main() { const RelativeRect r = RelativeRect.fromLTRB(10.0, 20.0, 30.0, 40.0); expect(r, RelativeRect.fromSize(const Rect.fromLTWH(10.0, 20.0, 0.0, 0.0), const Size(40.0, 60.0))); }); + test('RelativeRect.fromDirectional', () { + final RelativeRect r1 = RelativeRect.fromDirectional( + textDirection: TextDirection.ltr, + start: 10.0, + top: 20.0, + end: 30.0, + bottom: 40.0, + ); + final RelativeRect r2 = RelativeRect.fromDirectional( + textDirection: TextDirection.rtl, + start: 10.0, + top: 20.0, + end: 30.0, + bottom: 40.0, + ); + expect(r1, const RelativeRect.fromLTRB(10.0, 20.0, 30.0, 40.0)); + expect(r2, const RelativeRect.fromLTRB(30.0, 20.0, 10.0, 40.0)); + }); test('RelativeRect.shift', () { const RelativeRect r1 = RelativeRect.fromLTRB(10.0, 20.0, 30.0, 40.0); final RelativeRect r2 = r1.shift(const Offset(5.0, 50.0));