Introduce debugWithActiveLayoutCleared to avoid duplicated code (#114003)

This commit is contained in:
fzyzcjy 2022-11-02 06:17:11 +08:00 committed by GitHub
parent fb9065fe00
commit e37ab48bca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1591,6 +1591,26 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
static RenderObject? get debugActiveLayout => _debugActiveLayout;
static RenderObject? _debugActiveLayout;
/// Set [debugActiveLayout] to null when [inner] callback is called.
/// This is useful when you have to temporarily clear that variable to
/// disable some false-positive checks, such as when computing toStringDeep
/// or using custom trees.
@pragma('vm:prefer-inline')
static T _withDebugActiveLayoutCleared<T>(T Function() inner) {
RenderObject? debugPreviousActiveLayout;
assert(() {
debugPreviousActiveLayout = _debugActiveLayout;
_debugActiveLayout = null;
return true;
}());
final T result = inner();
assert(() {
_debugActiveLayout = debugPreviousActiveLayout;
return true;
}());
return result;
}
/// Whether the parent render object is permitted to use this render object's
/// size.
///
@ -3399,22 +3419,11 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
String? prefixOtherLines = '',
DiagnosticLevel minLevel = DiagnosticLevel.debug,
}) {
RenderObject? debugPreviousActiveLayout;
assert(() {
debugPreviousActiveLayout = _debugActiveLayout;
_debugActiveLayout = null;
return true;
}());
final String result = super.toStringDeep(
prefixLineOne: prefixLineOne,
prefixOtherLines: prefixOtherLines,
minLevel: minLevel,
);
assert(() {
_debugActiveLayout = debugPreviousActiveLayout;
return true;
}());
return result;
return _withDebugActiveLayoutCleared(() => super.toStringDeep(
prefixLineOne: prefixLineOne,
prefixOtherLines: prefixOtherLines,
minLevel: minLevel,
));
}
/// Returns a one-line detailed description of the render object.
@ -3427,18 +3436,7 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
String joiner = ', ',
DiagnosticLevel minLevel = DiagnosticLevel.debug,
}) {
RenderObject? debugPreviousActiveLayout;
assert(() {
debugPreviousActiveLayout = _debugActiveLayout;
_debugActiveLayout = null;
return true;
}());
final String result = super.toStringShallow(joiner: joiner, minLevel: minLevel);
assert(() {
_debugActiveLayout = debugPreviousActiveLayout;
return true;
}());
return result;
return _withDebugActiveLayoutCleared(() => super.toStringShallow(joiner: joiner, minLevel: minLevel));
}
@protected