Skip to content

TabBarView is out of sync with the TabBar when tabs are added dynamically. #170090

Open
@evya-dev

Description

@evya-dev

Steps to reproduce

We want to create tabs in the app on the fly (by pressing a button).

Create a widget with an AppBar with TabBar and TabBarView. Create a TabController and assign it to both the TabBar and TabBarView.

In the builder we add a button which has the OnPressed generating a new tab. Since the TabController's length is final, we create a new TabController with the length of a new tab added to the children of TabBar and TabBarView.

Expected results

A new tab is created on the button press and onTap scrolls to the newly added child widget of the TabBarView.

Actual results

What ends up happening is that the tabbar is highlighted correctly but the contents of the newly added tab in TabBarView is not rendered and remains on the current child of the TabBarView. If you tab back to the first tab and then tap on the newly created tab then both tabbar and tabbarview are rendered correctly and are also in sync.

If we set the animationDuration to 0 seconds then the tapping works correctly. Any duration greater than 0 results in the tabbar and tabbarview going out of sync initially but sync up after tapping back and forth at least once.

Code sample

Code sample
class TestWidget extends StatefulWidget {

  @override
  State<TestWidget> createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> with TickerProviderStateMixin {
  late TabController _tabController;
  final _tabs = <Tab>[];
  final _tabViews = <Widget>[];

  @override
  void initState() {
    super.initState();

    _tabController = TabController(length: 1, vsync: this);
    _tabs.add(Tab(text: "Tab 1",));
    _tabViews.add(TextButton(
        onPressed: () async {
          _tabViews.add(_buildTabContent('Content for Tab 2'));
          _tabController = TabController(animationDuration: Duration(microseconds: 1), length: 2, vsync: this);
          _tabs.add(Tab(text: "Tab 2",));
          setState(() { });

          _tabController.animateTo(1);
        },
        child: Text("Add Tab")
    ));
  }

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Version 10.0.26100.4061], locale en-CA) [435ms]
    • Flutter version 3.29.0 on channel stable at C:\Users\habib\Downloads\Software\flutter_windows_3.29.0-stable\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 35c388afb5 (4 months ago), 2025-02-10 12:48:41 -0800
    • Engine revision f73bfc4522
    • Dart version 3.7.0
    • DevTools version 2.42.2

[√] Windows Version (11 Pro 64-bit, 24H2, 2009) [3.8s]

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [3.3s]
    • Android SDK at C:\Users\habib\AppData\Local\Android\sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe) [128ms]
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[√] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.45) [127ms]
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.35826.135
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2024.2) [14ms]
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)

[√] Connected device (2 available) [243ms]
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.26100.4061]
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 136.0.3240.92

[√] Network resources [597ms]
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.found in release: 3.32Found to occur in 3.32found in release: 3.33Found to occur in 3.33frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions