Repository: flutter
Issue: EnsureVisible does not work going backwards on a CustomScrollView set to primary inheriting from NestedScrollView
Labels: framework,f: scrolling,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.24,found in release: 3.26

### Steps to reproduce

Steps:
1. Scroll past an item with the global key in the inner scroll view
2. Press the floating action button to navigate to the item with the key ('no')
3. Observe


Notes: 
- works fine when primary is set to false on the CustomScrollView
- works fine when there is no content inside of the outer scroll view (headersliverbuilder)
- works fine when you have not scrolled past the item ('no')

### Expected results

Should navigate to the item with the global key ('no')

### Actual results

Navigated to the top of the screen, above the outer scroll view (green box)

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

const String target = 'no';
void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

final GlobalKey<NestedScrollViewState> nestedKey = GlobalKey();
final GlobalKey scrollKey = GlobalKey();

class _MainAppState extends State<MainApp> {
  List<String> stuff = [
    "hello",
    "goodbye",
    "whats up",
    "howdy",
    "no",
    "yes",
    "maybe",
    "possibly",
    "likely",
    "could be"
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: NestedScrollView(
        key: nestedKey,
        headerSliverBuilder: (context, innerBoxScrolled) => <Widget>[
          SliverToBoxAdapter(
            child: Container(
              height: 300,
              color: Colors.green,
            ),
          ),
        ],
        body: SizedBox(
          height: 500,
          child: CustomScrollView(primary: true, slivers: [
            SliverToBoxAdapter(
              child: Column(
                children: stuff
                    .map((e) => GestureDetector(
                          onTap: () {},
                          child: SizedBox(
                              height: 200,
                              child: ListTile(
                                key: e == target ? scrollKey : null,
                                leading: Text(e),
                              )),
                        ))
                    .toList(),
              ),
            ),
          ]),
        ),
      ),
      floatingActionButton: FloatingActionButton(onPressed: () {
        if (nestedKey.currentState != null) {
          Scrollable.ensureVisible(scrollKey.currentContext!);
        }
      }),
    ));
  }
}

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

https://github.com/user-attachments/assets/11db6a74-17fd-421a-9cdf-868918f425b6


</details>


### Logs

Logs: empty

<details open><summary>Logs</summary>

```console
Showing iPhone 16 Pro Max logs:


```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
flutter doctor -v 
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.5 23F79 darwin-arm64, locale en-AU)
    • Flutter version 3.24.3 on channel stable at /Users/**/Desktop/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (3 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/**/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/**/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16A242d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • 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.10+0-17.0.10b1087.21-11609105)

[✓] VS Code (version 1.93.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.96.0

[✓] Connected device (4 available)
    • iPhone 16 Pro Max (mobile)      • 7EF3EBC3-5032-463D-9189-66B86C4FFF9E • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-18-0 (simulator)
    • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 129.0.6668.70

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>