Skip to content

Commit c7a2c11

Browse files
author
fbchen
committed
fix unnecessary relayout
1 parent 9d9d591 commit c7a2c11

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

lib/src/constraint_layout.dart

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import 'package:flutter/rendering.dart';
2222
2323
class ConstraintLayout extends MultiChildRenderObjectWidget {
2424
/// Constraints can be separated from widgets
25-
final List<Constraint>? childConstraints;
25+
final List<Constraint> childConstraints;
2626

2727
final bool debugShowGuideline;
2828
final bool debugShowClickArea;
@@ -35,7 +35,7 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
3535

3636
ConstraintLayout({
3737
Key? key,
38-
this.childConstraints,
38+
this.childConstraints = const [],
3939
required List<Widget> children,
4040
this.debugShowGuideline = false,
4141
this.debugShowClickArea = false,
@@ -113,7 +113,8 @@ List<Widget> horizontalChain({
113113
// packed
114114
}
115115

116-
ConstraintId guidelineId = ConstraintId();
116+
ConstraintId guidelineId = ConstraintId(
117+
'internal_horizontal_chain_guideline_$i@${chainList[0].constraint.hashCode}');
117118
Guideline guideline = Guideline(
118119
id: guidelineId,
119120
horizontal: false,
@@ -276,7 +277,7 @@ bool _debugEnsureNegativePercent(String name, double? percent) {
276277
return true;
277278
}
278279

279-
ConstraintId parent = ConstraintId();
280+
ConstraintId parent = ConstraintId('parent');
280281
const double matchConstraint = -3.1415926;
281282
const double matchParent = -2.7182818;
282283
const double wrapContent = -0.6180339;
@@ -309,9 +310,9 @@ enum PercentageAnchor {
309310
}
310311

311312
class ConstraintId {
312-
String? name;
313+
String id;
313314

314-
ConstraintId({this.name}) {
315+
ConstraintId(this.id) {
315316
left.id = this;
316317
top.id = this;
317318
right.id = this;
@@ -340,24 +341,17 @@ class ConstraintId {
340341
if (other.runtimeType != runtimeType) {
341342
return false;
342343
}
343-
if ((other).name == null && name == null) {
344-
return false;
345-
}
346-
return (other).name == name;
344+
return (other).id == id;
347345
}
348346

349347
@override
350348
int get hashCode {
351-
if (name == null) {
352-
return super.hashCode;
353-
} else {
354-
return name.hashCode;
355-
}
349+
return id.hashCode;
356350
}
357351

358352
@override
359353
String toString() {
360-
return 'ConstraintId{name: $name}';
354+
return 'ConstraintId{name: $id}';
361355
}
362356
}
363357

@@ -992,12 +986,10 @@ class UnConstrained extends ParentDataWidget<_ConstraintBoxData> {
992986
@override
993987
void applyParentData(RenderObject renderObject) {
994988
assert(renderObject.parent is _ConstraintRenderBox);
995-
List<Constraint>? childConstraints =
989+
List<Constraint> childConstraints =
996990
(renderObject.parent as _ConstraintRenderBox)._childConstraints;
997-
assert(childConstraints != null,
998-
'Can not find Constraint for child with id $id.');
999991
Iterable<Constraint> constraintIterable =
1000-
childConstraints!.where((element) => element.id == id);
992+
childConstraints.where((element) => element.id == id);
1001993
assert(constraintIterable.isNotEmpty,
1002994
'Can not find Constraint for child with id $id.');
1003995
assert(constraintIterable.length == 1, 'Duplicate id in childConstraints.');
@@ -1016,7 +1008,7 @@ class _ConstraintRenderBox extends RenderBox
10161008
with
10171009
ContainerRenderObjectMixin<RenderBox, _ConstraintBoxData>,
10181010
RenderBoxContainerDefaultsMixin<RenderBox, _ConstraintBoxData> {
1019-
List<Constraint>? _childConstraints;
1011+
late List<Constraint> _childConstraints;
10201012
late bool _debugShowGuideline;
10211013
late bool _debugShowClickArea;
10221014
late bool _debugPrintConstraints;
@@ -1031,8 +1023,19 @@ class _ConstraintRenderBox extends RenderBox
10311023
final Map<ConstraintId, _ConstrainedNode> _tempConstrainedNodes = HashMap();
10321024
late List<_ConstrainedNode> _paintingOrderList;
10331025

1034-
set childConstraints(List<Constraint>? value) {
1035-
if (_childConstraints != value) {
1026+
set childConstraints(List<Constraint> value) {
1027+
bool isSameList = true;
1028+
if (_childConstraints.length != value.length) {
1029+
isSameList = false;
1030+
} else {
1031+
for (int i = 0; i < _childConstraints.length; i++) {
1032+
if (_childConstraints[i] != value[i]) {
1033+
isSameList = false;
1034+
break;
1035+
}
1036+
}
1037+
}
1038+
if (!isSameList) {
10361039
_childConstraints = value;
10371040
markNeedsLayout();
10381041
}
@@ -1255,8 +1258,7 @@ class _ConstraintRenderBox extends RenderBox
12551258
child,
12561259
childParentData.id ??
12571260
ConstraintId(
1258-
name:
1259-
'child[$childIndex]@${child.runtimeType}@${child.hashCode}'));
1261+
'child[$childIndex]@${child.runtimeType}@${child.hashCode}'));
12601262
currentNode.parentData = childParentData;
12611263
currentNode.index = childIndex;
12621264

@@ -2583,7 +2585,18 @@ class _BarrierRenderBox extends _InternalBox {
25832585
}
25842586

25852587
set referencedIds(List<ConstraintId> value) {
2586-
if (_referencedIds != value) {
2588+
bool isSameList = true;
2589+
if (_referencedIds.length != value.length) {
2590+
isSameList = false;
2591+
} else {
2592+
for (int i = 0; i < _referencedIds.length; i++) {
2593+
if (_referencedIds[i] != value[i]) {
2594+
isSameList = false;
2595+
break;
2596+
}
2597+
}
2598+
}
2599+
if (!isSameList) {
25872600
_referencedIds = value;
25882601
updateParentData();
25892602
markParentNeedsLayout();

lib/src/examples/badge.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class BadgeExample extends StatelessWidget {
66

77
@override
88
Widget build(BuildContext context) {
9-
ConstraintId anchor = ConstraintId();
9+
ConstraintId anchor = ConstraintId('anchor');
1010
return MaterialApp(
1111
home: Scaffold(
1212
body: ConstraintLayout(

lib/src/examples/barrier.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class BarrierExample extends StatelessWidget {
66

77
@override
88
Widget build(BuildContext context) {
9-
ConstraintId leftChild = ConstraintId();
10-
ConstraintId rightChild = ConstraintId();
11-
ConstraintId barrier = ConstraintId();
9+
ConstraintId leftChild = ConstraintId('leftChild');
10+
ConstraintId rightChild = ConstraintId('rightChild');
11+
ConstraintId barrier = ConstraintId('barrier');
1212
return MaterialApp(
1313
home: Scaffold(
1414
body: ConstraintLayout(

lib/src/examples/guideline.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class GuidelineExample extends StatelessWidget {
66

77
@override
88
Widget build(BuildContext context) {
9-
ConstraintId guideline = ConstraintId();
9+
ConstraintId guideline = ConstraintId('guideline');
1010
return MaterialApp(
1111
home: Scaffold(
1212
body: ConstraintLayout(

lib/src/examples/sumary.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ class ExampleState extends State<Example> {
1212
double x = 0;
1313
double y = 0;
1414

15-
ConstraintId box0 = ConstraintId();
16-
ConstraintId box1 = ConstraintId();
17-
ConstraintId box2 = ConstraintId();
18-
ConstraintId box3 = ConstraintId();
19-
ConstraintId box4 = ConstraintId();
20-
ConstraintId box5 = ConstraintId();
21-
ConstraintId box6 = ConstraintId();
22-
ConstraintId box7 = ConstraintId();
23-
ConstraintId box8 = ConstraintId();
24-
ConstraintId box9 = ConstraintId();
25-
ConstraintId box10 = ConstraintId();
26-
ConstraintId box11 = ConstraintId();
27-
ConstraintId barrier = ConstraintId();
15+
ConstraintId box0 = ConstraintId('box0');
16+
ConstraintId box1 = ConstraintId('box1');
17+
ConstraintId box2 = ConstraintId('box2');
18+
ConstraintId box3 = ConstraintId('box3');
19+
ConstraintId box4 = ConstraintId('box4');
20+
ConstraintId box5 = ConstraintId('box5');
21+
ConstraintId box6 = ConstraintId('box6');
22+
ConstraintId box7 = ConstraintId('box7');
23+
ConstraintId box8 = ConstraintId('box8');
24+
ConstraintId box9 = ConstraintId('box9');
25+
ConstraintId box10 = ConstraintId('box10');
26+
ConstraintId box11 = ConstraintId('box11');
27+
ConstraintId barrier = ConstraintId('barrier');
2828

2929
@override
3030
Widget build(BuildContext context) {

0 commit comments

Comments
 (0)