Sub topic 12
Scroll Physics and Platform Behavior
What scroll physics controls
This chapter assumes basic Dart syntax, Flutter widgets, Column, Row, and Expanded, plus the ability to run and hot reload a Flutter app.
Scroll physics is the set of rules that controls how a scrollable responds to input. It affects dragging, overscroll, momentum, and what happens when the user releases a finger or trackpad.
The physics property accepts a ScrollPhysics object:
ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: 50,
itemBuilder: (context, index) {
return ListTile(title: Text('Row $index'));
},
)This list uses a bouncing effect when the user drags past an edge. Physics does not decide whether the list scrolls vertically or horizontally. Use scrollDirection for that:
ListView(
scrollDirection: Axis.horizontal,
children: const [
SizedBox(width: 180, child: Card(child: Center(child: Text('One')))),
SizedBox(width: 180, child: Card(child: Center(child: Text('Two')))),
],
)Default platform behavior
Flutter's default ScrollBehavior chooses physics that match the target platform. Android commonly uses clamping behavior. The content stops at the edge without a visible bounce. Apple platforms commonly use bouncing behavior.
This default is usually the best choice because users recognize the behavior from other apps. Override it only when your product needs a consistent experience or a specific interaction.
| Physics | Typical result | Useful when |
|---|---|---|
| `ClampingScrollPhysics` | Stops at the edge | You want Android-style bounded scrolling |
| `BouncingScrollPhysics` | Allows an elastic edge effect | You want an iOS-style bounce |
| `NeverScrollableScrollPhysics` | Prevents user scrolling | Another widget owns the scrolling |
| `AlwaysScrollableScrollPhysics` | Allows dragging even when content is short | A pull-to-refresh control must always work |
Choosing and combining physics
A short list may not have enough content to scroll. AlwaysScrollableScrollPhysics still permits a drag, which is important with RefreshIndicator:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: PhysicsDemo()));
}
class PhysicsDemo extends StatelessWidget {
const PhysicsDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Scroll physics')),
body: RefreshIndicator(
onRefresh: () async {
await Future<void>.delayed(const Duration(milliseconds: 500));
},
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: 3,
itemBuilder: (context, index) {
return ListTile(title: Text('Item $index'));
},
),
),
);
}
}You can combine behavior through the parent property. For example, this allows a short list to overscroll with a bounce:
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),Use NeverScrollableScrollPhysics carefully. It disables touch scrolling, but a ScrollController can still move the list programmatically. A common use is a non-scrolling inner list inside a parent scroll view. In many nested layouts, however, using CustomScrollView and slivers gives clearer ownership of scrolling.
Exercise
Which physics should you add when a short list must support pull to refresh?
Show answer
Use AlwaysScrollableScrollPhysics, optionally as the parent of BouncingScrollPhysics when an elastic edge effect is wanted.