Repository: flutter
Issue: Add support for getting the default ScrollController from context using ScrollController.of()
Labels: c: new feature,framework,f: scrolling,c: proposal,P3,team-framework,triaged-framework

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Use case

Consider a scenario where we have a nested CustomScrollView inside a NestedScrollView. The CustomScrollView has a child that needs to know the current scroll position of the CustomScrollView to perform some action. Without the proposed feature, the developer would have to manually pass the ScrollController of the CustomScrollView through the widget tree to the child that needs it. This can lead to a lot of boilerplate code, especially when dealing with complex nested scroll views.

With the proposed feature, the developer can simply use ScrollController.of(context) in the child that needs to access the scroll position. This eliminates the need for manually passing the ScrollController through the widget tree and makes it much easier to access the scroll position of a nested CustomScrollView.

This is a common use case, when using NestedScrollView which is a built-in scrollable that can contain multiple scrolling widgets, and the CustomScrollView which allows developers to create custom scroll effects.

This use case will also help to improve the code readability and structure, and would make it much more straightforward to access the ScrollController in a complex nested scroll view.
<!--
     Please tell us the problem you are running into that led to you wanting
     a new feature.

     Is your feature request related to a problem? Please give a clear and
     concise description of what the problem is.

     Describe the alternative solutions you've considered. Is there a package
     on pub.dev/flutter that already solves this?
-->

## Proposal

<!--
     Briefly but precisely describe what you would like Flutter to be able to do.

     Consider attaching something showing what you are imagining:
      * images
      * videos
      * code samples

     Does this have to be provided by Flutter directly, or can it be provided
     by a package on pub.dev/flutter? If so, maybe consider implementing and
     publishing such a package rather than filing a bug.
-->


### Introduction:

It would be useful to have a way to easily access the default ScrollController of a scrollable widget from the context, similar to how we can access the Theme using Theme.of(context). This would allow developers to easily access the ScrollController of a ScrollView, for example, without having to manually pass it down through the widget tree.

### Proposed solution:

Introduce a new method, ScrollController.of(context), that takes a BuildContext as an argument and returns the default ScrollController of the nearest ancestor Scrollable widget. If no such widget is found, the method should return null.

### Benefit:

This feature will be useful for developers who want to access the ScrollController of a ScrollView without having to manually pass it down through the widget tree. This can also help reduce the amount of boilerplate code needed to manage the ScrollController of a ScrollView.

### Impact:

This feature will have a low impact on the existing codebase and will be easy to implement. It will also improve developer productivity by reducing the amount of boilerplate code needed to manage the ScrollController of a ScrollView.

### Example Code:
In this example, MyChild is a widget that needs to know the current scroll position of the nearest ancestor Scrollable widget, which in this case is the CustomScrollView. Without the proposed feature, the developer would have to manually pass the ScrollController of the CustomScrollView through the widget tree to MyChild. But with the proposed feature, the developer can simply use ScrollController.of(context) in MyChild, eliminating the need for manually passing the ScrollController through the widget tree.

```dart
import 'package:flutter/material.dart';

class MyChild extends StatefulWidget {
  MyChild({Key? key}) : super(key: key);

  @override
  State<MyChild> createState() => _MyChildState();
}

class _MyChildState extends State<MyChild> {
  void _onScrolled() {
    setState(() {
      print('Scrolling');
    });
  }

  @override
  Widget build(BuildContext context) {
    final ScrollController controller = ScrollController.of(context);
    final double currentPosition = controller.position.pixels;
    controller.addListener(_onScrolled);
    try {
      return SliverAppBar(
        pinned: true,
        title: Text("Current position: $currentPosition"),
      );
    } finally {
      controller.removeListener(_onScrolled);
    }
  }
}

class MyParent extends StatelessWidget {
  final ScrollController _controller = ScrollController();

  @override
  Widget build(BuildContext context) {
    return NestedScrollView(
      floatHeaderSlivers: true,
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
          const SliverAppBar(
            title: Text("MyParent"),
            floating: true,
          ),
        ];
      },
      body: CustomScrollView(
        controller: _controller,
        slivers: [
          MyChild(),
          SliverList.builder(
            itemCount: 9,
            itemBuilder: (context, index) {
              return Container(height: 200, color: Colors.blue[100 * index]);
            },
          ),
        ],
      ),
    );
  }
}

```

Here's an example of how a developer would have to manually pass the ScrollController of the CustomScrollView to the child:


```dart
void main() {
  runApp(MaterialApp(home: MyParent()));
}

class MyChild extends StatefulWidget {
  final ScrollController controller;

  const MyChild({Key? key, required this.controller}) : super(key: key);

  @override
  State<MyChild> createState() => _MyChildState();
}

class _MyChildState extends State<MyChild> {
  void _onScrolled() {
    setState(() {
      print('Scrolling');
    });
  }

  @override
  void initState() {
    super.initState();
    widget.controller.addListener(_onScrolled);
  }

  @override
  void dispose() {
    widget.controller.removeListener(_onScrolled);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final double currentPosition = widget.controller.position.pixels;
    return SliverAppBar(
      pinned: true,
      title: Text("Current position: $currentPosition"),
    );
  }
}

class MyParent extends StatelessWidget {
  final ScrollController _controller = ScrollController();

  @override
  Widget build(BuildContext context) {
    return NestedScrollView(
      floatHeaderSlivers: true,
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
          const SliverAppBar(
            title: Text("MyParent"),
            floating: true,
            // expandedHeight: 150,
          ),
        ];
      },
      body: CustomScrollView(
        controller: _controller,
        slivers: [
          MyChild(controller: _controller),
          SliverList.builder(
            itemCount: 9,
            itemBuilder: (context, index) {
              return Container(height: 200, color: Colors.blue[100 * index]);
            },
          ),
        ],
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(home: MyParent()));
}
```