Skip to content

Using NestedScrollView, TabBarView and AutomaticKeepAliveClientMixin will scroll other scroll views in the tab. #36419

@najeira

Description

@najeira

Steps to Reproduce

  1. Change tab to Tab 2
  2. Scroll inner list in Tab 2
  3. Change tab to Tab 1
  4. Inner list in Tab 1 has also scrolled

If we switch tabs, States outside the screen will be disposed.
In many cases, loading data is a heavy process, so I want to keep States.
I am going to use AutomaticKeepAliveClientMixin for that.

However, with this combination of Widgets,
the scroll operation also affects the scroll view of other tabs.

  • Without NestedScrollView, this does not occur
  • Without AutomaticKeepAliveClientMixin, this does not occur
  • When wantKeepAlive is false, this does not occur
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: MyApp()));

const _kTabs = ["Tab 1", "Tab 2"];

class MyApp extends StatefulWidget {
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  ScrollController scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    final Widget tabBar = TabBar(
        tabs: _kTabs.map<Widget>((String name) => Tab(text: name)).toList());

    return Scaffold(
      body: DefaultTabController(
        length: _kTabs.length,
        child: NestedScrollView(
          controller: scrollController,
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                title: Text("Title"),
                bottom: tabBar,
                pinned: true,
                forceElevated: innerBoxIsScrolled,
              ),
            ];
          },
          body: TabBarView(
            children: _kTabs.map((String name) {
              return Container(
                key: PageStorageKey(name),
                child: MyListView(),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
}

class MyListView extends StatefulWidget {
  @override
  _MyListViewState createState() => _MyListViewState();
}

class _MyListViewState extends State<MyListView> with AutomaticKeepAliveClientMixin {
  @override
  final bool wantKeepAlive = true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          title: Text('Item $index'),
        );
      },
    );
  }
}

Logs

[✓] Flutter (Channel v1.7.8-hotfixes, v1.7.8+hotfix.3, on Mac OS X 10.14.4 18E226, locale ja-JP)
    • Flutter version 1.7.8+hotfix.3 at /Applications/flutter
    • Framework revision b712a172f9 (8 days ago), 2019-07-09 13:14:38 -0700
    • Engine revision 54ad777fd2
    • Dart version 2.4.0


[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/najeira/Library/Android/sdk
    • Android NDK at /Users/najeira/Library/Android/sdk/ndk-bundle
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /Users/najeira/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/najeira/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 10.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.2, Build version 10E125
    • CocoaPods version 1.7.4

[✓] iOS tools - develop for iOS devices
    • ios-deploy 1.9.4

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 3.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 34.0.1
    • Dart plugin version 182.5215
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[✓] IntelliJ IDEA Ultimate Edition (version 2018.1)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 31.1.1
    • Dart plugin version 181.4203.498

[✓] VS Code (version 1.35.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.0.2

[✓] Connected device (5 available)
    • SC 02H               • 145faa87                                 • android-arm64  • Android 8.0.0 (API 26)
    • HW 01K               • LPF0218529000628                         • android-arm64  • Android 8.1.0 (API 27)
    • TetsuhiroのiPod touch • 8669364bb2135049086983a6bcc1f2db7cdd255e • ios            • iOS 12.3.1
    • macOS                • macOS                                    • darwin-x64     • Mac OS X 10.14.4 18E226
    • web                  • web                                      • web-javascript • Google Chrome 75.0.3770.142

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listcustomer: crowdAffects or could affect many people, though not necessarily a specific customer.f: material designflutter/packages/flutter/material repository.f: scrollingViewports, list views, slivers, etc.found in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: duplicateIssue is closed as a duplicate of an existing issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions