Skip to content

AutomaticKeepAliveClientMixin does not work in DefaultTabController With 3 tabs #25807

@cqhchan

Description

@cqhchan

Steps to Reproduce

  1. Use the code Template found here -> https://flutter.io/docs/cookbook/design/tabs
class TabBarDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: [
                Tab(icon: Icon(Icons.directions_car)),
                Tab(icon: Icon(Icons.directions_transit)),
                Tab(icon: Icon(Icons.directions_bike)),
              ],
            ),
            title: Text('Tabs Demo'),
          ),
          body: TabBarView(
            children: [
              Page1(),
              Page2(),
              Page3(),
            ],
          ),
        ),
      ),
    );
  }
}
  1. Create 3 stateful widget with AutomaticKeepAliveClientMixin set to True and super.build(context) under build before the return statement.

  2. Log the initState() function for each page.
    e.g

class Page1 extends StatefulWidget{
  
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState

    return _Page1State();
  }

class _Page1State extends State<Page1> with AutomaticKeepAliveClientMixin{ 
  @override
    void initState() {
      super.initState();
      print("Page 1 init");
    }
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Text("Page 1");
  }
  @override
  bool get wantKeepAlive => true;

}
  1. When moving from tab 1->2->3 or reverse, it only calls initState() on each Page once as intended.
    When moving from tab 1 -> 3, it calls initState on tab 1 and 2 again.
    When moving from tab 3 -> 1, it initStates again for tab 2 and 3.

AutomaticKeepAliveClientMixin doesn't seem to work when moving across tabs that are not next to each other.

Logs

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v1.0.0, on Mac OS X 10.14 18A391, locale en-SG)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
[✓] Android Studio (version 3.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.30.1)
[✓] Connected device (1 available)

• No issues found!

Metadata

Metadata

Assignees

Labels

c: API breakBackwards-incompatible API changesframeworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions