Skip to content

Commit

Permalink
feat: add navigate back to command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Aug 6, 2022
1 parent 0b00b15 commit 35fd9a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 14 additions & 3 deletions lib/navigation/commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class NavigationDirections {
}

class NavigateToCommand extends NavigationCommand {
NavigateToCommand({required this.directions});

NavigateToCommand.named(String named)
: this(
directions: NavigationDirections(
routeBuilder: () => named,
),
);

NavigateToCommand({required this.directions});

final NavigationDirections directions;

@override
Expand All @@ -44,4 +44,15 @@ class NavigateBackCommand extends NavigationCommand {
}
}

class NavigateBackToCommand extends NavigationCommand {}
class NavigateBackToCommand extends NavigationCommand {
NavigateBackToCommand({required this.directions});

NavigateBackToCommand.named(String named)
: this(
directions: NavigationDirections(
routeBuilder: () => named,
),
);

final NavigationDirections directions;
}
13 changes: 11 additions & 2 deletions lib/navigation/event_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ class NavigationCommandHandler extends TypedEventHandler<NavigationCommand> {
@override
//ignore: avoid_renaming_method_parameters
void handle(NavigationCommand command) {
if (command is NavigateBackCommand) {
if (command is NavigateBackCommand || command is NavigateBackToCommand) {
if (_canPop()) {
navigator.back();
Buzz.fire(OnNavigatedBackEvent());
return;
}

final fallback = command.preferredBackDefault ?? backDefault;
String fallback = backDefault;
if (command is NavigateBackCommand) {
//TODO: Watch for duplicated logic. Improve method design.
fallback = command.preferredBackDefault ?? backDefault;
}

if (command is NavigateBackToCommand) {
fallback = command.directions.routeBuilder();
}

navigator.offAndToNamed(fallback);
Buzz.fire(OnNavigatedBackEvent(fallbackPath: fallback));
}
Expand Down

0 comments on commit 35fd9a8

Please sign in to comment.