Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
c06995a3
Commit
c06995a3
authored
Sep 30, 2015
by
Hans Muller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clamp the snapped scroll simulation; Scrollable settle() and fling() return a Future
parent
946da101
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
45 deletions
+79
-45
animation.dart
packages/flutter/lib/animation.dart
+1
-0
scrollable.dart
packages/flutter/lib/src/fn3/scrollable.dart
+46
-35
snap_scrolling_test.dart
packages/unit/test/widget/snap_scrolling_test.dart
+32
-10
No files found.
packages/flutter/lib/animation.dart
View file @
c06995a3
...
...
@@ -10,6 +10,7 @@ library animation;
export
'src/animation/animated_simulation.dart'
;
export
'src/animation/animated_value.dart'
;
export
'src/animation/animation_performance.dart'
;
export
'src/animation/clamped_simulation.dart'
;
export
'src/animation/curves.dart'
;
export
'src/animation/forces.dart'
;
export
'src/animation/scheduler.dart'
;
...
...
packages/flutter/lib/src/fn3/scrollable.dart
View file @
c06995a3
...
...
@@ -132,34 +132,45 @@ abstract class ScrollableState<T extends Scrollable> extends State<T> {
return
offset
>=
behavior
.
minScrollOffset
&&
offset
<
behavior
.
maxScrollOffset
;
}
double
_alignedScrollSnapOffset
(
double
offset
)
{
return
config
.
snapOffsetCallback
(
offset
+
config
.
snapAlignmentOffset
)
-
config
.
snapAlignmentOffset
;
Simulation
_createFlingSimulation
(
double
velocity
)
{
return
scrollBehavior
.
createFlingScrollSimulation
(
scrollOffset
,
velocity
)
;
}
void
_startToEndAnimation
({
double
velocity
})
{
_stopAnimations
();
Simulation
_createSnapSimulation
(
double
velocity
)
{
if
(
velocity
==
null
||
config
.
snapOffsetCallback
==
null
||
!
_scrollOffsetIsInBounds
(
scrollOffset
))
return
null
;
if
(
velocity
!=
null
&&
config
.
snapOffsetCallback
!=
null
&&
_scrollOffsetIsInBounds
(
scrollOffset
))
{
Simulation
simulation
=
scrollBehavior
.
createFlingScrollSimulation
(
scrollOffset
,
velocity
);
if
(
simulation
==
null
)
return
;
double
endScrollOffset
=
simulation
.
x
(
double
.
INFINITY
);
if
(!
endScrollOffset
.
isNaN
)
{
double
alignedScrollOffset
=
_alignedScrollSnapOffset
(
endScrollOffset
);
if
(
_scrollOffsetIsInBounds
(
alignedScrollOffset
))
{
double
snapVelocity
=
velocity
.
abs
()
*
(
alignedScrollOffset
-
scrollOffset
).
sign
;
Simulation
toSnapSimulation
=
scrollBehavior
.
createSnapScrollSimulation
(
scrollOffset
,
alignedScrollOffset
,
snapVelocity
);
_toEndAnimation
.
start
(
toSnapSimulation
);
return
;
}
}
}
Simulation
simulation
=
_createFlingSimulation
(
velocity
);
if
(
simulation
==
null
)
return
null
;
double
endScrollOffset
=
simulation
.
x
(
double
.
INFINITY
);
if
(
endScrollOffset
.
isNaN
)
return
null
;
double
snappedScrollOffset
=
config
.
snapOffsetCallback
(
endScrollOffset
+
config
.
snapAlignmentOffset
);
double
alignedScrollOffset
=
snappedScrollOffset
-
config
.
snapAlignmentOffset
;
if
(!
_scrollOffsetIsInBounds
(
alignedScrollOffset
))
return
null
;
Simulation
simulation
=
scrollBehavior
.
createFlingScrollSimulation
(
scrollOffset
,
velocity
??
0.0
);
double
snapVelocity
=
velocity
.
abs
()
*
(
alignedScrollOffset
-
scrollOffset
).
sign
;
Simulation
toSnapSimulation
=
scrollBehavior
.
createSnapScrollSimulation
(
scrollOffset
,
alignedScrollOffset
,
snapVelocity
);
if
(
toSnapSimulation
==
null
)
return
null
;
double
offsetMin
=
math
.
min
(
scrollOffset
,
alignedScrollOffset
);
double
offsetMax
=
math
.
max
(
scrollOffset
,
alignedScrollOffset
);
return
new
ClampedSimulation
(
toSnapSimulation
,
xMin:
offsetMin
,
xMax:
offsetMax
);
}
Future
_startToEndAnimation
({
double
velocity
})
{
_stopAnimations
();
Simulation
simulation
=
_createSnapSimulation
(
velocity
)
??
_createFlingSimulation
(
velocity
??
0.0
);
if
(
simulation
==
null
)
return
;
_toEndAnimation
.
start
(
simulation
);
return
new
Future
.
value
()
;
return
_toEndAnimation
.
start
(
simulation
);
}
void
dispose
()
{
...
...
@@ -195,16 +206,16 @@ abstract class ScrollableState<T extends Scrollable> extends State<T> {
return
scrollTo
(
newScrollOffset
,
duration:
duration
,
curve:
curve
);
}
void
fling
(
Offset
velocity
)
{
if
(
velocity
!=
Offset
.
zero
)
{
_startToEndAnimation
(
velocity:
_scrollVelocity
(
velocity
));
}
else
if
(!
_toEndAnimation
.
isAnimating
&&
(
_toOffsetAnimation
==
null
||
!
_toOffsetAnimation
.
isAnimating
))
{
settleScrollOffset
();
}
Future
fling
(
Offset
velocity
)
{
if
(
velocity
!=
Offset
.
zero
)
return
_startToEndAnimation
(
velocity:
_scrollVelocity
(
velocity
));
if
(!
_toEndAnimation
.
isAnimating
&&
(
_toOffsetAnimation
==
null
||
!
_toOffsetAnimation
.
isAnimating
))
return
settleScrollOffset
();
return
new
Future
.
value
();
}
void
settleScrollOffset
()
{
_startToEndAnimation
();
Future
settleScrollOffset
()
{
return
_startToEndAnimation
();
}
double
_scrollVelocity
(
sky
.
Offset
velocity
)
{
...
...
@@ -623,12 +634,12 @@ class PageableListState<T> extends ScrollableListState<T, PageableList<T>> {
.
clamp
(
scrollBehavior
.
minScrollOffset
,
scrollBehavior
.
maxScrollOffset
);
}
void
fling
(
sky
.
Offset
velocity
)
{
Future
fling
(
sky
.
Offset
velocity
)
{
double
scrollVelocity
=
_scrollVelocity
(
velocity
);
double
newScrollOffset
=
_snapScrollOffset
(
scrollOffset
+
scrollVelocity
.
sign
*
config
.
itemExtent
)
.
clamp
(
_snapScrollOffset
(
scrollOffset
-
config
.
itemExtent
/
2.0
),
_snapScrollOffset
(
scrollOffset
+
config
.
itemExtent
/
2.0
));
scrollTo
(
newScrollOffset
,
duration:
config
.
duration
,
curve:
config
.
curve
).
then
(
_notifyPageChanged
);
return
scrollTo
(
newScrollOffset
,
duration:
config
.
duration
,
curve:
config
.
curve
).
then
(
_notifyPageChanged
);
}
int
get
currentPage
=>
(
scrollOffset
/
config
.
itemExtent
).
floor
()
%
itemCount
;
...
...
@@ -638,8 +649,8 @@ class PageableListState<T> extends ScrollableListState<T, PageableList<T>> {
config
.
onPageChanged
(
currentPage
);
}
void
settleScrollOffset
()
{
scrollTo
(
_snapScrollOffset
(
scrollOffset
),
duration:
config
.
duration
,
curve:
config
.
curve
).
then
(
_notifyPageChanged
);
Future
settleScrollOffset
()
{
return
scrollTo
(
_snapScrollOffset
(
scrollOffset
),
duration:
config
.
duration
,
curve:
config
.
curve
).
then
(
_notifyPageChanged
);
}
}
...
...
packages/unit/test/widget/snap_scrolling_test.dart
View file @
c06995a3
...
...
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'dart:async'
;
import
'package:quiver/testing/async.dart'
;
import
'package:sky/src/fn3.dart'
;
import
'package:test/test.dart'
;
...
...
@@ -49,11 +51,11 @@ void set scrollOffset(double value) {
scrollableState
.
scrollTo
(
value
);
}
void
fling
(
double
velocity
)
{
Future
fling
(
double
velocity
)
{
Offset
velocityOffset
=
scrollDirection
==
ScrollDirection
.
vertical
?
new
Offset
(
0.0
,
velocity
)
:
new
Offset
(
velocity
,
0.0
);
scrollableState
.
fling
(
velocityOffset
);
return
scrollableState
.
fling
(
velocityOffset
);
}
void
main
(
)
{
...
...
@@ -66,7 +68,7 @@ void main() {
expect
(
scrollOffset
,
0.0
);
double
t0
=
0.0
;
int
dt
=
1
000
;
int
dt
=
2
000
;
new
FakeAsync
().
run
((
async
)
{
fling
(-
800.0
);
tester
.
pumpFrameWithoutChange
(
t0
);
// Start the scheduler at 0.0
...
...
@@ -82,10 +84,10 @@ void main() {
expect
(
scrollOffset
,
0.0
);
double
t0
=
0.0
;
int
dt
=
1
000
;
int
dt
=
2
000
;
new
FakeAsync
().
run
((
async
)
{
fling
(-
2000.0
);
tester
.
pumpFrameWithoutChange
(
t0
);
// Start the scheduler at 0.0
tester
.
pumpFrameWithoutChange
(
t0
);
tester
.
pumpFrameWithoutChange
(
t0
+
dt
);
async
.
elapse
(
new
Duration
(
milliseconds:
dt
));
expect
(
scrollOffset
,
closeTo
(
400.0
,
1.0
));
...
...
@@ -94,14 +96,14 @@ void main() {
test
(
'ScrollableList snap scrolling, fling(800)'
,
()
{
scrollOffset
=
400.0
;
tester
.
pumpFrameWithoutChange
(
1000.0
);
tester
.
pumpFrameWithoutChange
();
expect
(
scrollOffset
,
400.0
);
double
t0
=
0.0
;
int
dt
=
2000
;
new
FakeAsync
().
run
((
async
)
{
fling
(
800.0
);
tester
.
pumpFrameWithoutChange
(
t0
);
// Start the scheduler at 0.0
tester
.
pumpFrameWithoutChange
(
t0
);
tester
.
pumpFrameWithoutChange
(
t0
+
dt
);
async
.
elapse
(
new
Duration
(
milliseconds:
dt
));
expect
(
scrollOffset
,
closeTo
(
0.0
,
1.0
));
...
...
@@ -110,18 +112,38 @@ void main() {
test
(
'ScrollableList snap scrolling, fling(2000)'
,
()
{
scrollOffset
=
800.0
;
tester
.
pumpFrameWithoutChange
(
1000.0
);
tester
.
pumpFrameWithoutChange
();
expect
(
scrollOffset
,
800.0
);
double
t0
=
0.0
;
int
dt
=
15
00
;
int
dt
=
20
00
;
new
FakeAsync
().
run
((
async
)
{
fling
(
2000.0
);
tester
.
pumpFrameWithoutChange
(
t0
);
// Start the scheduler at 0.0
tester
.
pumpFrameWithoutChange
(
t0
);
tester
.
pumpFrameWithoutChange
(
t0
+
dt
);
async
.
elapse
(
new
Duration
(
milliseconds:
dt
));
expect
(
scrollOffset
,
closeTo
(
200.0
,
1.0
));
});
});
test
(
'ScrollableList snap scrolling, fling(2000).then()'
,
()
{
scrollOffset
=
800.0
;
tester
.
pumpFrameWithoutChange
();
expect
(
scrollOffset
,
800.0
);
double
t0
=
0.0
;
int
dt
=
2000
;
bool
completed
=
false
;
new
FakeAsync
().
run
((
async
)
{
fling
(
2000.0
).
then
((
_
)
{
completed
=
true
;
expect
(
scrollOffset
,
closeTo
(
200.0
,
1.0
));
});
tester
.
pumpFrameWithoutChange
(
t0
);
tester
.
pumpFrameWithoutChange
(
t0
+
dt
);
async
.
elapse
(
new
Duration
(
milliseconds:
dt
));
expect
(
completed
,
true
);
});
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment