Skip to content

Commit e4dece3

Browse files
authored
Ramin/animate_to_current_tick (#2)
* Animating to current tick when arrow button is clicked * Animation duration relative to distance so it animate by a similar pace every time * Using a Tween Animation instead * idea/codeStyle in gitignore * Using only AnimationController again and have animation duration relative * Removed relative duration. * Hiding scrollToNow button while already animating * Adding the animation time to its upperbound * A separate getter to check arrow button be visible * Minor refactor
1 parent e95ac0a commit e4dece3

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ captures/
6060
.idea/modules.xml
6161
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
6262
.idea/navEditor.xml
63+
.idea/codeStyles
6364

6465
# Keystore files
6566
# Uncomment the following lines if you do not want to check your keystore files in.

lib/src/chart.dart

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ class _ChartState extends State<Chart> with TickerProviderStateMixin {
8080
AnimationController _loadingAnimationController;
8181
AnimationController _topBoundQuoteAnimationController;
8282
AnimationController _bottomBoundQuoteAnimationController;
83+
AnimationController _rightEpochAnimationController;
8384
Animation _currentTickAnimation;
8485
Animation _currentTickBlinkAnimation;
8586

8687
bool get _shouldAutoPan => rightBoundEpoch > nowEpoch;
8788

89+
bool get _arrowButtonBeVisible =>
90+
!_shouldAutoPan &&
91+
!(_rightEpochAnimationController?.isAnimating ?? false);
92+
8893
double get _topBoundQuote => _topBoundQuoteAnimationController.value;
8994

9095
double get _bottomBoundQuote => _bottomBoundQuoteAnimationController.value;
@@ -129,19 +134,20 @@ class _ChartState extends State<Chart> with TickerProviderStateMixin {
129134

130135
if (oldGranularity != newGranularity) {
131136
msPerPx = _getDefaultScale(newGranularity);
132-
_scrollToNow();
137+
rightBoundEpoch = nowEpoch + _pxToMs(maxCurrentTickOffset);
133138
} else {
134139
_onNewTick();
135140
}
136141
}
137142

138143
@override
139144
void dispose() {
140-
_currentTickAnimationController.dispose();
141-
_currentTickBlinkingController.dispose();
142-
_loadingAnimationController.dispose();
143-
_topBoundQuoteAnimationController.dispose();
144-
_bottomBoundQuoteAnimationController.dispose();
145+
_rightEpochAnimationController?.dispose();
146+
_currentTickAnimationController?.dispose();
147+
_currentTickBlinkingController?.dispose();
148+
_loadingAnimationController?.dispose();
149+
_topBoundQuoteAnimationController?.dispose();
150+
_bottomBoundQuoteAnimationController?.dispose();
145151
super.dispose();
146152
}
147153

@@ -170,6 +176,16 @@ class _ChartState extends State<Chart> with TickerProviderStateMixin {
170176
_setupCurrentTickAnimation();
171177
_setupBlinkingAnimation();
172178
_setupBoundsAnimation();
179+
_setupRightEpochAnimation();
180+
}
181+
182+
void _setupRightEpochAnimation() {
183+
_rightEpochAnimationController = AnimationController.unbounded(
184+
vsync: this,
185+
value: rightBoundEpoch.toDouble(),
186+
)..addListener(() {
187+
rightBoundEpoch = _rightEpochAnimationController.value.toInt();
188+
});
173189
}
174190

175191
void _setupCurrentTickAnimation() {
@@ -333,7 +349,7 @@ class _ChartState extends State<Chart> with TickerProviderStateMixin {
333349
);
334350
}),
335351
),
336-
if (!_shouldAutoPan)
352+
if (_arrowButtonBeVisible)
337353
Positioned(
338354
bottom: 30 + timeLabelsAreaHeight,
339355
right: 30 + quoteLabelsAreaWidth,
@@ -447,7 +463,20 @@ class _ChartState extends State<Chart> with TickerProviderStateMixin {
447463
}
448464

449465
void _scrollToNow() {
450-
rightBoundEpoch = nowEpoch + _pxToMs(maxCurrentTickOffset);
466+
final animationMsDuration = 600;
467+
final lowerBound = rightBoundEpoch.toDouble();
468+
final upperBound = nowEpoch +
469+
_pxToMs(maxCurrentTickOffset).toDouble() +
470+
animationMsDuration;
471+
472+
if (upperBound > lowerBound) {
473+
_rightEpochAnimationController.value = lowerBound;
474+
_rightEpochAnimationController.animateTo(
475+
upperBound,
476+
curve: Curves.easeOut,
477+
duration: Duration(milliseconds: animationMsDuration),
478+
);
479+
}
451480
}
452481

453482
void _onScaleAndPanEnd(ScaleEndDetails details) {

0 commit comments

Comments
 (0)