Skip to content

Nesting SliverMainAxisGroups breaks Scrollable.ensureVisible #167801

@navaronbracke

Description

@navaronbracke

Steps to reproduce

  1. flutter create bug
  2. paste the code sample
  3. Run the code sample on the iOS Simulator (also reproduced this on Dartpad, so I think the platform is irrelevant)
  4. Scroll down to Field 2.1
  5. Tap on the text field, so that it gains focus
  6. Once the scrollable settles, the field Field 2.1 is not the one that is scrolled to.

Expected results

Nesting SliverMainAxisGroup widgets should not break Scrollable.ensureVisible().

Actual results

The surrounding scrollable scrolls to the wrong offset.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() => runApp(const SliverBugTestApp());

class SliverBugTestApp extends StatelessWidget {
  const SliverBugTestApp({super.key});

  @override
  Widget build(BuildContext context) {
    // The number of groups and items per group needs to be high enough to reproduce the bug.
    const int sliverGroupsCount = 3;
    const int sliverGroupItemsCount = 60;
    // To make working with the scroll offset easier, each item is a fixed height.
    const double itemHeight = 72.0;

    return MaterialApp(
      theme: ThemeData(
        inputDecorationTheme: const InputDecorationTheme(
          focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: Color(0xFF1489FD))),
          enabledBorder: OutlineInputBorder(borderSide: BorderSide(color: Color(0xFFB1BDC5))),
        ),
      ),
      home: Scaffold(
        body: CustomScrollView(
          slivers: [
            SliverMainAxisGroup(
              slivers: [
                for (int i = 1; i <= sliverGroupsCount; i++)
                  SliverMainAxisGroup(
                    slivers: <Widget>[
                      SliverList.builder(
                        itemCount: sliverGroupItemsCount,
                        itemBuilder: (_, int index) {
                          return SizedBox(
                            height: itemHeight,
                            child: Padding(
                              // This extra padding is to make visually debugging the test app a bit better,
                              // othwerwise the label text clips the text field above.
                              padding: const EdgeInsets.symmetric(vertical: 8),
                              child: TextField(decoration: InputDecoration(labelText: 'Field $i.${index + 1}')),
                            ),
                          );
                        },
                      ),
                    ],
                  ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
Screen.Recording.2025-04-29.at.10.03.56.mov

Logs

Logs
navaronbracke@MacBook-Pro-van-Navaron slivers_group_bug % flutter run
Launching lib/main.dart on iPhone 16 in debug mode...
Running Xcode build...                                                  
 └─Compiling, linking and signing...                         4.4s
Xcode build done.                                           21.3s
Syncing files to device iPhone 16...                                54ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on iPhone 16 is available at: http://127.0.0.1:51921/JElW0BScgSU=/
The Flutter DevTools debugger and profiler on iPhone 16 is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:51921/JElW0BScgSU=/

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.2, on macOS 14.6.1 23G93 darwin-x64, locale en-BE) [2.2s]
    • Flutter version 3.29.2 on channel stable at /Users/navaronbracke/Documents/flutter
    • Upstream repository git@github.com:navaronbracke/flutter.git
    • FLUTTER_GIT_URL = git@github.com:navaronbracke/flutter.git
    • Framework revision c236373904 (6 weeks ago), 2025-03-13 16:17:06 -0400
    • Engine revision 18b71d647a
    • Dart version 3.7.2
    • DevTools version 2.42.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [5.6s]
    • Android SDK at /Users/navaronbracke/Library/Android/sdk
    • Platform android-35, build-tools 34.0.0
    • ANDROID_HOME = /Users/navaronbracke/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/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 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2) [5.7s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [32ms]
    • CHROME_EXECUTABLE = /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1) [31ms]
    • 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.11+0-17.0.11b1207.24-11852314)

[✓] VS Code (version 1.99.3) [25ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.108.0

[✓] Connected device (3 available) [7.1s]
    • iPhone 16 (mobile) • 7F6D7425-2414-420F-B041-06FF175CABD6 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-18-2 (simulator)
    • macOS (desktop)    • macos                                • darwin-x64     • macOS 14.6.1 23G93
      darwin-x64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome
      135.0.7049.115
    ! Error: Browsing on the local area network for iPhone van device. Ensure the device is unlocked
      and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for RudyB. Ensure the device is unlocked and attached
      with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources [877ms]
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: scrollingViewports, list views, slivers, etc.found in release: 3.29Found to occur in 3.29found in release: 3.32Found to occur in 3.32frameworkflutter/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 team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions