Skip to content

Scrollable not changing ScrollBehaviour #130793

Closed

Description

@Dampfwalze

Is there an existing issue for this?

Steps to reproduce

Create a Scrollable widget and change its scrollBehavior property at runtime (using state or hot reload).

Expected results

Changes to scrollBehavior should be applied without completely recreating the Scrollables state.

Actual results

The observed scroll behavior always stays the same.

Code sample

This version does not work:

class _NotWorkingState extends State<NotWorking> {
  bool _enable = true;

  @override
  Widget build(BuildContext context) => MaterialApp(
        home: Scaffold(
          body: Scrollable(
            scrollBehavior: const MaterialScrollBehavior().copyWith(dragDevices: {
              if (_enable) PointerDeviceKind.mouse,
            }),
            viewportBuilder: (context, position) => Viewport(
              offset: position,
              slivers: [
                for (int i = 0; i < 100; i++)
                  SliverToBoxAdapter(
                    child: Text("Item $i"),
                  ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(onPressed: () {
            setState(() {
              _enable = !_enable;
            });
          }),
        ),
      );
}

While this one does:

class _WorkingState extends State<Working> {
  bool _enable = true;

  @override
  Widget build(BuildContext context) => MaterialApp(
        scrollBehavior: const MaterialScrollBehavior().copyWith(dragDevices: {
          if (_enable) PointerDeviceKind.mouse,
        }),
        home: Scaffold(
          body: Scrollable(
            viewportBuilder: (context, position) => Viewport(
              offset: position,
              slivers: [
                for (int i = 0; i < 100; i++)
                  SliverToBoxAdapter(
                    child: Text("Item $i"),
                  ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(onPressed: () {
            setState(() {
              _enable = !_enable;
            });
          }),
        ),
      );
}

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.10.6, on Microsoft Windows [Version 10.0.19045.3208], locale de-DE)
    • Flutter version 3.10.6 on channel stable at C:\Users\...\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f468f3366c (6 days ago), 2023-07-12 15:19:05 -0700
    • Engine revision cdbeda788a
    • Dart version 3.0.6
    • DevTools version 2.23.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0) 
    • Android SDK at C:\Users\...\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java       
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)     
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Professional 2022 17.6.5) 
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional
    • Visual Studio Professional 2022 version 17.6.33829.357
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2020.3)
    • 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 11.0.10+0-b96-7249189)

[√] VS Code (version 1.80.1)
    • VS Code at C:\Users\...\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.68.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.3208]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 114.0.5735.199
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 114.0.1823.82

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

Activity

  1. Dampfwalze commented on Jul 18, 2023

    @Dampfwalze
    Author

    I did some research myself and concluded, that this is caused by these lines in ScrollableState.didUpdateWidget:

    if (_shouldUpdatePosition(oldWidget)) {
    _updatePosition();
    }

    because ScrollableState._shouldUpdatePosition does not handle changes to Scrollable.scrollBehaviorcorrectly. But due to:

    _configuration = widget.scrollBehavior ?? ScrollConfiguration.of(context);

    it works fine when Scrollable.scrollBehavior is null, since in this case ScollableState.didChangeDependencies will be called, when ScrollConfiguration changes, in which, ScrollableState._updatePosition will be called regardless:

    void didChangeDependencies() {
    _mediaQueryGestureSettings = MediaQuery.maybeGestureSettingsOf(context);
    _devicePixelRatio = MediaQuery.maybeDevicePixelRatioOf(context) ?? View.of(context).devicePixelRatio;
    _updatePosition();
    super.didChangeDependencies();
    }

    I found that ScrollBehavior has this convenient bool shouldNotify(covariant ScrollBehavior oldDelegate) method, which maybe can be used to improve ScrollableState._shouldUpdatePosition.

  2. added
    in triagePresently being triaged by the triage team
    on Jul 19, 2023
  3. huycozy commented on Jul 19, 2023

    @huycozy
    Member

    Thanks for your detailed report. I also can see the issue on master channel with behavior as descibed.

    flutter doctor -v (stable and master)
    [✓] Flutter (Channel stable, 3.10.6, on macOS 13.0.1 22A400 darwin-x64, locale en-VN)
        • Flutter version 3.10.6 on channel stable at /Users/huynq/Documents/GitHub/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision f468f3366c (5 hours ago), 2023-07-12 15:19:05 -0700
        • Engine revision cdbeda788a
        • Dart version 3.0.6
        • DevTools version 2.23.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 32.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14E222b
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.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 17.0.6+0-17.0.6b802.4-9586694)
    
    [✓] VS Code (version 1.80.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.68.0
    
    [✓] Connected device (3 available)
        • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64  • Android 11 (API 30)
        • macOS (desktop)  • macos            • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
        • Chrome (web)     • chrome           • web-javascript • Google Chrome 114.0.5735.198
    
    [✓] Network resources
        • All expected network resources are available.
    
    • No issues found!
    [!] Flutter (Channel master, 3.13.0-6.0.pre.30, on macOS 13.0.1 22A400 darwin-x64, locale en-VN)
        • Flutter version 3.13.0-6.0.pre.30 on channel master at /Users/huynq/Documents/GitHub/flutter_master
        ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 33d174d797 (46 minutes ago), 2023-07-18 22:36:21 -0400
        • Engine revision 39d60be72f
        • Dart version 3.1.0 (build 3.1.0-324.0.dev)
        • DevTools version 2.25.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 32.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 32.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14E222b
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.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 17.0.6+0-17.0.6b802.4-9586694)
    
    [✓] VS Code (version 1.80.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.68.0
    
    [✓] Connected device (3 available)
        • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64  • Android 11 (API 30)
        • macOS (desktop)  • macos            • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
        • Chrome (web)     • chrome           • web-javascript • Google Chrome 114.0.5735.198
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
  4. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    f: scrollingViewports, list views, slivers, etc.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Jul 19, 2023
  5. added
    P2Important issues not at the top of the work list
    on Jul 21, 2023
  6. added a commit that references this issue on Aug 17, 2023
    f1d04a4
  7. added
    r: fixedIssue is closed as already fixed in a newer version
    on Aug 18, 2023
  8. github-actions commented on Sep 1, 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.

  9. locked as resolved and limited conversation to collaborators on Sep 1, 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 listf: scrollingViewports, list views, slivers, etc.found in release: 3.10Found to occur in 3.10found in release: 3.13Found to occur in 3.13frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions