Open RB-93 opened 2 months ago
Hi, thank you for opening this issue. You are right, dynamically chaning the number of tabs is not supported as of now. I assumed that a user expects a persistent bottom navigation bar to be static and not change while the app is running. Thus, changing the content of the navigation bar results in bad UX (see official Material 3 docs). Do you disagree or have a justifying use case to allow that?
I also need the ability to dynamically change the number of tabs in my app.
I assumed that a user expects a persistent bottom navigation bar to be static and not change while the app is running.
It was my expectation that the tab bar should indeed be persistent, i.e. not need rebuilding from scratch ever, while also supporting dynamically changing numbers of tabs.
It is standard practice in Flutter to allow different numbers of items in any sort of container between different calls to build
, and the PersistentTabView
API basically looks like the ListView.builder
API, in other words, requiring the user to specify the number of items, and the content of each item, each time build
is called.
In ListView
, if items are deleted or inserted into the list, the widgets that are rendered may be structurally identical, so you may need to use a ValueKey
for the updates to actually be detected and rendered. Nevertheless, the builder doesn't care about how many items were in the list last time build
was called.
With PersistentBottomNavBarV2
, a lot of things assume the number of tabs never changes, so I got all sorts of errors (mostly !
used on a null value, but I think also an array index out of bounds issue) when I tried changing the number of tabs dynamically. I was starting to work through all these issues to fix them, and instead, I ended up wrapping the whole PersistentTabView
in a ValueListenableBuilder
, also assigning a new unique key in PersistentTabView
every time the ValueListenableBuilder
was rebuilt, to force the entire PersistentTabView
to be torn down and rebuilt. This is inefficient, but it's the only solution to the problem I could come up with.
Yes it will indeed need quite some amount of work to do that, so that will probably take some time, sorry... I'll try looking into fixing that. But to be honest I am still concerned about the UX aspect, so personally I would advise against changing the tabs dynamically.
UX is always contextual. In my app, continuing to show a tab for a feature that the user just manually disabled doesn't make any sense.
Version
5.2.1
Flutter Doctor Output
What platforms are you seeing the problem on?
Android
What happened?
On a fresh run (reinstall with data clear) with this pkg build, It shows red screen with "cannot add to a fixed-length list" error.
My app use case: I have maintained bottom bar widget and kept a toggling boolean to one of the tab option to hide or show it on UI which is listening values using Provider state management.
I tried to set growable flag to true in addAll() method (PFA 1)
and here too (PFA 2)
But after this it throws this exception could be because focusNodes index doesn't get updated with provider updated values of tablist.
════════ Exception caught by widgets library ═══════════════════════════════════ RangeError (index): Invalid value: Not in inclusive range 0..1: 2 The relevant error-causing widget was: ════════════════════════════════════════════════════════════════════════════════
The list of tab options should support 'growable' list functionality, so that Tab list can handled dynamically in the code.
Steps to reproduce
Run provided code with where any one option should be handled with a boolean flag in the Firebase Realtime database listened using Provider state management.
Code to reproduce the problem
Relevant log output
Screenshots
(PFA 1)![image](https://github.com/jb3rndt/PersistentBottomNavBarV2/assets/29726705/6e7cc661-34fa-4afa-944f-8200540ac05b)
(PFA 2)![image](https://github.com/jb3rndt/PersistentBottomNavBarV2/assets/29726705/d09e39ca-d499-4df0-a8e3-dd660c715965)