Sub topic 13
Platform-appropriate scrolling
This chapter assumes basic Dart, StatefulWidget, setState, BuildContext, and familiarity with MaterialApp, CupertinoApp, and ListView.
Scrolling can feel different on each platform. iOS commonly uses bouncing physics, where content moves slightly past an edge and springs back. Android commonly uses clamping physics, where content stops at the edge. Desktop interfaces often add visible scrollbars and support mouse-wheel input.
Flutter's ScrollBehavior controls these choices. A scroll physics object describes how a scrollable responds to gestures and reaches its boundaries. Flutter already supplies platform-aware defaults, but an explicit behavior is useful when you need consistent control.
Choosing scroll physics
The following behavior selects bouncing physics for iOS and macOS and clamping physics for Android and other platforms:
import 'package:flutter/material.dart';
class PlatformScrollBehavior extends MaterialScrollBehavior {
const PlatformScrollBehavior();
@override
ScrollPhysics getScrollPhysics(BuildContext context) {
final platform = Theme.of(context).platform;
final isApplePlatform =
platform == TargetPlatform.iOS || platform == TargetPlatform.macOS;
if (isApplePlatform) {
return const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
);
}
return const ClampingScrollPhysics();
}
}
void main() {
runApp(
MaterialApp(
scrollBehavior: const PlatformScrollBehavior(),
home: const ItemsPage(),
),
);
}
class ItemsPage extends StatelessWidget {
const ItemsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Items')),
body: ListView.builder(
itemCount: 40,
itemBuilder: (context, index) {
return ListTile(title: Text('Item ${index + 1}'));
},
),
);
}
}AlwaysScrollableScrollPhysics allows the list to respond to a drag even when its content does not fill the viewport. This can make the iOS bounce visible on short lists. Remove it when that behavior is undesirable.
Extending MaterialScrollBehavior preserves Flutter's other behavior, such as desktop scrollbar support. Replacing the entire scroll behavior without considering those features can make mouse and keyboard scrolling less useful.
Applying behavior locally
A global scrollBehavior affects scrollables below the application. Use ScrollConfiguration when only one screen or section needs a different rule:
ScrollConfiguration(
behavior: const PlatformScrollBehavior(),
child: ListView.builder(
itemCount: 20,
itemBuilder: (context, index) => Text('Row $index'),
),
)Use local configuration for an intentional exception, such as a horizontally scrolling carousel. Avoid assigning different physics to every list without a design reason. Mixed scrolling rules can make one app feel inconsistent.
Exercise
Why might a custom scroll behavior extend MaterialScrollBehavior instead of ScrollBehavior directly?
Show answer
It keeps useful inherited behavior, such as desktop scrollbar handling, while changing only the physics decision.