Skip to content

Commit ccebb43

Browse files
Trefa1998rayliverified
authored andcommitted
Refactored scroll_behavior.dart to add const constructors and use TargetPlatform dragDevices as a baseline even if dragWithMouse enabled
1 parent 0c253a4 commit ccebb43

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/src/utils/scroll_behavior.dart

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'package:flutter/gestures.dart';
22
import 'package:flutter/widgets.dart';
33

44
class BouncingScrollBehavior extends ScrollBehavior {
5+
const BouncingScrollBehavior();
6+
57
// Disable overscroll glow.
68
@override
79
Widget buildOverscrollIndicator(
@@ -35,20 +37,23 @@ class BouncingScrollWrapper extends StatelessWidget {
3537

3638
@override
3739
Widget build(BuildContext context) {
40+
ScrollBehavior scrollBehavior = const BouncingScrollBehavior();
41+
if (dragWithMouse) {
42+
// If mouse dragging is desired, add it to the drag devices.
43+
scrollBehavior = scrollBehavior.copyWith(
44+
dragDevices: scrollBehavior.dragDevices..add(PointerDeviceKind.mouse),
45+
);
46+
}
3847
return ScrollConfiguration(
39-
behavior: BouncingScrollBehavior().copyWith(
40-
dragDevices: dragWithMouse
41-
? {
42-
PointerDeviceKind.touch,
43-
PointerDeviceKind.mouse,
44-
}
45-
: null),
48+
behavior: scrollBehavior,
4649
child: child,
4750
);
4851
}
4952
}
5053

5154
class ClampingScrollBehavior extends ScrollBehavior {
55+
const ClampingScrollBehavior();
56+
5257
// Disable overscroll glow.
5358
@override
5459
Widget buildOverscrollIndicator(
@@ -77,14 +82,15 @@ class ClampingScrollWrapper extends StatelessWidget {
7782

7883
@override
7984
Widget build(BuildContext context) {
85+
ScrollBehavior scrollBehavior = const ClampingScrollBehavior();
86+
if (dragWithMouse) {
87+
// If mouse dragging is desired, add it to the drag devices.
88+
scrollBehavior = scrollBehavior.copyWith(
89+
dragDevices: scrollBehavior.dragDevices..add(PointerDeviceKind.mouse),
90+
);
91+
}
8092
return ScrollConfiguration(
81-
behavior: ClampingScrollBehavior().copyWith(
82-
dragDevices: dragWithMouse
83-
? {
84-
PointerDeviceKind.touch,
85-
PointerDeviceKind.mouse,
86-
}
87-
: null),
93+
behavior: scrollBehavior,
8894
child: child,
8995
);
9096
}

0 commit comments

Comments
 (0)