Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Loading builder for Route #3113

Merged
merged 15 commits into from
Jul 24, 2024
Merged
Changes from 1 commit
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
25 changes: 23 additions & 2 deletions packages/flame/lib/src/components/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class Route extends PositionComponent
with ParentIsA<RouterComponent>, HasTimeScale {
Route(
Component Function()? builder, {
Component Function()? loadingBuilder,
this.transparent = false,
this.maintainState = true,
}) : _builder = builder,
_loadingBuilder = loadingBuilder,
_renderEffect = Decorator();

/// If true, then the route below this one will continue to be rendered when
Expand All @@ -53,6 +55,11 @@ class Route extends PositionComponent
/// in which case the user must override the [build] method.
final Component Function()? _builder;

/// The function that will be invoked that will build the loading page
/// component when this route first becomes active. Can be left `null`,
/// in which case it is not required.
spydon marked this conversation as resolved.
Show resolved Hide resolved
final Component Function()? _loadingBuilder;

/// This method is invoked when the route is pushed on top of the
/// [RouterComponent]'s stack.
///
Expand Down Expand Up @@ -118,17 +125,31 @@ class Route extends PositionComponent
/// also be added as a child component.
Component? _page;

/// The loadingpage that was built and is now owned by this route. This
/// loadingpage will also be added as a child component.
Component? _loadingpage;
PistonShot1 marked this conversation as resolved.
Show resolved Hide resolved

/// Additional visual effect that may be applied to the page during rendering.
final Decorator _renderEffect;

/// Invoked by the [RouterComponent] when this route is pushed to the top
/// of the navigation stack.
/// of the navigation stack
@internal
void didPush(Route? previousRoute) {
_page ??= build()..addToParent(this);
_page ??= build();
(_loadingBuilder != null) ? _addLoadingPage() : _page!.addToParent(this);
onPush(previousRoute);
}

/// Adds the [_loadingpage] to the parent and invoked by [didPush] , when
/// [_loadingBuilder] is specified
Future<void> _addLoadingPage() async {
_loadingpage ??= _loadingBuilder!()..addToParent(this);
await _page!.addToParent(this);
spydon marked this conversation as resolved.
Show resolved Hide resolved
await _page!.loaded;
_loadingpage!.removeFromParent();
}

/// Invoked by the [RouterComponent] when this route is popped off the top
/// of the navigation stack.
/// If [maintainState] is false, the page component rendered by this route
Expand Down
Loading