Skip to content

Commit cf23133

Browse files
author
fbchen
committed
add relative id
1 parent c7ad856 commit cf23133

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

example/home.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'complex_list.dart';
66
import 'dimension_ratio.dart';
77
import 'guideline.dart';
88
import 'percentage_layout.dart';
9+
import 'relative_id.dart';
910
import 'summary.dart';
1011

1112
class ExampleHome extends StatelessWidget {
@@ -48,6 +49,9 @@ class ExampleHome extends StatelessWidget {
4849
button('DimensionRatio', () {
4950
push(context, const DimensionRatioExample());
5051
}),
52+
button('Relative Id', () {
53+
push(context, const RelativeIdExample());
54+
}),
5155
button('Chain (Coming soon)', null),
5256
const Spacer(),
5357
const Text(

example/relative_id.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_constraintlayout/src/constraint_layout.dart';
3+
4+
import 'custom_app_bar.dart';
5+
6+
class RelativeIdExample extends StatelessWidget {
7+
const RelativeIdExample({Key? key}) : super(key: key);
8+
9+
@override
10+
Widget build(BuildContext context) {
11+
return Scaffold(
12+
appBar: const CustomAppBar(
13+
title: 'Relative Id',
14+
codePath: 'example/relative_id.dart',
15+
),
16+
body: ConstraintLayout(
17+
children: [
18+
Container(
19+
color: Colors.yellow,
20+
).applyConstraint(
21+
width: 200,
22+
height: 200,
23+
centerTo: parent,
24+
),
25+
Container(
26+
color: Colors.green,
27+
child: const Text(
28+
'Indeterminate badge size',
29+
style: TextStyle(
30+
color: Colors.black,
31+
fontSize: 20,
32+
),
33+
),
34+
).applyConstraint(
35+
left: rId(0).right,
36+
bottom: rId(0).top,
37+
translate: const Offset(-0.5, 0.5),
38+
percentageTranslate: true,
39+
),
40+
Container(
41+
color: Colors.green,
42+
).applyConstraint(
43+
width: 100,
44+
height: 100,
45+
left: rId(0).right,
46+
right: rId(0).right,
47+
top: rId(0).bottom,
48+
bottom: rId(0).bottom,
49+
)
50+
],
51+
),
52+
);
53+
}
54+
}

lib/src/constraint_layout.dart

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,32 @@ class ConstraintId {
413413
}
414414
}
415415

416+
class RelativeConstraintId extends ConstraintId {
417+
final int childIndex;
418+
419+
RelativeConstraintId(this.childIndex) : super('child[$childIndex]');
420+
421+
@override
422+
bool operator ==(Object other) =>
423+
identical(this, other) ||
424+
super == other &&
425+
other is RelativeConstraintId &&
426+
runtimeType == other.runtimeType &&
427+
childIndex == other.childIndex;
428+
429+
@override
430+
int get hashCode => super.hashCode ^ childIndex.hashCode;
431+
432+
@override
433+
String toString() {
434+
return 'RelativeConstraintId{childIndex: $childIndex}';
435+
}
436+
}
437+
438+
ConstraintId rId(int childIndex) {
439+
return RelativeConstraintId(childIndex);
440+
}
441+
416442
class _Align {
417443
ConstraintId? id;
418444
_AlignType type;
@@ -1388,9 +1414,11 @@ class _ConstraintRenderBox extends RenderBox
13881414

13891415
/// The id used by all constraints must be defined
13901416
Set<ConstraintId> illegalIdSet = constraintsIdSet.difference(idSet);
1391-
if (illegalIdSet.isNotEmpty) {
1417+
Set<RelativeConstraintId> relativeIds =
1418+
illegalIdSet.whereType<RelativeConstraintId>().toSet();
1419+
if (relativeIds.length != illegalIdSet.length) {
13921420
throw ConstraintLayoutException(
1393-
'These ids $illegalIdSet are not yet defined.');
1421+
'These ids ${illegalIdSet.difference(relativeIds)} are not yet defined.');
13941422
}
13951423

13961424
/// All ids referenced by Barrier must be defined
@@ -1536,8 +1564,7 @@ class _ConstraintRenderBox extends RenderBox
15361564
childParentData._constrainedNodeMap = nodesMap;
15371565

15381566
_ConstrainedNode currentNode = _getConstrainedNodeForChild(
1539-
childParentData.id ??
1540-
ConstraintId('child[$childIndex]@${child.runtimeType}'));
1567+
childParentData.id ?? RelativeConstraintId(childIndex));
15411568
currentNode.parentData = childParentData;
15421569
currentNode.index = childIndex;
15431570
currentNode.renderBox = child;

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ flutter:
2828
- example/guideline.dart
2929
- example/summary.dart
3030
- example/percentage_layout.dart
31-
- example/dimension_ratio.dart
31+
- example/dimension_ratio.dart
32+
- example/relative_id.dart

0 commit comments

Comments
 (0)