Sub topic 12
Scroll physics and platform behavior
This chapter assumes you can build a ListView and GridView, and that you know what a ScrollController is. Here you will learn what decides how a list feels when you drag, fling or overscroll it, and how to change that behavior for one list or for the whole app.
What scroll physics controls
A ScrollPhysics object answers questions that the scrollable asks on every gesture:
- How much of the user's finger movement becomes scroll movement?
- What happens at the edge of the content: stop, or stretch past it?
- How does a fling decelerate, and where does it settle?
- Can the user scroll at all?
ListView, GridView, CustomScrollView, SingleChildScrollView and PageView all take a physics parameter. Most of the time you leave it null. Flutter then picks physics that match the platform.
The built-in physics classes
| Class | Behavior | Typical use |
|---|---|---|
| `ClampingScrollPhysics` | Stops at the edges. The content cannot move past them. | Android, Windows, Linux default |
| `BouncingScrollPhysics` | Lets the content move past the edge, then springs back. | iOS and macOS default |
| `AlwaysScrollableScrollPhysics` | Always allows user scrolling, even when the content is shorter than the viewport. | Pull to refresh on short lists |
| `NeverScrollableScrollPhysics` | Ignores user drags and flings. | Lists nested in another scrollable, or lists driven by code |
| `PageScrollPhysics` | Snaps to page boundaries. | `PageView` |
| `FixedExtentScrollPhysics` | Snaps to item boundaries. | `ListWheelScrollView` |
AlwaysScrollableScrollPhysics and NeverScrollableScrollPhysics decide whether scrolling is allowed. The other classes decide how scrolling feels. You often need both at once, and that is what the parent argument is for.
Setting physics on one list
Pass a physics object to the scrollable. The example below forces iOS-style bounce on every platform:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: BounceDemo()));
class BounceDemo extends StatelessWidget {
const BounceDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Bounce')),
body: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: 40,
itemBuilder: (context, index) => ListTile(title: Text('Row $index')),
),
);
}
}Forcing one style on every platform is usually a design choice you should make on purpose. Users expect their own platform's feel, so the default is normally the right one.
How Flutter picks the default
When physics is null, the scrollable asks the nearest ScrollConfiguration for its ScrollBehavior. That object's getScrollPhysics method looks at Theme.of(context).platform and returns the matching physics.
Because the lookup uses the theme's platform, you can test the iOS feel on an Android device by overriding the theme:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
theme: ThemeData(platform: TargetPlatform.iOS),
home: Scaffold(
body: ListView(
children: [
for (var i = 0; i < 30; i++) ListTile(title: Text('Row $i')),
],
),
),
),
);
}Overriding the platform changes more than scrolling. It also affects page transitions and some text-editing behavior. Use it for experiments, not as a way to set scroll feel in a shipped app.
Chaining physics with parent
Physics objects can wrap each other. Each object handles the parts it knows and passes the rest to its parent. This is how you combine "bounce" with "always scrollable":
ListView.builder(
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),
itemCount: items.length,
itemBuilder: (context, index) => ListTile(title: Text(items[index])),
)The most common reason to chain is RefreshIndicator. A pull-to-refresh gesture needs a drag to be accepted. If the list has only three items, it is shorter than the viewport, and the default physics will refuse to scroll. The indicator never appears. Adding AlwaysScrollableScrollPhysics fixes that.
If you pass only AlwaysScrollableScrollPhysics(), you lose the platform's default feel. On iOS you would get clamping instead of bounce. Chain it with the platform physics, or build the chain from the platform default with ScrollConfiguration.of(context).getScrollPhysics(context):
import 'package:flutter/material.dart';
class RefreshableList extends StatelessWidget {
const RefreshableList({super.key, required this.items, required this.onRefresh});
final List<String> items;
final Future<void> Function() onRefresh;
@override
Widget build(BuildContext context) {
final platformPhysics = ScrollConfiguration.of(context).getScrollPhysics(context);
return RefreshIndicator(
onRefresh: onRefresh,
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics().applyTo(platformPhysics),
itemCount: items.length,
itemBuilder: (context, index) => ListTile(title: Text(items[index])),
),
);
}
}applyTo returns a copy of the physics with a new parent, so the always-scrollable rule sits on top of whatever the platform provides.
Turning scrolling off
NeverScrollableScrollPhysics is the standard way to make a list that does not respond to the user. It matters most in the nested layout from the earlier chapter on bounded constraints. There, a shrinkWrap: true list sits inside an outer scroll view, and you want only the outer view to scroll:
SingleChildScrollView(
child: Column(
children: [
const Text('Summary'),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: 5,
itemBuilder: (context, index) => ListTile(title: Text('Row $index')),
),
],
),
)Disabling user input does not disable code. A ScrollController can still call jumpTo or animateTo on a list with NeverScrollableScrollPhysics. This is how apps build a PageView that moves only through buttons.
Remember that shrinkWrap: true makes the list build all of its children, so the lazy-building benefit is gone. For long lists, use a CustomScrollView with slivers instead.
Mouse, trackpad and web
Phones use touch, but desktop and web apps also receive mouse and trackpad input. Flutter's default ScrollBehavior accepts touch, stylus and trackpad drags. It does not let a plain mouse drag scroll the content, because desktop users expect the wheel and scrollbars. This surprises developers who test a web build and find that dragging a list with the mouse does nothing.
Enable it by supplying a custom ScrollBehavior to the app:
import 'dart:ui' show PointerDeviceKind;
import 'package:flutter/material.dart';
void main() => runApp(const DragApp());
class DragApp extends StatelessWidget {
const DragApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
scrollBehavior: const MaterialScrollBehavior().copyWith(
dragDevices: {
PointerDeviceKind.touch,
PointerDeviceKind.mouse,
PointerDeviceKind.stylus,
PointerDeviceKind.trackpad,
},
),
home: Scaffold(
body: ListView.builder(
itemCount: 50,
itemBuilder: (context, index) => ListTile(title: Text('Row $index')),
),
),
);
}
}Scrollbars follow the same mechanism. On desktop platforms the default behavior wraps scrollables in a Scrollbar. On mobile it does not. To change this for a subtree, wrap it in a ScrollConfiguration with a behavior that overrides buildScrollbar, or set the scrollbars option through copyWith(scrollbars: false).
Overscroll indicators
On clamping platforms, the visual response at the edge is not part of the physics class. It comes from ScrollBehavior.buildOverscrollIndicator. Depending on the Flutter version and whether the app uses Material 3, Android shows either a glow or a stretch effect. iOS and macOS show no indicator, because the bounce is the feedback. To remove the indicator, supply a ScrollBehavior that returns the child unchanged from buildOverscrollIndicator.
Common mistakes
- Setting
physics: AlwaysScrollableScrollPhysics()and expecting a bounce on iOS. It only permits scrolling. Chain it with a bouncing parent. - Using
NeverScrollableScrollPhysicson a list that is not nested, then wondering why it will not move. Check whether the physics was set by a parentScrollConfiguration. - Forcing
BouncingScrollPhysicswithRefreshIndicatoron Android. The indicator relies on overscroll notifications, which differ by physics, and the result can look wrong. Test on a real device. - Testing only with touch. Always check mouse-wheel, drag and scrollbar behavior if you ship to web or desktop.
Exercise
A ListView with three items has a RefreshIndicator around it, but pulling down never shows the spinner. Why, and what is the smallest fix that keeps the platform's normal feel?
Show answer
With three items the content is shorter than the viewport, so the default physics will not accept a drag. The indicator never receives an overscroll. Set physics to AlwaysScrollableScrollPhysics chained with the platform physics, for example const AlwaysScrollableScrollPhysics().applyTo(ScrollConfiguration.of(context).getScrollPhysics(context)).
Exercise
Your web build lets users scroll a list with the mouse wheel but not by dragging with the mouse. Where do you change this, and what do you change?
Show answer
Supply a custom ScrollBehavior, for example MaterialApp(scrollBehavior: const MaterialScrollBehavior().copyWith(dragDevices: {...})), and include PointerDeviceKind.mouse in the dragDevices set.