Skip to content

Commit c3dea1e

Browse files
author
fbchen
committed
add layout callback
1 parent 8459e85 commit c3dea1e

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lib/constrait_layout/constraint_layout.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ extension WidgetsExt on Widget {
169169
@_wrapperConstraint ConstraintId? bottomRightTo,
170170
@_wrapperConstraint ConstraintId? centerHorizontalTo,
171171
@_wrapperConstraint ConstraintId? centerVerticalTo,
172+
OnLayoutCallback? callback,
172173
}) {
173174
return Constrained(
174175
key: key,
@@ -206,6 +207,7 @@ extension WidgetsExt on Widget {
206207
bottomRightTo: bottomRightTo,
207208
centerHorizontalTo: centerHorizontalTo,
208209
centerVerticalTo: centerVerticalTo,
210+
callback: callback,
209211
),
210212
child: this,
211213
);
@@ -352,6 +354,8 @@ class _Align {
352354
int get hashCode => id.hashCode ^ type.hashCode;
353355
}
354356

357+
typedef OnLayoutCallback = void Function(RenderObject renderObject, Rect rect);
358+
355359
class Constraint {
356360
/// id can be null, but not an empty string
357361
final ConstraintId? id;
@@ -433,6 +437,8 @@ class Constraint {
433437
@_wrapperConstraint
434438
final ConstraintId? centerVerticalTo;
435439

440+
final OnLayoutCallback? callback;
441+
436442
const Constraint({
437443
this.id,
438444
required this.width,
@@ -467,6 +473,7 @@ class Constraint {
467473
@_wrapperConstraint this.bottomRightTo,
468474
@_wrapperConstraint this.centerHorizontalTo,
469475
@_wrapperConstraint this.centerVerticalTo,
476+
this.callback,
470477
});
471478

472479
@override
@@ -506,7 +513,8 @@ class Constraint {
506513
bottomCenterTo == other.bottomCenterTo &&
507514
bottomRightTo == other.bottomRightTo &&
508515
centerHorizontalTo == other.centerHorizontalTo &&
509-
centerVerticalTo == other.centerVerticalTo;
516+
centerVerticalTo == other.centerVerticalTo &&
517+
callback == other.callback;
510518

511519
@override
512520
int get hashCode =>
@@ -542,7 +550,8 @@ class Constraint {
542550
bottomCenterTo.hashCode ^
543551
bottomRightTo.hashCode ^
544552
centerHorizontalTo.hashCode ^
545-
centerVerticalTo.hashCode;
553+
centerVerticalTo.hashCode ^
554+
callback.hashCode;
546555

547556
bool checkSize(double size) {
548557
if (size == matchParent || size == wrapContent || size == matchConstraint) {
@@ -798,6 +807,8 @@ class Constraint {
798807
needsLayout = true;
799808
}
800809

810+
parentData.callback = callback;
811+
801812
if (needsLayout) {
802813
AbstractNode? targetParent = renderObject.parent;
803814
if (targetParent is RenderObject) {
@@ -851,6 +862,7 @@ class _ConstraintBoxData extends ContainerBoxParentData<RenderBox> {
851862
PercentageAnchor? heightPercentageAnchor;
852863
double? horizontalBias;
853864
double? verticalBias;
865+
OnLayoutCallback? callback;
854866
}
855867

856868
class Constrained extends ParentDataWidget<_ConstraintBoxData> {
@@ -1625,6 +1637,12 @@ class _ConstraintRenderBox extends RenderBox
16251637
}
16261638

16271639
element.offset = Offset(offsetX, offsetY);
1640+
if (element.callback != null) {
1641+
element.callback!.call(
1642+
element.renderBox!,
1643+
Rect.fromLTWH(offsetX, offsetY, element.getMeasuredWidth(size),
1644+
element.getMeasuredHeight(size)));
1645+
}
16281646
}
16291647
}
16301648

@@ -1847,6 +1865,8 @@ class _ConstrainedNode {
18471865
PercentageAnchor get heightPercentageAnchor =>
18481866
parentData.heightPercentageAnchor!;
18491867

1868+
OnLayoutCallback? get callback => parentData.callback;
1869+
18501870
set offset(Offset value) {
18511871
parentData.offset = value;
18521872
}

0 commit comments

Comments
 (0)