Skip to content

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

Closed

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

Activity

  1. added
    p: material_uimaterial_ui package in flutter/packages
    f: scrollingViewports, list views, slivers, etc.
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jul 18, 2019
  2. curls commented on Aug 5, 2019

    @curls

    Hi,I have the same problem, how to fixed?

  3. najeira commented on Aug 5, 2019

    @najeira
    ContributorAuthor

    I use PageStorage instead of AutomaticKeepAliveClientMixin to avoid this problem.

  4. KorbinianMossandl commented on Aug 29, 2019

    @KorbinianMossandl

    @HansMuller i would like to bump this. Using PageStorage only works to some extend.
    For example if you have a tab with an ExpansionPanelList.radio, it is not possible to save the currently expanded panel sate without AutomaticKeepAliveClientMixin or a custom rewrite of the whole widget just to be able to use PageStorage.

  5. zmtzawqlp commented on Dec 2, 2019

    @zmtzawqlp
    Contributor

    i have done some things to solve this thing, may be you have better solution 。please check it when you have the time.
    i notice innerscrollcontrol is sync the cache scrollpostion, that the problem here.
    may be we should only Focus on the active one
    https://github.com/fluttercandies/extended_nested_scroll_view

  6. added this to the milestone on Jan 6, 2020
  7. VladyslavBondarenko commented on Apr 3, 2020

    @VladyslavBondarenko

    Reproduced with dev v1.16.4-pre.83

  8. marcusgregory commented on Apr 6, 2020

    @marcusgregory

    I also encountered this problem, did anyone find a solution?

  9. iapicca commented on May 13, 2020

    @iapicca

    the issue reproduce with the latest master (1.19)
    #57071 (comment)

  10. lyf571321556 commented on May 14, 2020

    @lyf571321556

    @iapicca This question has been raised for a long time. Are there any restoration plans in the near future

  11. added
    P2Important issues not at the top of the work list
    on May 29, 2020
  12. 57 remaining items

  13. iapicca commented on Dec 14, 2022

    @iapicca

    I am a new person. I can't believe the problems have persisted for 3 years. =]]

    @quangtuyen1993 imho this issue shouldn't be open at all, just use a key and everything works just fine (code sample)

  14. maRci002 commented on Dec 14, 2022

    @maRci002
    Contributor

    imho this issue shouldn't be open at all, just use a key and everything works just fine (code sample)

    This solution doesn't benefit from NestedScrollView since Header won't be scrolled at all.

  15. maheshj01 commented on Jan 14, 2023

    @maheshj01
    Member

    Reproducible on stable 3.3 and master 3.7

    code sample
    import 'package:flutter/material.dart';
    
    void main() => runApp(const MaterialApp(home: MyApp()));
    
    const _kTabs = ["Tab 1", "Tab 2"];
    
    class MyApp extends StatefulWidget {
      const MyApp({super.key});
    
      @override
      MyAppState createState() => MyAppState();
    }
    
    class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
      ScrollController scrollController = ScrollController();
    
      @override
      Widget build(BuildContext context) {
        final 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: const Text("Title"),
                    bottom: tabBar,
                    pinned: true,
                    forceElevated: innerBoxIsScrolled,
                  ),
                ];
              },
              body: TabBarView(
                children: _kTabs.map((String name) {
                  return Container(
                    key: PageStorageKey(name),
                    child: const MyListView(),
                  );
                }).toList(),
              ),
            ),
          ),
        );
      }
    }
    
    class MyListView extends StatefulWidget {
      const MyListView({super.key});
    
      @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
    Launching lib/main.dart on Chrome in debug mode...
    This app is linked to the debug service: ws://127.0.0.1:49305/ifeyLlT9fbI=/ws
    Debug service listening on ws://127.0.0.1:49305/ifeyLlT9fbI=/ws
    💪 Running with sound null safety 💪
    Connecting to VM Service at ws://127.0.0.1:49305/ifeyLlT9fbI=/ws
    ══╡ EXCEPTION CAUGHT BY ANIMATION LIBRARY ╞═════════════════════════════════════════════════════════
    The following assertion was thrown while notifying status listeners for AnimationController:
    The provided ScrollController is currently attached to more than one ScrollPosition.
    The Scrollbar requires a single ScrollPosition in order to be painted.
    When the scrollbar is interactive, the associated ScrollController must only have one ScrollPosition
    attached.The provided ScrollController must be unique to one ScrollView widget.
    When the exception was thrown, this was the stack:
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:49      throw_
    packages/flutter/src/widgets/scrollbar.dart 1630:9                                <fn>
    packages/flutter/src/widgets/scrollbar.dart 1654:14                               [_debugCheckHasValidScrollPosition]
    packages/flutter/src/widgets/scrollbar.dart 1564:14                               [_validateInteractions]
    packages/flutter/src/animation/listener_helpers.dart 240:19                       notifyStatusListeners
    packages/flutter/src/animation/animation_controller.dart 822:7                    [_checkStatusChanged]
    packages/flutter/src/animation/animation_controller.dart 756:5                    [_startSimulation]
    packages/flutter/src/animation/animation_controller.dart 619:12                   [_animateToInternal]
    packages/flutter/src/animation/animation_controller.dart 467:12                   forward
    packages/flutter/src/widgets/scrollbar.dart 1973:37                               [_handleScrollNotification]
    packages/flutter/src/widgets/notification_listener.dart 130:22                    onNotification
    packages/flutter/src/widgets/framework.dart 3194:18                               dispatchNotification
    packages/flutter/src/widgets/framework.dart 3197:13                               dispatchNotification
    packages/flutter/src/widgets/framework.dart 4558:24                               dispatchNotification
    packages/flutter/src/widgets/notification_listener.dart 60:13                     dispatch
    packages/flutter/src/widgets/scroll_activity.dart 92:90                           dispatchScrollUpdateNotification
    packages/flutter/src/widgets/scroll_position.dart 917:5                           didUpdateScrollPositionBy
    packages/flutter/src/widgets/nested_scroll_view.dart 1326:5                       applyClampedPointerSignalUpdate
    packages/flutter/src/widgets/nested_scroll_view.dart 953:21                       pointerScroll
    packages/flutter/src/widgets/nested_scroll_view.dart 1427:24                      pointerScroll
    packages/flutter/src/widgets/scrollable.dart 799:16                               [_handlePointerScroll]
    packages/flutter/src/gestures/pointer_signal_resolver.dart 90:7                   resolve
    packages/flutter/src/gestures/binding.dart 466:29                                 handleEvent
    packages/flutter/src/gestures/binding.dart 440:14                                 dispatchEvent
    packages/flutter/src/rendering/binding.dart 336:11                                dispatchEvent
    packages/flutter/src/gestures/binding.dart 395:7                                  [_handlePointerEventImmediately]
    packages/flutter/src/gestures/binding.dart 357:5                                  handlePointerEvent
    packages/flutter/src/gestures/binding.dart 314:7                                  [_flushPointerEventQueue]
    packages/flutter/src/gestures/binding.dart 295:7                                  [_handlePointerDataPacket]
    lib/_engine/engine/platform_dispatcher.dart 1207:13                               invoke1
    lib/_engine/engine/platform_dispatcher.dart 244:5                                 invokeOnPointerDataPacket
    lib/_engine/engine/pointer_binding.dart 167:39                                    [_onPointerData]
    lib/_engine/engine/pointer_binding.dart 480:14                                    [_handleWheelEvent]
    lib/_engine/engine/pointer_binding.dart 799:7                                     <fn>
    lib/_engine/engine/pointer_binding.dart 470:43                                    <fn>
    lib/_engine/engine/pointer_binding.dart 223:80                                    <fn>
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 367:37  _checkAndCall
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 372:39  dcall
    The AnimationController notifying status listeners was:
      AnimationController#a517b(▶ 0.000)
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
    Application finished.
    Exited
    flutter doctor -v (mac)
    [✓] Flutter (Channel master, 3.7.0-13.0.pre.131, on macOS 13.1 22C65 darwin-arm64, locale en-IN)
        • Flutter version 3.7.0-13.0.pre.131 on channel master at /Users/mahesh/Development/flutter_master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 0196e6050b (2 days ago), 2022-12-31 11:10:23 -0500
        • Engine revision 932591ec04
        • Dart version 3.0.0 (build 3.0.0-76.0.dev)
        • DevTools version 2.20.0
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks
          and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • 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 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.74.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.56.0
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 13.1 22C65 darwin-arm64
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 108.0.5359.124
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
        
    • No issues found!
    
    [✓] Flutter (Channel stable, 3.3.9, on macOS 12.6 21G115 darwin-arm, locale en-IN)
        • Flutter version 3.3.9 on channel stable at /Users/mahesh/Development/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision b8f7f1f986 (24 hours ago), 2022-11-23 06:43:51 +0900
        • Engine revision 8f2221fbef
        • Dart version 2.18.5
        • DevTools version 2.15.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • 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 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.70.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.53.20221101
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 107.0.5304.110
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    

    Looks related to #62833

  16. maRci002 commented on Jan 30, 2023

    @maRci002
    Contributor

    Upvote #102003 issue to get priority.
    I asked to add this issue also for NestedScrollView rewrite.

  17. maRci002 commented on Feb 5, 2023

    @maRci002
    Contributor

    Here is a workaround which is based on #118713 (comment)

    Check out DartPad (enabled touch events on desktop to simulate scroll by touch)

    nestedscrollview_with_tabbar.mp4
  18. DexEze commented on May 26, 2023

    @DexEze

    This issue is still here today in 3.10

  19. Piinks commented on May 30, 2023

    @Piinks
    Contributor

    I think this is another case of #40740. I have been scanning the issue tracker looking to consolidate all of these so we can see about a proper solution. I am going to close this since #40740 has the most upvotes, let's follow up there. (I am adding a list there of issues like these to be linked)

    Thanks for providing test cases, updates and work arounds! :)

  20. github-actions commented on Jun 19, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  21. locked as resolved and limited conversation to collaborators on Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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: 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 onp: material_uimaterial_ui package in flutter/packagesr: 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