Repository: flutter
Issue: Scroll Controller + Animated List -> MaxScrollExtent not updated correctly - (tested on web)
Labels: framework,f: scrolling,has reproducible steps,P2,found in release: 2.2,found in release: 2.5,team-framework,triaged-framework

Hi !

I am using the animated list from https://api.flutter.dev/flutter/widgets/AnimatedList-class.html and created the sample project with flutter create --sample=widgets.AnimatedList.1 mysample

I added a Scroll Controller to the animated list and noticed that the Scroll Controller doesn't have the correct data after something has been added or removed.

To reproduce the issue: 
1. run the `flutter create --sample=widgets.AnimatedList.1 mysample`
2. replace the `_AnimatedListSampleState` class by this:

```dart
  class _AnimatedListSampleState extends State<AnimatedListSample> {
  late ScrollController _sc;
  final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
  late ListModel<int> _list;
  int? _selectedItem;
  late int
      _nextItem; // The next item inserted when the user presses the '+' button.

  @override
  void initState() {
    super.initState();
    _sc = ScrollController();
    _sc.addListener(_printScrollControllerData);
    _list = ListModel<int>(
      listKey: _listKey,
      removedItemBuilder: _buildRemovedItem,
    );
    _nextItem = 3;
  }

  _printScrollControllerData() {
    //_sc.position.applyNewDimensions();
    print(
        "OFFSET -> ${_sc.offset} <- AND MAX -> ${_sc.position.maxScrollExtent}");
  }

  // Used to build list items that haven't been removed.
  Widget _buildItem(
      BuildContext context, int index, Animation<double> animation) {
    return CardItem(
      animation: animation,
      item: _list[index],
      selected: _selectedItem == _list[index],
      onTap: () {
        setState(() {
          _selectedItem = _selectedItem == _list[index] ? null : _list[index];
        });
      },
    );
  }

  // Used to build an item after it has been removed from the list. This
  // method is needed because a removed item remains visible until its
  // animation has completed (even though it's gone as far this ListModel is
  // concerned). The widget will be used by the
  // [AnimatedListState.removeItem] method's
  // [AnimatedListRemovedItemBuilder] parameter.
  Widget _buildRemovedItem(
      int item, BuildContext context, Animation<double> animation) {
    return CardItem(
      animation: animation,
      item: item,
      selected: false,
      // No gesture detector here: we don't want removed items to be interactive.
    );
  }

  // Insert the "next item" into the list model.
  void _insert() {
    final int index =
        _selectedItem == null ? _list.length : _list.indexOf(_selectedItem!);
    _list.insert(index, _nextItem++);
  }

  // Remove the selected item from the list model.
  void _remove() {
    if (_selectedItem != null) {
      _list.removeAt(_list.indexOf(_selectedItem!));
      setState(() {
        _selectedItem = null;
      });
    } else {
      _list.removeAt(0);
      setState(() {
        _selectedItem = null;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('AnimatedList'),
          actions: <Widget>[
            IconButton(
              icon: const Icon(Icons.add_circle),
              onPressed: _insert,
              tooltip: 'insert a new item',
            ),
            IconButton(
              icon: const Icon(Icons.remove_circle),
              onPressed: _remove,
              tooltip: 'remove the selected item',
            ),
          ],
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: AnimatedList(
            controller: _sc,
            key: _listKey,
            itemBuilder: _buildItem,
          ),
        ),
      ),
    );
  }
}
```

After that, run the project with `flutter run -d chrome` and do this
a. Add a lot of elements - outside the visible screen
b. Start scrolling down 
=> OUTPUT:
OFFSET -> 0.5 <- AND MAX -> 477
OFFSET -> 1 <- **AND MAX -> 2241**

The first little scroll will assume that the MaxScrollExtent is 477 and it's only at the next scroll event that this value is correctly set.

Thank you !

<details>
  <summary>❯ flutter doctor -v</summary>
  
```
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.4 20F71 darwin-x64, locale en-BE)
    • Flutter version 2.2.3 at /Users/cekrozl1/Desktop/OneSpan/MSS/FLUTTER/flutter
    • Framework revision f4abaa0735 (5 weeks ago), 2021-07-01 12:46:11 -0700
    • Engine revision 241c87ad80
    • Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/cekrozl1/Library/Android/sdk
    • Platform android-30, build-tools 30.0.3
    • ANDROID_HOME = /Users/cekrozl1/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.5.1, Build version 12E507
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.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 11.0.8+10-b944.6916264)

[✓] IntelliJ IDEA Community Edition (version 2020.3.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.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.58.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.24.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 92.0.4515.131

• No issues found!
```
</details>