-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[article] snap to next article in related section
- Loading branch information
Showing
3 changed files
with
139 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* This file is part of BoxBox (https://github.com/BrightDV/BoxBox). | ||
* | ||
* BoxBox is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* BoxBox is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with BoxBox. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright (c) 2022-2023, BrightDV | ||
*/ | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
// Originally form https://github.com/Sangwan5688/BlackHole/blob/main/lib/CustomWidgets/custom_physics.dart | ||
|
||
class PagingScrollPhysics extends ScrollPhysics { | ||
final double itemDimension; | ||
|
||
const PagingScrollPhysics({required this.itemDimension, super.parent}); | ||
|
||
@override | ||
PagingScrollPhysics applyTo(ScrollPhysics? ancestor) { | ||
return PagingScrollPhysics( | ||
itemDimension: itemDimension, | ||
parent: buildParent(ancestor), | ||
); | ||
} | ||
|
||
Tolerance toleranceFor(ScrollMetrics metrics) { | ||
double devicePixelRatio = | ||
MediaQueryData.fromWindow(WidgetsBinding.instance.window) | ||
.devicePixelRatio / | ||
WidgetsBinding.instance.window.physicalSize.height; | ||
return Tolerance( | ||
velocity: 1.0 / (50 * devicePixelRatio), // logical pixels per second | ||
distance: 1.0 / devicePixelRatio, // logical pixels | ||
); | ||
} | ||
|
||
double _getPage(ScrollMetrics position) { | ||
return position.pixels / itemDimension; | ||
} | ||
|
||
double _getPixels(double page) { | ||
return page * itemDimension; | ||
} | ||
|
||
double _getTargetPixels( | ||
ScrollMetrics position, | ||
Tolerance tolerance, | ||
double velocity, | ||
) { | ||
double page = _getPage(position); | ||
if (velocity < -tolerance.velocity) { | ||
page -= 0.5; | ||
} else if (velocity > tolerance.velocity) { | ||
page += 0.5; | ||
} | ||
return _getPixels(page.roundToDouble()); | ||
} | ||
|
||
@override | ||
Simulation? createBallisticSimulation( | ||
ScrollMetrics position, | ||
double velocity, | ||
) { | ||
if ((velocity <= 0.0 && position.pixels <= position.minScrollExtent) || | ||
(velocity >= 0.0 && position.pixels >= position.maxScrollExtent)) { | ||
return super.createBallisticSimulation(position, velocity); | ||
} | ||
final Tolerance tolerance = toleranceFor(position); | ||
final double target = _getTargetPixels(position, tolerance, velocity); | ||
if (target != position.pixels) { | ||
return ScrollSpringSimulation( | ||
spring, | ||
position.pixels, | ||
target, | ||
velocity, | ||
tolerance: tolerance, | ||
); | ||
} | ||
return null; | ||
} | ||
|
||
@override | ||
bool get allowImplicitScrolling => false; | ||
} |