Nate Wilson
9ee00ae168
Fix NestedScrollView inner position logic (#157756)
closes #40740
<p align="center">
<img
src="https://github.com/user-attachments/assets/8a48abd0-466b-4e06-90f3-dd05869bac27"
alt="NestedScrollController fix"
height="720px"
/>
</p>
<details> <summary><h4>Demo source code</h4> (click to expand)</summary>
(this is a slightly more concise version of the code sample from #40740)
```dart
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(body: NewsScreen()),
),
);
}
class NewsScreen extends StatelessWidget {
const NewsScreen({super.key});
static const List<String> _tabs = <String>['Featured', 'Popular', 'Latest'];
static final List<Widget> _tabViews = <Widget>[
for (final String name in _tabs)
SafeArea(
top: false,
bottom: false,
child: Builder(builder: (BuildContext context) {
final handle = NestedScrollView.sliverOverlapAbsorberHandleFor(context);
return NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) => true,
child: CustomScrollView(
key: PageStorageKey<String>(name),
slivers: <Widget>[
SliverOverlapInjector(handle: handle),
SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
childCount: 30,
(BuildContext context, int index) => Container(
margin: const EdgeInsets.only(bottom: 8),
width: double.infinity,
height: 150,
color: const Color(0xFFB0A4C8),
alignment: Alignment.center,
child: Text(
'$name $index',
style: const TextStyle(fontWeight: FontWeight.w600),
),
),
),
),
),
],
),
);
}),
),
];
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: _tabs.length,
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) => <Widget>[
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverSafeArea(
top: false,
sliver: SliverAppBar(
title: const Text('Tab Demo'),
floating: true,
pinned: true,
snap: true,
forceElevated: innerBoxIsScrolled,
bottom: TabBar(
tabs: _tabs.map((String name) => Tab(text: name)).toList(),
),
),
),
),
],
body: TabBarView(children: _tabViews),
),
);
}
}
```
<br>
</details>
<br>
This bug can be traced to a return statement inside `_NestedScrollPosition`:
```dart
double applyClampedDragUpdate(double delta) {
// ...
return delta + offset;
}
```
Thanks to some quirks of floating-point arithmetic, `applyClampedDragUpdate` would sometimes return a tiny non-zero value, which ends up ruining one of the scroll coordinator's equality checks.
8990ed6538/packages/flutter/lib/src/widgets/nested_scroll_view.dart (L658-L664)
2024-11-04 16:42:06 +00:00
..
2024-06-12 20:05:10 +00:00
2024-06-12 20:05:10 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-09-10 15:32:40 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-06-05 15:20:07 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-01-19 18:29:07 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-02-07 10:01:22 -08:00
2023-12-15 14:13:31 -08:00
2024-01-04 23:26:38 +00:00
2024-08-30 09:36:10 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2024-01-19 18:29:07 +00:00
2024-09-03 19:55:12 +00:00
2023-12-15 14:13:31 -08:00
2024-05-13 22:12:49 +00:00
2024-10-25 16:27:51 +00:00
2024-09-26 04:04:24 +00:00
2024-03-18 21:32:22 +00:00
2024-09-10 15:32:40 -07:00
2024-05-13 22:12:49 +00:00
2024-02-29 00:47:55 +00:00
2024-07-31 23:35:24 +00:00
2024-08-15 19:02:23 +00:00
2024-03-24 18:01:17 +00:00
2023-12-15 14:13:31 -08:00
2024-03-24 18:01:17 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-05-28 16:35:29 -07:00
2024-08-14 18:19:17 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-09-10 15:32:40 -07:00
2024-03-26 12:22:57 -07:00
2023-12-15 14:13:31 -08:00
2024-05-20 17:17:55 +00:00
2023-12-15 14:13:31 -08:00
2024-10-18 22:37:51 +00:00
2023-12-15 14:13:31 -08:00
2024-04-03 18:36:19 +00:00
2024-01-26 23:05:53 +00:00
2023-12-15 14:13:31 -08:00
2024-02-17 01:18:33 +00:00
2024-05-25 19:59:19 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-14 14:20:44 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2024-10-30 03:01:18 +00:00
2024-10-17 23:46:25 +00:00
2024-07-09 15:54:17 -07:00
2024-06-12 20:05:10 +00:00
2023-12-15 14:13:31 -08:00
2024-04-09 19:35:07 +00:00
2024-01-03 00:11:18 +00:00
2023-12-15 14:13:31 -08:00
2024-10-23 23:22:15 +00:00
2024-03-25 10:45:02 -07:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2024-05-31 03:50:30 +00:00
2023-12-15 14:13:31 -08:00
2024-05-30 23:00:21 +00:00
2024-05-18 10:41:56 -07:00
2024-07-08 22:51:04 +00:00
2024-08-05 23:04:50 +00:00
2024-01-10 00:17:33 +00:00
2024-09-30 17:19:25 +00:00
2024-09-10 15:32:40 -07:00
2024-06-12 20:05:10 +00:00
2024-06-12 20:05:10 +00:00
2024-06-18 17:52:21 +00:00
2023-12-15 14:13:31 -08:00
2024-09-13 01:24:11 +00:00
2024-06-12 20:05:10 +00:00
2024-02-09 12:37:20 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-06-17 19:59:06 +00:00
2024-09-09 20:35:03 +00:00
2023-12-15 14:13:31 -08:00
2024-10-22 17:53:17 +00:00
2023-12-15 14:13:31 -08:00
2024-09-09 17:30:19 +00:00
2024-09-10 15:32:40 -07:00
2024-01-10 00:17:33 +00:00
2024-02-21 03:14:19 +00:00
2024-08-20 15:33:21 -07:00
2024-08-20 15:33:21 -07:00
2024-03-14 14:20:44 -07:00
2024-10-22 10:22:01 -07:00
2024-10-22 10:22:01 -07:00
2024-09-10 15:32:40 -07:00
2024-10-18 22:37:51 +00:00
2024-01-10 00:17:33 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-10-04 18:59:05 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-09-11 23:59:53 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-09-11 17:34:58 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-06-05 20:51:04 +00:00
2023-12-15 14:13:31 -08:00
2024-06-03 16:45:23 +00:00
2023-12-15 14:13:31 -08:00
2024-09-13 01:24:11 +00:00
2024-07-31 23:35:24 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-01-19 18:29:07 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-07-02 23:01:44 +00:00
2023-12-15 14:13:31 -08:00
2024-03-06 03:23:18 +00:00
2023-12-15 14:13:31 -08:00
2024-01-11 18:26:04 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-08 15:34:01 +00:00
2023-12-15 14:13:31 -08:00
2024-03-19 17:00:24 +00:00
2024-04-03 15:49:38 -07:00
2023-12-15 14:13:31 -08:00
2024-01-26 23:05:53 +00:00
2024-01-16 09:44:49 -08:00
2023-12-15 14:13:31 -08:00
2024-05-28 10:39:30 -07:00
2024-01-29 09:49:44 -08:00
2024-09-27 00:55:31 +00:00
2024-01-31 19:22:07 +00:00
2024-09-27 00:55:31 +00:00
2024-01-26 23:05:53 +00:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2024-10-31 16:47:55 +00:00
2023-08-17 23:55:05 +00:00
2024-11-04 16:42:06 +00:00
2023-12-15 14:13:31 -08:00
2024-06-20 06:44:11 -07:00
2023-12-15 14:13:31 -08:00
2024-03-18 12:45:22 +00:00
2024-01-23 17:16:26 +00:00
2023-12-15 14:13:31 -08:00
2024-08-29 01:59:34 +00:00
2024-08-29 21:49:25 +00:00
2024-02-22 19:16:25 +00:00
2024-05-20 17:43:00 +00:00
2024-03-14 14:20:44 -07:00
2024-02-23 20:06:06 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-08-07 16:45:53 +00:00
2024-01-29 21:21:34 -08:00
2023-12-15 14:13:31 -08:00
2024-05-01 22:05:02 +00:00
2023-12-15 14:13:31 -08:00
2024-06-07 21:27:06 +00:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2023-12-20 22:05:29 +00:00
2024-05-07 09:04:49 -07:00
2024-01-10 00:17:33 +00:00
2023-12-27 19:03:29 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-14 14:20:44 -07:00
2024-10-02 17:18:21 +00:00
2023-12-15 14:13:31 -08:00
2024-05-13 22:12:49 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-01-12 22:10:25 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-07-31 23:35:24 +00:00
2023-12-15 14:13:31 -08:00
2024-10-21 21:29:05 +00:00
2024-10-02 20:08:11 +00:00
2024-08-14 18:19:17 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-02-01 11:49:54 -08:00
2024-03-13 07:10:48 +00:00
2024-01-19 18:29:07 +00:00
2024-03-25 10:45:02 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-06-04 22:02:12 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-05-31 20:30:09 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-02-14 21:08:25 +00:00
2024-01-10 00:17:33 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-02-22 19:16:25 +00:00
2024-04-02 21:10:52 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-06-10 17:52:58 +00:00
2024-10-15 20:58:51 +00:00
2024-04-09 19:35:07 +00:00
2024-10-21 19:25:52 +00:00
2024-09-30 22:44:20 +00:00
2024-04-02 21:10:52 +00:00
2024-10-18 22:37:51 +00:00
2024-10-18 22:37:51 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-05-08 23:12:47 +00:00
2023-12-15 14:13:31 -08:00
2024-10-18 22:37:51 +00:00
2024-10-21 19:25:52 +00:00
2024-10-18 22:37:51 +00:00
2023-12-15 14:13:31 -08:00
2024-07-31 23:35:24 +00:00
2023-12-15 14:13:31 -08:00
2024-10-18 22:37:51 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-05 00:16:21 +00:00
2023-12-15 14:13:31 -08:00
2024-08-15 19:02:23 +00:00
2023-12-15 14:13:31 -08:00
2024-07-23 15:46:08 +00:00
2024-01-10 00:17:33 +00:00
2024-01-24 16:13:26 +00:00
2024-09-24 22:12:32 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-15 10:42:02 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2024-03-13 16:34:25 +00:00
2024-03-06 03:23:18 +00:00
2024-07-08 19:33:14 +00:00
2023-12-15 14:13:31 -08:00
2024-10-24 19:23:07 +00:00
2023-12-15 14:13:31 -08:00
2024-05-28 16:35:29 -07:00
2024-10-21 19:25:52 +00:00
2023-12-15 14:13:31 -08:00
2024-10-17 18:12:18 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-06 03:23:18 +00:00
2024-03-15 10:42:02 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-07-02 23:21:22 +00:00
2023-12-15 14:13:31 -08:00
2024-04-15 16:06:07 +00:00
2024-05-20 17:17:55 +00:00
2024-02-17 01:18:33 +00:00
2023-12-15 14:13:31 -08:00
2024-08-09 13:52:06 -07:00
2024-05-20 17:17:55 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-05-15 16:52:17 -07:00
2024-01-19 18:29:07 +00:00
2024-10-21 16:59:16 +00:00
2024-05-28 16:35:29 -07:00
2024-01-16 21:40:08 +00:00
2024-05-01 20:51:54 +00:00
2023-12-15 14:13:31 -08:00
2024-07-30 01:29:31 +00:00
2023-12-15 14:13:31 -08:00
2024-08-15 23:00:15 +00:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-03-25 10:36:42 -07:00
2023-12-15 14:13:31 -08:00
2024-05-14 13:46:35 -07:00
2024-01-29 21:21:34 -08:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-09-10 15:32:40 -07:00
2024-03-25 10:36:42 -07:00
2023-12-15 14:13:31 -08:00
2024-05-31 20:30:09 +00:00
2024-05-24 16:26:07 +00:00
2024-05-24 16:26:07 +00:00
2024-06-25 21:55:34 -07:00
2023-12-15 14:13:31 -08:00
2023-12-15 14:13:31 -08:00
2024-07-26 00:27:37 +00:00
2023-12-15 14:13:31 -08:00
2024-07-31 23:35:24 +00:00
2024-10-23 12:30:55 -07:00
2023-07-17 17:56:07 +00:00
2024-10-16 20:18:28 +00:00
2024-03-19 17:58:13 +00:00
2024-05-09 20:20:48 -07:00