[Material] Update TabController to support dynamic Tabs#30884
[Material] Update TabController to support dynamic Tabs#30884johnsonmh merged 15 commits intoflutter:masterfrom
Conversation
|
This looks like a good change: DefaultTabController.didUpdateWidget.didUpdateWidget definitely should DTRT when rebuilt with a new length. I'd prefer to not make TabController.copyWith public. It introduces a problem with the lifetime the TabController's AnimationController, since TabController.dispose() will now apply to the original and all copies. Should be easy enough to deal with that in this one case, but I don't think we want to face the general problem (at least not yet). |
rami-a
left a comment
There was a problem hiding this comment.
LGTM, just small comments
HansMuller
left a comment
There was a problem hiding this comment.
Looks good with a few cleanup suggestions.
HansMuller
left a comment
There was a problem hiding this comment.
LGTM with one small change.
…)" This reverts commit 5412ef0.
|
Can you please merge this to the |
|
Thanks @johnsonmh for your work on this! Can we know ETA for releasing this? |
|
This will be great to have it in the very nearest future, stuck with this problem today :-/ @johnsonmh any news on that?.. |
|
Hi all! Glad this work is helpful to people. This PR was merged sucessfully into |
|
@johnsonmh Hi! Thanks for prompt feedback! Just changes to master and realized that this does not help me much, because I use TabController, not the DefaultTabController as I need to know the index of the selected Tab on screen. Looks like your fix does not work for TabController (but it works for default one) :-/ Any ideas on that? |
|
@fellow7000 Sorry, I don't know how you would make this work with a regular If all you need is the index of the currently selected tab, you could do that pretty easily with a Something like: int _selectedIndex = 0;
...
DefaultTabController(
length: length,
child: TabBar(
tabs: _tabs,
onTap: (i) => setState(() => _selectedIndex = i),
),
), |
Description
Because
DefaultTabControllercreates itsTabControlleronce (ininitState), it is currently impossible to:TabBarand hot reload the changes.This change adds an override to
didUpdateWidgetinDefaultTabController, it will detect updates toDefaultTabController.lengthand update theDefaultTabController.controller accordingly.After the changes here, we can now do:

Related Issues
Tests
I added the following tests:
TabBarwith three tabs, update it to have two tabs, then one tab, then two tabs again. App should never crash andDefaultTabControllershould be updated accordingly.TabBar, it renders with the proper color (regression test for When there is only one tab, it's not shown selected #15008).Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Does your PR require Flutter developers to manually update their apps to accommodate your change?
See: https://groups.google.com/forum/#!topic/flutter-announce/SfLv0x5zDQ0