Skip to content

Commit ea07327

Browse files
committed
initial support for rtl
1 parent 2fdd9e9 commit ea07327

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

example/pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ packages:
5858
flutter_constraintlayout:
5959
dependency: "direct main"
6060
description:
61-
name: flutter_constraintlayout
62-
url: "https://pub.dartlang.org"
63-
source: hosted
61+
path: ".."
62+
relative: true
63+
source: path
6464
version: "1.6.3-stable"
6565
flutter_lints:
6666
dependency: "direct dev"

example/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ environment:
2929
dependencies:
3030
flutter:
3131
sdk: flutter
32-
flutter_constraintlayout: ^1.6.3-stable
32+
flutter_constraintlayout:
33+
path: ../
3334

3435
dev_dependencies:
3536
flutter_test:

lib/src/core.dart

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
8080
///these operations to stop parameter comparison.
8181
final ConstraintLayoutController? controller;
8282

83+
final bool rtl;
84+
8385
ConstraintLayout({
8486
Key? key,
8587
this.childConstraints,
@@ -94,6 +96,7 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
9496
this.height = matchParent,
9597
this.size,
9698
this.controller,
99+
this.rtl = true,
97100
}) : super(
98101
key: key,
99102
children: children ?? [],
@@ -121,7 +124,8 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
121124
.._debugPrintConstraints = debugPrintConstraints
122125
.._width = selfWidth
123126
.._height = selfHeight
124-
.._controller = controller?._copy();
127+
.._controller = controller?._copy()
128+
.._rtl = rtl;
125129
}
126130

127131
@override
@@ -149,7 +153,8 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
149153
..debugPrintConstraints = debugPrintConstraints
150154
..width = selfWidth
151155
..height = selfHeight
152-
..controller = controller?._copy();
156+
..controller = controller?._copy()
157+
..rtl = rtl;
153158
}
154159
}
155160

@@ -704,15 +709,15 @@ class Constraint extends ConstraintDefine {
704709
/// These are the base constraints constraint on sibling id or parent
705710
/// The essence of constraints is alignment
706711
@_baseConstraint
707-
ConstraintAlign? left;
712+
final ConstraintAlign? left;
708713
@_baseConstraint
709-
ConstraintAlign? top;
714+
final ConstraintAlign? top;
710715
@_baseConstraint
711-
ConstraintAlign? right;
716+
final ConstraintAlign? right;
712717
@_baseConstraint
713-
ConstraintAlign? bottom;
718+
final ConstraintAlign? bottom;
714719
@_baseConstraint
715-
ConstraintAlign? baseline;
720+
final ConstraintAlign? baseline;
716721

717722
/// When setting baseline alignment, height must be wrap_content or fixed size, other vertical constraints will be illegal.
718723
/// Warning: Due to a bug in the flutter framework, baseline alignment may not take effect in debug mode
@@ -1095,6 +1100,9 @@ class Constraint extends ConstraintDefine {
10951100
}
10961101

10971102
void _applyTo(RenderObject renderObject) {
1103+
_ConstraintRenderBox constraintRenderBox =
1104+
(renderObject.parent as _ConstraintRenderBox);
1105+
10981106
ConstraintAlign? left = this.left;
10991107
ConstraintAlign? top = this.top;
11001108
ConstraintAlign? right = this.right;
@@ -1271,9 +1279,58 @@ class Constraint extends ConstraintDefine {
12711279
bottom = centerBottomRightTo!.bottom;
12721280
}
12731281

1282+
double horizontalBias = this.horizontalBias;
1283+
12741284
EdgeInsets margin = this.margin;
12751285
EdgeInsets goneMargin = this.goneMargin;
12761286

1287+
if (constraintRenderBox._rtl) {
1288+
ConstraintAlign? convertedLeft;
1289+
ConstraintAlign? convertedRight;
1290+
if (left != null) {
1291+
/// left: box.right -> right: box.left
1292+
ConstraintAlignType type;
1293+
if (left._type == ConstraintAlignType.left) {
1294+
type = ConstraintAlignType.right;
1295+
} else if (left._type == ConstraintAlignType.right) {
1296+
type = ConstraintAlignType.left;
1297+
} else {
1298+
type = ConstraintAlignType.center;
1299+
}
1300+
convertedRight = ConstraintAlign(left._id, type);
1301+
convertedRight._margin = left._margin;
1302+
convertedRight._goneMargin = left._goneMargin;
1303+
if (left._bias != null) {
1304+
convertedRight._bias = 1 - left._bias!;
1305+
}
1306+
}
1307+
if (right != null) {
1308+
/// right: box.left -> left: box.right
1309+
ConstraintAlignType type;
1310+
if (right._type == ConstraintAlignType.left) {
1311+
type = ConstraintAlignType.right;
1312+
} else if (right._type == ConstraintAlignType.right) {
1313+
type = ConstraintAlignType.left;
1314+
} else {
1315+
type = ConstraintAlignType.center;
1316+
}
1317+
convertedLeft = ConstraintAlign(right._id, type);
1318+
convertedLeft._margin = right._margin;
1319+
convertedLeft._goneMargin = right._goneMargin;
1320+
if (right._bias != null) {
1321+
convertedLeft._bias = 1 - right._bias!;
1322+
}
1323+
}
1324+
left = convertedLeft;
1325+
right = convertedRight;
1326+
1327+
horizontalBias = 1 - horizontalBias;
1328+
1329+
margin = margin.copyWith(left: margin.right, right: margin.left);
1330+
goneMargin =
1331+
goneMargin.copyWith(left: goneMargin.right, right: goneMargin.left);
1332+
}
1333+
12771334
if (left != null) {
12781335
if (left._margin != null) {
12791336
margin = margin.add(EdgeInsets.only(
@@ -1373,7 +1430,7 @@ class Constraint extends ConstraintDefine {
13731430
parentData.layoutCallback = layoutCallback;
13741431
parentData.paintCallback = paintCallback;
13751432

1376-
if ((renderObject.parent as _ConstraintRenderBox)._controller != null) {
1433+
if (constraintRenderBox._controller != null) {
13771434
parentData.id = _id;
13781435
parentData.width = width;
13791436
parentData.height = height;
@@ -1840,6 +1897,7 @@ class _ConstraintRenderBox extends RenderBox
18401897
late bool _showZIndex;
18411898
late bool _showChildDepth;
18421899
late bool _debugPrintConstraints;
1900+
late bool _rtl;
18431901

18441902
late double _width;
18451903
late double _height;
@@ -2023,6 +2081,14 @@ class _ConstraintRenderBox extends RenderBox
20232081
}
20242082
}
20252083

2084+
set rtl(bool value) {
2085+
if (_rtl != value) {
2086+
_rtl = value;
2087+
markNeedsRecalculateConstraints();
2088+
markNeedsLayout();
2089+
}
2090+
}
2091+
20262092
@override
20272093
void setupParentData(covariant RenderObject child) {
20282094
if (child.parentData is! ConstraintBoxData) {

0 commit comments

Comments
 (0)