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

PersistentTabView add onRedirect callback; #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

boomcx
Copy link

@boomcx boomcx commented May 8, 2024

I found there was no way to handle the jump intercept, when I used PersistentTabView construction method. Sometimes we don't want to use go_router,although it's good enough.
So, I want to add something.

  //  ----> example/lib/main.dart

  @override
  Widget build(BuildContext context) => PersistentTabView(
        controller: controller,
        tabs: _tabs(),
        navBarBuilder: (navBarConfig) => Style1BottomNavBar(
          navBarConfig: navBarConfig,
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            controller.jumpToTab(2);
          },
          child: const Text('Jump'),
        ),
        onRedirect: (index) async {
          if (index == 2) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text('Intercept when jumping to $index'),
              ),
            );
            return true;
          }
          return null;
        },
      );

@jb3rndt jb3rndt changed the title - PersistentTabView add onRedirect callback; PersistentTabView add onRedirect callback; May 26, 2024
@jb3rndt
Copy link
Owner

jb3rndt commented May 26, 2024

Hi, I guess you want to prevent switching to a tab in certain cases? Can you elaborate the use case of that so I get a better understanding of it? It does seem unusual and you could implement it on your own using a custom nav bar widget. Thats why I'm unsure if that should be included in the package :)

@boomcx
Copy link
Author

boomcx commented Jun 19, 2024

I am sorry for the late reply.
When using the normal constructor, the component cannot catch the button interaction interception event, and the source code only has the changed back.
If this PR is unnecessary, please ignore it.

CleanShot.2024-06-19.at.10.44.13.mp4

@boomcx boomcx closed this Jun 19, 2024
@boomcx boomcx reopened this Jun 19, 2024
@jb3rndt
Copy link
Owner

jb3rndt commented Jun 30, 2024

I believe I understand what you're aiming to achieve :) However, I think this approach might involve more work than anticipated. Here are my initial thoughts:

For naming and using the callback, I prefer canSwitchToTab(int index) over onRedirect because it clearly describes the purpose and assigns canSwitchToTab a single responsibility: determining if a specific tab can be accessed or not. Thus, canSwitchToTab should not have any side effects (such as showing a snackbar). This allows canSwitchToTab to visually indicate to the user that a tab is disabled without side effects when canSwitchToTab is invoked (which would occur at every build). The snackbar in your example should be managed by the widget instead. The widget should check canSwitchToTab when pressed and act based on the returned value.

Additionally, we need to consider what happens when the user uses the Android back button. By default, the controller iterates through the tab history backwards and might encounter a tab that should not be switched to. In that case, we should probably skip this item and immediately move to the next one (if available).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants