Skip to content

Commit

Permalink
handlesBackButton
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Dec 27, 2023
1 parent 5d649ed commit f75866a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions example/lib/src/feature/shop/widget/basket_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class BasketTab extends StatelessWidget {
@override
Widget build(BuildContext context) => BucketNavigator(
bucket: '${ShopTabsEnum.basket}-tab',
// Handles back button only if the current route is the basket screen
handlesBackButton: () =>
Octopus.instance.state.arguments['shop'] == 'basket',
);
}

Expand Down
3 changes: 3 additions & 0 deletions example/lib/src/feature/shop/widget/catalog_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CatalogTab extends StatelessWidget {
@override
Widget build(BuildContext context) => BucketNavigator(
bucket: '${ShopTabsEnum.catalog}-tab',
// Handles back button only if the current route is the catalog screen
handlesBackButton: () =>
Octopus.instance.state.arguments['shop'] == 'catalog',
);
}

Expand Down
6 changes: 2 additions & 4 deletions lib/src/controller/information_provider_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,8 @@ final class OctopusInformationProvider$JS extends OctopusInformationProvider {

void pushRoute(RouteInformation routeInformation) {
if (_value == routeInformation) return;
if (routeInformation.uri.path.startsWith('/')) return;
final uri = routeInformation.uri;
_value = RouteInformation(uri: uri, state: null);
_valueInEngine = OctopusInformationProvider.kEmptyRouteInformation;
//if (routeInformation.uri.path.startsWith('/')) return;
_value = _valueInEngine = routeInformation;
notifyListeners();
}

Expand Down
6 changes: 2 additions & 4 deletions lib/src/controller/information_provider_vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,8 @@ final class OctopusInformationProvider$VM extends OctopusInformationProvider {

void pushRoute(RouteInformation routeInformation) {
if (_value == routeInformation) return;
if (routeInformation.uri.path.startsWith('/')) return;
final uri = routeInformation.uri;
_value = RouteInformation(uri: uri, state: null);
_valueInEngine = OctopusInformationProvider.kEmptyRouteInformation;
//if (routeInformation.uri.path.startsWith('/')) return;
_value = _valueInEngine = routeInformation;
notifyListeners();
}

Expand Down
14 changes: 13 additions & 1 deletion lib/src/widget/bucket_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import 'package:octopus/src/widget/route_context.dart';
///
/// The [bucket] unique identifier is used to identify the navigator
/// within the all application.
/// The [handlesBackButton] parameter is used to decide whether this navigator
/// should handle back button presses.
/// The [transitionDelegate] parameter is used to customize the transition
/// animation.
/// The [observers] parameter is used to observe the navigation events.
Expand All @@ -25,6 +27,7 @@ class BucketNavigator extends StatefulWidget {
/// {@macro bucket_navigator}
const BucketNavigator({
required this.bucket,
this.handlesBackButton,
this.transitionDelegate,
this.observers = const <NavigatorObserver>[],
this.restorationScopeId,
Expand All @@ -34,6 +37,13 @@ class BucketNavigator extends StatefulWidget {
/// The unique identifier of the navigator.
final String bucket;

/// The [handlesBackButton] parameter is used to decide whether this navigator
/// should handle back button presses.
/// Usefull when you want to handle back button only when the current screen
/// is in focus now.
/// By default, the value is `true` if the navigator has more than one page.
final bool Function()? handlesBackButton;

/// The delegate that decides how the route transition animation should
/// look like.
final TransitionDelegate<Object?>? transitionDelegate;
Expand Down Expand Up @@ -141,6 +151,9 @@ class _BucketNavigatorState extends State<BucketNavigator>
@override
Future<bool> _onBackButtonPressed() {
if (!mounted) return Future<bool>.value(false);
final handlesBackButton = widget.handlesBackButton;
if (handlesBackButton != null && !handlesBackButton())
return Future<bool>.value(false);
final node = _node;
if (node == null) return Future<bool>.value(false);
if (node.children.length < 2) return Future<bool>.value(false);
Expand Down Expand Up @@ -174,7 +187,6 @@ mixin _BackButtonBucketNavigatorStateMixin on State<BucketNavigator> {

@override
void initState() {
// TODO(plugfox): check priority for nested navigators
dispatcher?.removeCallback(_onBackButtonPressed);
final rootBackDispatcher = context.octopus.config.backButtonDispatcher;
dispatcher = rootBackDispatcher.createChildBackButtonDispatcher()
Expand Down
1 change: 1 addition & 0 deletions lib/src/widget/tabs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO(plugfox): implement tabs with internal navigators

0 comments on commit f75866a

Please sign in to comment.