Skip to content

Commit

Permalink
fixed progressive scroll from linear to curved
Browse files Browse the repository at this point in the history
  • Loading branch information
Sivan22 committed Jul 29, 2024
1 parent 2b51119 commit 1cafd3e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
8 changes: 5 additions & 3 deletions lib/screens/combined_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:otzaria/widgets/progressive_scrolling.dart';
import 'package:otzaria/widgets/progressive_scrolling.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:flutter_settings_screens/flutter_settings_screens.dart';
import 'package:otzaria/widgets/commentary_list.dart';
Expand Down Expand Up @@ -32,9 +33,10 @@ class _CombinedViewState extends State<CombinedView> {
FocusNode focusNode = FocusNode();

Widget buildKeyboardListener() {
return ProgressiveScrollWrapper(
maxSpeed: 5000.0,
acceleration: 10.0,
return ProgressiveScroll(
maxSpeed: 10000.0,
curve: 10.0,
accelerationFactor: 5,
scrollController: widget.tab.scrollOffsetController,
child: buildOuterList());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/main_window_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class MainWindowScreenState extends State<MainWindowScreen>
));
return jsonDecode(data.body)["body"];
},
currentVersion: '0.1.8-dev.0',
currentVersion: '0.1.8-dev.2',
updateChipBuilder: flatChip,

callback: (status) {},
Expand Down
67 changes: 31 additions & 36 deletions lib/screens/simple_book_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,36 @@ class _SimpleBookViewState extends State<SimpleBookView> {

@override
Widget build(BuildContext context) {
return ProgressiveScrollWrapper(
scrollController: widget.tab.scrollOffsetController,
maxSpeed: 50.0,
acceleration: 0.5,
child: SelectionArea(
key: PageStorageKey(widget.tab),
child: ScrollablePositionedList.builder(
initialScrollIndex: widget.tab.index,
itemPositionsListener: widget.tab.positionsListener,
itemScrollController: widget.tab.scrollController,
scrollOffsetController: widget.tab.scrollOffsetController,
itemCount: widget.data.length,
itemBuilder: (context, index) {
return ValueListenableBuilder(
valueListenable: widget.tab.removeNikud,
builder: (context, removeNikud, child) => InkWell(
onTap: () => widget.tab.selectedIndex.value = index,
child: Html(
//remove nikud if needed
data: removeNikud
? highLight(removeVolwels(widget.data[index]),
widget.tab.searchTextController.text)
: highLight(widget.data[index],
widget.tab.searchTextController.text),
style: {
'body': Style(
fontSize: FontSize(widget.textSize),
fontFamily:
Settings.getValue('key-font-family') ??
'candara',
textAlign: TextAlign.justify),
}),
),
);
}),
));
return SelectionArea(
key: PageStorageKey(widget.tab),
child: ScrollablePositionedList.builder(
initialScrollIndex: widget.tab.index,
itemPositionsListener: widget.tab.positionsListener,
itemScrollController: widget.tab.scrollController,
scrollOffsetController: widget.tab.scrollOffsetController,
itemCount: widget.data.length,
itemBuilder: (context, index) {
return ValueListenableBuilder(
valueListenable: widget.tab.removeNikud,
builder: (context, removeNikud, child) => InkWell(
onTap: () => widget.tab.selectedIndex.value = index,
child: Html(
//remove nikud if needed
data: removeNikud
? highLight(removeVolwels(widget.data[index]),
widget.tab.searchTextController.text)
: highLight(widget.data[index],
widget.tab.searchTextController.text),
style: {
'body': Style(
fontSize: FontSize(widget.textSize),
fontFamily:
Settings.getValue('key-font-family') ?? 'candara',
textAlign: TextAlign.justify),
}),
),
);
}),
);
}
}
30 changes: 21 additions & 9 deletions lib/widgets/progressive_scrolling.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:math';

import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

class ProgressiveScrollWrapper extends StatefulWidget {
class ProgressiveScroll extends StatefulWidget {
final Widget child;
final ScrollOffsetController scrollController;
final double maxSpeed;
final double acceleration;
final double accelerationFactor;
final double curve;

ProgressiveScrollWrapper({
ProgressiveScroll({
Key? key,
required this.child,
required this.scrollController,
this.maxSpeed = 50.0,
this.acceleration = 0.5,
this.maxSpeed = 5000.0,
this.accelerationFactor = 0.1,
this.curve = 2.0,
}) : super(key: key);

@override
_ProgressiveScrollWrapperState createState() =>
_ProgressiveScrollWrapperState();
_ProgressiveScrollState createState() => _ProgressiveScrollState();
}

class _ProgressiveScrollWrapperState extends State<ProgressiveScrollWrapper> {
class _ProgressiveScrollState extends State<ProgressiveScroll> {
double _scrollSpeed = 0;
bool _isKeyPressed = false;
int _scrollDirection = 0; // 1 for down, -1 for up, 0 for no scroll
double _timePressedInSeconds = 0;

@override
void initState() {
Expand All @@ -35,11 +39,15 @@ class _ProgressiveScrollWrapperState extends State<ProgressiveScrollWrapper> {
void _startScrolling() {
Future.delayed(Duration(milliseconds: 16), () {
if (_isKeyPressed) {
_scrollSpeed = (_scrollSpeed + widget.acceleration * _scrollDirection)
_timePressedInSeconds += 0.016; // 16 milliseconds in seconds
double t = _timePressedInSeconds;
double curvedT = exp(widget.curve * t);
_scrollSpeed = (curvedT * widget.accelerationFactor * _scrollDirection)
.clamp(-widget.maxSpeed, widget.maxSpeed);
print('scroll speed: $_scrollSpeed');
} else {
_scrollSpeed = 0;
_timePressedInSeconds = 0;
}

if (_scrollSpeed != 0) {
Expand Down Expand Up @@ -68,6 +76,10 @@ class _ProgressiveScrollWrapperState extends State<ProgressiveScrollWrapper> {
if (event.logicalKey == LogicalKeyboardKey.arrowDown ||
event.logicalKey == LogicalKeyboardKey.arrowUp) {
_isKeyPressed = false;
widget.scrollController.animateScroll(
offset: 100.0 * _scrollDirection,
duration: const Duration(milliseconds: 300),
);
_scrollDirection = 0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msix_config:
display_name: אוצריא
publisher_display_name: sivan22
identity_name: sivan22.Otzaria
msix_version: 0.1.8.0
msix_version: 0.1.8.2
logo_path: assets/icon/icon.png
publisher: CN=sivan22, O=sivan22, C=IL
certificate_path: C:\dev\sivan22.pfx
Expand All @@ -33,7 +33,7 @@ msix_config:
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 0.1.8-dev.1
version: 0.1.8-dev.2

environment:
sdk: ">=3.2.6 <4.0.0"
Expand Down

0 comments on commit 1cafd3e

Please sign in to comment.