Default multitouch scrolling
Summary
#ScrollBehavior
s now allow or disallow scrolling speeds to be affected by the
number of pointers on the screen. ScrollBehavior.multitouchDragStrategy
, by
default, prevents multiple pointers interacting wih the scrollable at the same
time from affecting the speed of scrolling.
Context
#Prior to this change, for each pointer dragging a Scrollable
widget, the
scroll speed would increase. This did not match platform expectations when
interacting with Flutter applications.
Now, the inherited ScrollBehavior
manages how multiple pointers affect
scrolling widgets as specified by ScrollBehavior.multitouchDragStrategy
. This
enum, MultitouchDragStrategy
, can also be configured for the prior behavior.
Description of change
#This change fixed the unexpected ability to increase scroll speeds by dragging with more than one finger.
If you have relied on the previous behavior in your application, there are several ways to control and configure this feature.
-
Extend
ScrollBehavior
,MaterialScrollBehavior
, orCupertinoScrollBehavior
to modify the default behavior, overridingScrollBehavior.multitouchDragStrategy
.- With your own
ScrollBehavior
, you can apply it app-wide by settingMaterialApp.scrollBehavior
orCupertinoApp.scrollBehavior
. - Or, if you wish to only apply it to specific widgets, add a
ScrollConfiguration
above the widget in question with your customScrollBehavior
.
- With your own
Your scrollable widgets then inherit and reflect this behavior.
- Instead of creating your own
ScrollBehavior
, another option for changing the default behavior is to copy the existingScrollBehavior
, and set differentmultitouchDragStrategy
.- Create a
ScrollConfiguration
in your widget tree, and provide a modified copy of the existingScrollBehavior
in the current context usingcopyWith
.
- Create a
To accommodate the new configuration
DragGestureRecognizer
was updated to support MultitouchDragStrategy
as well
in other dragging contexts.
Migration guide
#Setting a custom ScrollBehavior
for your application
#Code before migration:
MaterialApp(
// ...
);
Code after migration:
class MyCustomScrollBehavior extends MaterialScrollBehavior {
// Override behavior methods and getters like multitouchDragStrategy
@override
MultitouchDragStrategy getMultitouchDragStrategy(BuildContext context) => MultitouchDragStrategy.sumAllPointers;
}
// Set ScrollBehavior for an entire application.
MaterialApp(
scrollBehavior: MyCustomScrollBehavior(),
// ...
);
Setting a custom ScrollBehavior
for a specific widget
#Code before migration:
final ScrollController controller = ScrollController();
ListView.builder(
controller: controller,
itemBuilder: (BuildContext context, int index) {
return Text('Item $index');
},
);
Code after migration:
class MyCustomScrollBehavior extends MaterialScrollBehavior {
// Override behavior methods and getters like multitouchDragStrategy
@override
MultitouchDragStrategy getMultitouchDragStrategy(BuildContext context) => MultitouchDragStrategy.sumAllPointers;
}
// ScrollBehavior can be set for a specific widget.
final ScrollController controller = ScrollController();
ScrollConfiguration(
behavior: MyCustomScrollBehavior(),
child: ListView.builder(
controller: controller,
itemBuilder: (BuildContext context, int index) {
return Text('Item $index');
},
),
);
Copy and modify existing ScrollBehavior
#Code before migration:
final ScrollController controller = ScrollController();
ListView.builder(
controller: controller,
itemBuilder: (BuildContext context, int index) {
return Text('Item $index');
},
);
Code after migration:
// ScrollBehavior can be copied and adjusted.
final ScrollController controller = ScrollController();
ScrollConfiguration(
behavior: ScrollConfiguration.of(context).copyWith(
multitouchDragStrategy: MultitouchDragStrategy.sumAllPointers,
),
child: ListView.builder(
controller: controller,
itemBuilder: (BuildContext context, int index) {
return Text('Item $index');
},
),
);
Timeline
#Landed in version: 3.18.0-4.0.pre
In stable release: 3.19.0
References
#API documentation:
ScrollConfiguration
ScrollBehavior
MaterialScrollBehavior
CupertinoScrollBehavior
MultitouchDragStrategy
DragGestureRecognizer
Relevant issue:
Relevant PRs:
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2024-10-09。 查看文档源码 或者 为本页面内容提出建议。