Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions packages/flame/lib/src/components/core/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -587,19 +587,16 @@ class Component {
/// cleans them up afterwards.
@protected
void renderChild(Canvas canvas, Component child) {
int? originalLength;
final hasContext = _renderContexts.isNotEmpty;
if (hasContext) {
originalLength = child._renderContexts.length;
child._renderContexts.addAll(_renderContexts);
final contexts = _renderContexts;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also be the same

final renderContexts = _renderContexts ?? const [];

so you don't have to null check or fallback below?

if (contexts == null || contexts.isEmpty) {
child.renderTree(canvas);
return;
}
final childContexts = child._renderContexts ??= [];
final originalLength = childContexts.length;
childContexts.addAll(contexts);
child.renderTree(canvas);
if (hasContext) {
child._renderContexts.removeRange(
originalLength!,
child._renderContexts.length,
);
}
childContexts.removeRange(originalLength, childContexts.length);
}

/// Called once after all children have been rendered in [renderTree].
Expand All @@ -612,7 +609,7 @@ class Component {
void renderTree(Canvas canvas) {
final context = renderContext;
if (context != null) {
_renderContexts.add(context);
(_renderContexts ??= []).add(context);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do final renderContexts = _renderContexts ??= []; at the top to avoid the ! later?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also wanna do the const [] trick?

}

render(canvas);
Expand All @@ -630,7 +627,7 @@ class Component {
}

if (context != null) {
_renderContexts.removeLast();
_renderContexts!.removeLast();
}
}

Expand Down Expand Up @@ -1153,14 +1150,16 @@ class Component {

//#region Context

final QueueList<ComponentRenderContext> _renderContexts = QueueList();
/// The stack of render contexts inherited from ancestors during the render
/// pass. Created lazily: most components never provide or receive one.
List<ComponentRenderContext>? _renderContexts;

/// Override this method if you want your component to provide a custom
/// render context to all its children (recursively).
ComponentRenderContext? get renderContext => null;

T? findRenderContext<T extends ComponentRenderContext>() {
return _renderContexts.whereType<T>().lastOrNull;
return _renderContexts?.whereType<T>().lastOrNull;
}

//#endregion
Expand Down Expand Up @@ -1191,8 +1190,9 @@ class Component {
/// The color that the debug output should be rendered with.
Color debugColor = const Color(0xFFFF00FF);

final ValueCache<Paint> _debugPaintCache = ValueCache<Paint>();
final ValueCache<TextPaint> _debugTextPaintCache = ValueCache<TextPaint>();
late final ValueCache<Paint> _debugPaintCache = ValueCache<Paint>();
late final ValueCache<TextPaint> _debugTextPaintCache =
ValueCache<TextPaint>();

/// The [debugColor] represented as a [Paint] object.
Paint get debugPaint {
Expand Down
Loading