When calling `Scrollable.ensureVisible` to pinned `SliverPersistentHeader`, the view scrolls too much

This issue has been created since 2022-11-24.

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. Tap "Sliver Persistent Header" Button
  3. Too much Scroll Until it reach the bottom

Expected results: Gently arrive at the top both pinned & not pinned

Actual results: Gently arrive at the top when not pinned. However, if use it after pinned it, it scrolls very violently

Pinned.mp4
NotPinned.mp4
Code sample
void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late final ScrollController scrollController;

  @override
  void initState() {
    super.initState();
    scrollController = ScrollController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        controller: scrollController,
        slivers: [
          SliverToBoxAdapter(
            child: Container(
              height: 400,
              color: Colors.grey,
            ),
          ),
          const SliverPersistentHeader(
            pinned: true,
            delegate: SliverHeaderDelegate(),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate((context, index) {
              return Container(
                height: 40,
                color: Color.fromRGBO(
                    (index * 45) % 255, (index * 70) % 255, (index * 25), 1.0),
              );
            }, childCount: 200),
          ),
          const SliverToBoxAdapter(
            child: Text(
              "I'm Bottom",
              textAlign: TextAlign.center,
            ),
          )
        ],
      ),
    );
  }
}

class SliverHeaderDelegate extends SliverPersistentHeaderDelegate {
  const SliverHeaderDelegate();

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: Colors.white,
      height: 48,
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      child: TextButton(
          onPressed: () {
            Scrollable.ensureVisible(
              context,
              alignment: 0.000,
              duration: const Duration(seconds: 1),
            );
          },
          child: const Text("Sliver Persistent Header")),
    );
  }

  @override
  double get maxExtent => 48;

  @override
  double get minExtent => 48;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
Logs
Analyzing sliver_persistent_header_ensure_visible_bug...                
No issues found! (ran in 1.3s)
[✓] Flutter (Channel stable, 3.3.8, on macOS 13.0 22A380 darwin-arm, locale ko-KR)
    • Flutter version 3.3.8 on channel stable at /Users/adforus/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 52b3dc25f6 (2 weeks ago), 2022-11-09 12:09:26 +0800
    • Engine revision 857bd6b74c
    • Dart version 2.18.4
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/adforus/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Users/adforus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.3

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

[✓] Android Studio (version 2020.3)
    • Android Studio at /Users/adforus/Downloads/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.10+0-b96-7249189)

[✓] Android Studio (version 2021.3)
    • Android Studio at /Users/adforus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/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.13+0-b1751.21-8125866)

[✓] Android Studio (version 2021.3)
    • Android Studio at /Users/adforus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/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.13+0-b1751.21-8125866)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.3)
    • IntelliJ at /Users/adforus/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 71.0.5
    • Dart plugin version 222.4345.14

[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.2)
    • IntelliJ at /Users/adforus/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/222.4167.29/IntelliJ IDEA.app
    • 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

[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.3)
    • IntelliJ at /Users/adforus/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/222.4345.14/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.59.1)
    • VS Code at /Users/adforus/Downloads/Visual Studio Code.app/Contents
    • Flutter extension version 3.27.0

[✓] Connected device (3 available)
    • Pixel 7 (mobile) • 29281FDH200DQD • android-arm64  • Android 13 (API 33)
    • macOS (desktop)  • macos          • darwin-arm64   • macOS 13.0 22A380 darwin-arm
    • Chrome (web)     • chrome         • web-javascript • Google Chrome 107.0.5304.110

[!] HTTP Host Availability
    ✗ HTTP host "https://cloud.google.com/" is not reachable. Reason: Failed to connect to host in 10 seconds

! Doctor found issues in 1 category.

exaby73 wrote this answer on 2022-11-24

Triage report

I can reproduce this issue with the code sample provided on Master (3.6.0-7.0.pre.47). This only occurs with pinned: true.

Code Sample (Same as OP)
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late final ScrollController scrollController;

  @override
  void initState() {
    super.initState();
    scrollController = ScrollController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        controller: scrollController,
        slivers: [
          SliverToBoxAdapter(
            child: Container(
              height: 400,
              color: Colors.grey,
            ),
          ),
          const SliverPersistentHeader(
            pinned: true,
            delegate: SliverHeaderDelegate(),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate((context, index) {
              return Container(
                height: 40,
                color: Color.fromRGBO(
                    (index * 45) % 255, (index * 70) % 255, (index * 25), 1.0),
              );
            }, childCount: 200),
          ),
          const SliverToBoxAdapter(
            child: Text(
              "I'm Bottom",
              textAlign: TextAlign.center,
            ),
          )
        ],
      ),
    );
  }
}

class SliverHeaderDelegate extends SliverPersistentHeaderDelegate {
  const SliverHeaderDelegate();

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: Colors.white,
      height: 48,
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      child: TextButton(
          onPressed: () {
            Scrollable.ensureVisible(
              context,
              alignment: 0.000,
              duration: const Duration(seconds: 1),
            );
          },
          child: const Text("Sliver Persistent Header")),
    );
  }

  @override
  double get maxExtent => 48;

  @override
  double get minExtent => 48;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-12-10
Star Count 147243
Watcher Count 3561
Fork Count 23950
Issue Count 11325

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
dotnet: remove /proc/pid/root/tmp patch 0 2022-10-21 2022-11-28
NameError: name 'T' is not defined 6 2022-07-25 2022-12-01
[Go-SDK] Use pin.valist.io for pinning 0 2021-11-15 2022-05-12
Strange problem with a NATted server 0 2022-10-28 2022-11-27
Ability to support self hosted Gravatar 0 2022-11-10 2022-11-30
EVM: gas limits in delegate call 0 2022-12-01 2022-12-06
Running Inference using CPU only. 0 2022-07-04 2022-09-11
Bug: DCO does not allow valid emails 0 2021-03-04 2022-12-01
WIP covering RAD requests internal format 1 2018-02-11 2022-12-05
@floating-ui/dom is broken with typescript 4.7 `node16` module resolution 5 2022-05-27 2022-09-27
Feature to fetch versions of models/queryset defined by `follow` 3 2021-09-03 2022-12-01
FYI collisions when object_ids are case sensitive 3 2021-10-20 2022-12-01
Postgres jsonb fields are not serializable. 3 2021-09-06 2022-12-01
Very slow VersionQuerySet.get_deleted 1 2021-08-16 2022-12-01
createinitialrevisions does not group linked models under single revision_id 5 2021-08-26 2022-12-01
CreateInitialRevison should create an initial revision when user and date are provided 3 2021-09-03 2022-12-01
Handling eth transfer transactions with "0x0" as data in Etheno initialization file 7 2022-04-11 2022-11-22
Consider simplifying tunneling 1 2021-10-20 2022-10-22
[BUG]答题的时候就会一直没反应,浏览器也换了 都没用 1 2021-07-03 2022-11-16
[FEATURE] search on icons view 3 2022-04-08 2022-10-31
[FEA]: Update python implementation of Random Walk 0 2022-10-26 2022-11-06
client.set_db should also check for connection 0 2021-03-26 2022-11-13
Optimize CPX communication on UART2 0 2022-06-20 2022-12-02
The initialised event never gets fired 3 2022-02-04 2022-12-01
noverify doesn't recognize #-style autogen comments 0 2022-09-09 2022-11-28
Allow 'publish verification results' API be extendable without requiring a breaking change 3 2021-10-31 2022-11-27
[Optimizer] Pushdown equivalence predicate for outer/semi join 0 2021-09-11 2022-10-05
Motrix/Aria2c not using system dns 0 2022-11-11 2022-12-02
设置为菜单栏应用后AriaNG应用无法识别16800端口 1 2022-11-03 2022-12-02
MapBox texturing walkable surface/asphalt areas 1 2021-05-31 2022-10-06
the output has been hidden due to the fact that 'no_log: true 7 2022-06-28 2022-11-17
Playback creates characters not in original 9 2022-05-22 2022-11-27
[java] SimplifiedTernary: support for the java.lang.Boolean class 1 2021-11-07 2022-12-09
docs: storybook example error 1 2022-02-04 2022-12-07
Stuck on Google Screen 0 2022-01-21 2022-12-01
Help: VSCode Language Server Wrap - Will Pay 1 2021-08-25 2022-11-19
&quote; symbols in title 13 2019-03-04 2022-12-06
[General Issue] edge计算的问题 1 2022-07-04 2022-11-26
最新版本的leakcanary2.6提示ToastUtils内存泄漏 0 2021-04-02 2022-12-01
Splines video has two voices. 2:46 0 2020-12-11 2022-12-01
Support for datadog_github_repository resource 0 2021-11-04 2022-10-11
External check config fails when a turn on automation is defined for webostv 2 2022-10-16 2022-10-18
Can not run file in path that contains "!!" 3 2021-07-09 2022-12-08
New website UI 26 2020-10-27 2022-01-23
Trying to select a value in combobox changes the selection but change is not triggered. 8 2017-10-07 2022-12-06
Create a separate DockerHub organization/user for Sealed Secrets container images 0 2022-04-22 2022-11-04
Seller Experience - Build new design picker UI 0 2022-06-16 2022-10-28
Send a message to the room, why can you receive it if you are not in this room? 1 2021-03-28 2022-11-28
Facebook login exception after API 2.9 is disabled 3 2019-08-06 2022-12-04
"No GPIO controllers exist on this system" 60 2021-01-15 2022-12-03