Skip to content

Commit e37ab48

Browse files
authored
Introduce debugWithActiveLayoutCleared to avoid duplicated code (#114003)
1 parent fb9065f commit e37ab48

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

packages/flutter/lib/src/rendering/object.dart

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,26 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
15911591
static RenderObject? get debugActiveLayout => _debugActiveLayout;
15921592
static RenderObject? _debugActiveLayout;
15931593

1594+
/// Set [debugActiveLayout] to null when [inner] callback is called.
1595+
/// This is useful when you have to temporarily clear that variable to
1596+
/// disable some false-positive checks, such as when computing toStringDeep
1597+
/// or using custom trees.
1598+
@pragma('vm:prefer-inline')
1599+
static T _withDebugActiveLayoutCleared<T>(T Function() inner) {
1600+
RenderObject? debugPreviousActiveLayout;
1601+
assert(() {
1602+
debugPreviousActiveLayout = _debugActiveLayout;
1603+
_debugActiveLayout = null;
1604+
return true;
1605+
}());
1606+
final T result = inner();
1607+
assert(() {
1608+
_debugActiveLayout = debugPreviousActiveLayout;
1609+
return true;
1610+
}());
1611+
return result;
1612+
}
1613+
15941614
/// Whether the parent render object is permitted to use this render object's
15951615
/// size.
15961616
///
@@ -3399,22 +3419,11 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
33993419
String? prefixOtherLines = '',
34003420
DiagnosticLevel minLevel = DiagnosticLevel.debug,
34013421
}) {
3402-
RenderObject? debugPreviousActiveLayout;
3403-
assert(() {
3404-
debugPreviousActiveLayout = _debugActiveLayout;
3405-
_debugActiveLayout = null;
3406-
return true;
3407-
}());
3408-
final String result = super.toStringDeep(
3409-
prefixLineOne: prefixLineOne,
3410-
prefixOtherLines: prefixOtherLines,
3411-
minLevel: minLevel,
3412-
);
3413-
assert(() {
3414-
_debugActiveLayout = debugPreviousActiveLayout;
3415-
return true;
3416-
}());
3417-
return result;
3422+
return _withDebugActiveLayoutCleared(() => super.toStringDeep(
3423+
prefixLineOne: prefixLineOne,
3424+
prefixOtherLines: prefixOtherLines,
3425+
minLevel: minLevel,
3426+
));
34183427
}
34193428

34203429
/// Returns a one-line detailed description of the render object.
@@ -3427,18 +3436,7 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
34273436
String joiner = ', ',
34283437
DiagnosticLevel minLevel = DiagnosticLevel.debug,
34293438
}) {
3430-
RenderObject? debugPreviousActiveLayout;
3431-
assert(() {
3432-
debugPreviousActiveLayout = _debugActiveLayout;
3433-
_debugActiveLayout = null;
3434-
return true;
3435-
}());
3436-
final String result = super.toStringShallow(joiner: joiner, minLevel: minLevel);
3437-
assert(() {
3438-
_debugActiveLayout = debugPreviousActiveLayout;
3439-
return true;
3440-
}());
3441-
return result;
3439+
return _withDebugActiveLayoutCleared(() => super.toStringShallow(joiner: joiner, minLevel: minLevel));
34423440
}
34433441

34443442
@protected

0 commit comments

Comments
 (0)