Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/src/scroll_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ import 'scroll_translation.dart';
class ScrollProvider extends ChangeNotifier {
final ScrollTranslation st;

ScrollProvider(this.st, {ScrollPhysics? phys}) {
/// Default scroll physics for mobile devices.
final ScrollPhysics mobileScrollPhysics;

ScrollProvider(
this.st, {
required this.mobileScrollPhysics,
ScrollPhysics? phys,
}) {
// use ParentListener physics if passed
physics = phys ??=
(Platform.isWindows || Platform.isLinux || Platform.isMacOS)
? kDesktopPhysics
: kMobilePhysics;
: mobileScrollPhysics;
}

late ScrollPhysics physics;
Expand All @@ -35,10 +42,15 @@ class ScrollProvider extends ChangeNotifier {
///
/// view example here ::
class ParentPhysicsProvider with ChangeNotifier {
/// Default scroll physics for mobile devices.
final ScrollPhysics mobileScrollPhysics;

ParentPhysicsProvider({required this.mobileScrollPhysics});

late ScrollPhysics physics =
(Platform.isWindows || Platform.isLinux || Platform.isMacOS)
? kDesktopPhysics
: kMobilePhysics;
: mobileScrollPhysics;

setPhysics(ScrollPhysics physics) {
this.physics = physics;
Expand Down
35 changes: 27 additions & 8 deletions lib/src/source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import 'package:provider/provider.dart';
import 'scroll_state.dart';
import 'scroll_translation.dart';

const kMobilePhysics = BouncingScrollPhysics();
const kDefaultMobilePhysics = BouncingScrollPhysics();
const kDesktopPhysics = NeverScrollableScrollPhysics();

class DynMouseScroll extends StatelessWidget {
/// Scroll physics for mobile devices. Default is [BouncingScrollPhysics].
final ScrollPhysics mobileScrollPhysics;

/// Optional, where [controller] should set its initial scroll position.
/// Defaults to 0.
final double initialOffset;
Expand Down Expand Up @@ -69,6 +72,7 @@ class DynMouseScroll extends StatelessWidget {
const DynMouseScroll({
Key? key,
required this.builder,
this.mobileScrollPhysics = kDefaultMobilePhysics,
this.hasParentListener = false,
this.initialOffset = 0,
this.animationCurve = Curves.linear,
Expand Down Expand Up @@ -128,9 +132,15 @@ class DynMouseScroll extends StatelessWidget {
return ChangeNotifierProvider<ScrollProvider>(
lazy: false,
create: (_) => (hasParentListener)
? ScrollProvider(scrollTranslation,
phys: context.read<ParentPhysicsProvider>().physics)
: ScrollProvider(scrollTranslation),
? ScrollProvider(
scrollTranslation,
mobileScrollPhysics: mobileScrollPhysics,
phys: context.read<ParentPhysicsProvider>().physics,
)
: ScrollProvider(
scrollTranslation,
mobileScrollPhysics: mobileScrollPhysics,
),
builder: (context, _) {
ScrollProvider sp = context.read<ScrollProvider>();

Expand All @@ -149,7 +159,7 @@ class DynMouseScroll extends StatelessWidget {
onPointerSignal: (t) {
// Ensure correct physics
if (t.kind == PointerDeviceKind.mouse &&
physics == kMobilePhysics) {
physics == mobileScrollPhysics) {
physicsProvider.setPhysics(kDesktopPhysics);
}

Expand All @@ -163,7 +173,7 @@ class DynMouseScroll extends StatelessWidget {
// Ensure correct physics
if (e.kind == PointerDeviceKind.touch &&
physics == kDesktopPhysics) {
physicsProvider.setPhysics(kMobilePhysics);
physicsProvider.setPhysics(mobileScrollPhysics);
}
},
child: builder(context, controller, physics));
Expand All @@ -173,12 +183,21 @@ class DynMouseScroll extends StatelessWidget {

class ParentListener extends StatelessWidget {
final Widget child;
const ParentListener({super.key, required this.child});

/// Scroll physics for mobile devices. Default is [BouncingScrollPhysics].
final ScrollPhysics mobileScrollPhysics;

const ParentListener({
super.key,
this.mobileScrollPhysics = kDefaultMobilePhysics,
required this.child,
});

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ParentPhysicsProvider>(
create: (_) => ParentPhysicsProvider(),
create: (_) =>
ParentPhysicsProvider(mobileScrollPhysics: mobileScrollPhysics),
child: child,
);
}
Expand Down