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
17c50693
Unverified
Commit
17c50693
authored
Feb 01, 2018
by
Ian Hickson
Committed by
GitHub
Feb 01, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rationalize the ScrollMetrics cloning logic (#14281)
parent
d9ef7df9
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
175 additions
and
34 deletions
+175
-34
list_wheel_scroll_view.dart
packages/flutter/lib/src/widgets/list_wheel_scroll_view.dart
+51
-7
nested_scroll_view.dart
packages/flutter/lib/src/widgets/nested_scroll_view.dart
+23
-0
page_view.dart
packages/flutter/lib/src/widgets/page_view.dart
+59
-10
scroll_metrics.dart
packages/flutter/lib/src/widgets/scroll_metrics.dart
+19
-10
scroll_position.dart
packages/flutter/lib/src/widgets/scroll_position.dart
+5
-5
page_view_test.dart
packages/flutter/test/widgets/page_view_test.dart
+16
-0
scroll_behavior_test.dart
packages/flutter/test/widgets/scroll_behavior_test.dart
+2
-2
No files found.
packages/flutter/lib/src/widgets/list_wheel_scroll_view.dart
View file @
17c50693
...
...
@@ -130,12 +130,41 @@ class FixedExtentScrollController extends ScrollController {
/// This is distinct from `Fixed` in the parent class name's [FixedScrollMetric]
/// which refers to its immutability.
class
FixedExtentMetrics
extends
FixedScrollMetrics
{
/// Creates
fixed extent metrics that add the item index information to the
///
`parent` [FixedScrollMetrics
].
/// Creates
an immutable snapshot of values associated with a
///
[ListWheelScrollView
].
FixedExtentMetrics
({
ScrollMetrics
parent
,
@required
double
minScrollExtent
,
@required
double
maxScrollExtent
,
@required
double
pixels
,
@required
double
viewportDimension
,
@required
AxisDirection
axisDirection
,
@required
this
.
itemIndex
,
})
:
super
.
clone
(
parent
);
})
:
super
(
minScrollExtent:
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
,
pixels:
pixels
,
viewportDimension:
viewportDimension
,
axisDirection:
axisDirection
,
);
@override
FixedExtentMetrics
copyWith
({
double
minScrollExtent
,
double
maxScrollExtent
,
double
pixels
,
double
viewportDimension
,
AxisDirection
axisDirection
,
int
itemIndex
,
})
{
return
new
FixedExtentMetrics
(
minScrollExtent:
minScrollExtent
??
this
.
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
??
this
.
maxScrollExtent
,
pixels:
pixels
??
this
.
pixels
,
viewportDimension:
viewportDimension
??
this
.
viewportDimension
,
axisDirection:
axisDirection
??
this
.
axisDirection
,
itemIndex:
itemIndex
??
this
.
itemIndex
,
);
}
/// The scroll view's currently selected item index.
final
int
itemIndex
;
...
...
@@ -160,7 +189,7 @@ double _clipOffsetToScrollableRange(
/// A [ScrollPositionWithSingleContext] that can only be created based on
/// [_FixedExtentScrollable] and can access its `itemExtent` to derive [itemIndex].
class
_FixedExtentScrollPosition
extends
ScrollPositionWithSingleContext
{
class
_FixedExtentScrollPosition
extends
ScrollPositionWithSingleContext
implements
FixedExtentMetrics
{
_FixedExtentScrollPosition
({
@required
ScrollPhysics
physics
,
@required
ScrollContext
context
,
...
...
@@ -188,6 +217,7 @@ class _FixedExtentScrollPosition extends ScrollPositionWithSingleContext {
double
get
itemExtent
=>
_getItemExtentFromScrollContext
(
context
);
@override
int
get
itemIndex
{
return
_getItemFromOffset
(
offset:
pixels
,
...
...
@@ -198,8 +228,22 @@ class _FixedExtentScrollPosition extends ScrollPositionWithSingleContext {
}
@override
FixedExtentMetrics
cloneMetrics
()
{
return
new
FixedExtentMetrics
(
parent:
this
,
itemIndex:
itemIndex
);
FixedExtentMetrics
copyWith
({
double
minScrollExtent
,
double
maxScrollExtent
,
double
pixels
,
double
viewportDimension
,
AxisDirection
axisDirection
,
int
itemIndex
,
})
{
return
new
FixedExtentMetrics
(
minScrollExtent:
minScrollExtent
??
this
.
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
??
this
.
maxScrollExtent
,
pixels:
pixels
??
this
.
pixels
,
viewportDimension:
viewportDimension
??
this
.
viewportDimension
,
axisDirection:
axisDirection
??
this
.
axisDirection
,
itemIndex:
itemIndex
??
this
.
itemIndex
,
);
}
}
...
...
packages/flutter/lib/src/widgets/nested_scroll_view.dart
View file @
17c50693
...
...
@@ -425,6 +425,29 @@ class _NestedScrollMetrics extends FixedScrollMetrics {
axisDirection:
axisDirection
,
);
@override
_NestedScrollMetrics
copyWith
({
double
minScrollExtent
,
double
maxScrollExtent
,
double
pixels
,
double
viewportDimension
,
AxisDirection
axisDirection
,
double
minRange
,
double
maxRange
,
double
correctionOffset
,
})
{
return
new
_NestedScrollMetrics
(
minScrollExtent:
minScrollExtent
??
this
.
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
??
this
.
maxScrollExtent
,
pixels:
pixels
??
this
.
pixels
,
viewportDimension:
viewportDimension
??
this
.
viewportDimension
,
axisDirection:
axisDirection
??
this
.
axisDirection
,
minRange:
minRange
??
this
.
minRange
,
maxRange:
maxRange
??
this
.
maxRange
,
correctionOffset:
correctionOffset
??
this
.
correctionOffset
,
);
}
final
double
minRange
;
final
double
maxRange
;
...
...
packages/flutter/lib/src/widgets/page_view.dart
View file @
17c50693
...
...
@@ -178,18 +178,54 @@ class PageController extends ScrollController {
/// The metrics are available on [ScrollNotification]s generated from
/// [PageView]s.
class
PageMetrics
extends
FixedScrollMetrics
{
/// Creates page metrics that add the given information to the `parent`
/// metrics.
/// Creates an immutable snapshot of values associated with a [PageView].
PageMetrics
({
ScrollMetrics
parent
,
this
.
page
,
})
:
super
.
clone
(
parent
);
@required
double
minScrollExtent
,
@required
double
maxScrollExtent
,
@required
double
pixels
,
@required
double
viewportDimension
,
@required
AxisDirection
axisDirection
,
@required
this
.
viewportFraction
,
})
:
super
(
minScrollExtent:
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
,
pixels:
pixels
,
viewportDimension:
viewportDimension
,
axisDirection:
axisDirection
,
);
@override
PageMetrics
copyWith
({
double
minScrollExtent
,
double
maxScrollExtent
,
double
pixels
,
double
viewportDimension
,
AxisDirection
axisDirection
,
double
viewportFraction
,
})
{
return
new
PageMetrics
(
minScrollExtent:
minScrollExtent
??
this
.
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
??
this
.
maxScrollExtent
,
pixels:
pixels
??
this
.
pixels
,
viewportDimension:
viewportDimension
??
this
.
viewportDimension
,
axisDirection:
axisDirection
??
this
.
axisDirection
,
viewportFraction:
viewportFraction
??
this
.
viewportFraction
,
);
}
/// The current page displayed in the [PageView].
final
double
page
;
double
get
page
{
return
math
.
max
(
0.0
,
pixels
.
clamp
(
minScrollExtent
,
maxScrollExtent
))
/
math
.
max
(
1.0
,
viewportDimension
*
viewportFraction
);
}
/// The fraction of the viewport that each page occupies.
///
/// Used to compute [page] from the current [pixels].
final
double
viewportFraction
;
}
class
_PagePosition
extends
ScrollPositionWithSingleContext
{
class
_PagePosition
extends
ScrollPositionWithSingleContext
implements
PageMetrics
{
_PagePosition
({
ScrollPhysics
physics
,
ScrollContext
context
,
...
...
@@ -214,6 +250,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
final
int
initialPage
;
double
_pageToUseOnStartup
;
@override
double
get
viewportFraction
=>
_viewportFraction
;
double
_viewportFraction
;
set
viewportFraction
(
double
value
)
{
...
...
@@ -233,6 +270,7 @@ class _PagePosition extends ScrollPositionWithSingleContext {
return
page
*
viewportDimension
*
viewportFraction
;
}
@override
double
get
page
=>
pixels
==
null
?
null
:
getPageFromPixels
(
pixels
.
clamp
(
minScrollExtent
,
maxScrollExtent
),
viewportDimension
);
@override
...
...
@@ -264,10 +302,21 @@ class _PagePosition extends ScrollPositionWithSingleContext {
}
@override
PageMetrics
cloneMetrics
()
{
PageMetrics
copyWith
({
double
minScrollExtent
,
double
maxScrollExtent
,
double
pixels
,
double
viewportDimension
,
AxisDirection
axisDirection
,
double
viewportFraction
,
})
{
return
new
PageMetrics
(
parent:
this
,
page:
page
,
minScrollExtent:
minScrollExtent
??
this
.
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
??
this
.
maxScrollExtent
,
pixels:
pixels
??
this
.
pixels
,
viewportDimension:
viewportDimension
??
this
.
viewportDimension
,
axisDirection:
axisDirection
??
this
.
axisDirection
,
viewportFraction:
viewportFraction
??
this
.
viewportFraction
,
);
}
}
...
...
packages/flutter/lib/src/widgets/scroll_metrics.dart
View file @
17c50693
...
...
@@ -36,7 +36,25 @@ abstract class ScrollMetrics {
///
/// This is useful if this object is mutable, but you want to get a snapshot
/// of the current state.
ScrollMetrics
cloneMetrics
()
=>
new
FixedScrollMetrics
.
clone
(
this
);
///
/// The named arguments allow the values to be adjusted in the process. This
/// is useful to examine hypothetical situations, for example "would applying
/// this delta unmodified take the position [outOfRange]?".
ScrollMetrics
copyWith
({
double
minScrollExtent
,
double
maxScrollExtent
,
double
pixels
,
double
viewportDimension
,
AxisDirection
axisDirection
,
})
{
return
new
FixedScrollMetrics
(
minScrollExtent:
minScrollExtent
??
this
.
minScrollExtent
,
maxScrollExtent:
maxScrollExtent
??
this
.
maxScrollExtent
,
pixels:
pixels
??
this
.
pixels
,
viewportDimension:
viewportDimension
??
this
.
viewportDimension
,
axisDirection:
axisDirection
??
this
.
axisDirection
,
);
}
/// The minimum in-range value for [pixels].
///
...
...
@@ -93,7 +111,6 @@ abstract class ScrollMetrics {
/// An immutable snapshot of values associated with a [Scrollable] viewport.
///
/// For details, see [ScrollMetrics], which defines this object's interfaces.
@immutable
class
FixedScrollMetrics
extends
ScrollMetrics
{
/// Creates an immutable snapshot of values associated with a [Scrollable] viewport.
FixedScrollMetrics
({
...
...
@@ -104,14 +121,6 @@ class FixedScrollMetrics extends ScrollMetrics {
@required
this
.
axisDirection
,
});
/// Creates an immutable snapshot of the given metrics.
FixedScrollMetrics
.
clone
(
ScrollMetrics
parent
)
:
minScrollExtent
=
parent
.
minScrollExtent
,
maxScrollExtent
=
parent
.
maxScrollExtent
,
pixels
=
parent
.
pixels
,
viewportDimension
=
parent
.
viewportDimension
,
axisDirection
=
parent
.
axisDirection
;
@override
final
double
minScrollExtent
;
...
...
packages/flutter/lib/src/widgets/scroll_position.dart
View file @
17c50693
...
...
@@ -589,19 +589,19 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
/// Called by [beginActivity] to report when an activity has started.
void
didStartScroll
()
{
activity
.
dispatchScrollStartNotification
(
c
loneMetrics
(),
context
.
notificationContext
);
activity
.
dispatchScrollStartNotification
(
c
opyWith
(),
context
.
notificationContext
);
}
/// Called by [setPixels] to report a change to the [pixels] position.
void
didUpdateScrollPositionBy
(
double
delta
)
{
activity
.
dispatchScrollUpdateNotification
(
c
loneMetrics
(),
context
.
notificationContext
,
delta
);
activity
.
dispatchScrollUpdateNotification
(
c
opyWith
(),
context
.
notificationContext
,
delta
);
}
/// Called by [beginActivity] to report when an activity has ended.
///
/// This also saves the scroll offset using [saveScrollOffset].
void
didEndScroll
()
{
activity
.
dispatchScrollEndNotification
(
c
loneMetrics
(),
context
.
notificationContext
);
activity
.
dispatchScrollEndNotification
(
c
opyWith
(),
context
.
notificationContext
);
if
(
keepScrollOffset
)
saveScrollOffset
();
}
...
...
@@ -611,14 +611,14 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
/// not applied to the [pixels] value.
void
didOverscrollBy
(
double
value
)
{
assert
(
activity
.
isScrolling
);
activity
.
dispatchOverscrollNotification
(
c
loneMetrics
(),
context
.
notificationContext
,
value
);
activity
.
dispatchOverscrollNotification
(
c
opyWith
(),
context
.
notificationContext
,
value
);
}
/// Dispatches a notification that the [userScrollDirection] has changed.
///
/// Subclasses should call this function when they change [userScrollDirection].
void
didUpdateScrollDirection
(
ScrollDirection
direction
)
{
new
UserScrollNotification
(
metrics:
c
loneMetrics
(),
context:
context
.
notificationContext
,
direction:
direction
).
dispatch
(
context
.
notificationContext
);
new
UserScrollNotification
(
metrics:
c
opyWith
(),
context:
context
.
notificationContext
,
direction:
direction
).
dispatch
(
context
.
notificationContext
);
}
@override
...
...
packages/flutter/test/widgets/page_view_test.dart
View file @
17c50693
...
...
@@ -712,4 +712,20 @@ void main() {
semantics
.
dispose
();
});
testWidgets
(
'PageMetrics'
,
(
WidgetTester
tester
)
async
{
final
PageMetrics
page
=
new
PageMetrics
(
minScrollExtent:
100.0
,
maxScrollExtent:
200.0
,
pixels:
150.0
,
viewportDimension:
25.0
,
axisDirection:
AxisDirection
.
right
,
viewportFraction:
1.0
,
);
expect
(
page
.
page
,
6
);
final
PageMetrics
page2
=
page
.
copyWith
(
pixels:
page
.
pixels
-
100.0
,
);
expect
(
page2
.
page
,
4.0
);
});
}
packages/flutter/test/widgets/scroll_behavior_test.dart
View file @
17c50693
...
...
@@ -48,7 +48,7 @@ void main() {
expect
(
behavior
,
isNotNull
);
expect
(
behavior
.
flag
,
isTrue
);
expect
(
position
.
physics
,
const
isInstanceOf
<
ClampingScrollPhysics
>());
ScrollMetrics
metrics
=
position
.
c
loneMetrics
();
ScrollMetrics
metrics
=
position
.
c
opyWith
();
expect
(
metrics
.
extentAfter
,
equals
(
400.0
));
expect
(
metrics
.
viewportDimension
,
equals
(
600.0
));
...
...
@@ -64,7 +64,7 @@ void main() {
expect
(
behavior
.
flag
,
isFalse
);
expect
(
position
.
physics
,
const
isInstanceOf
<
BouncingScrollPhysics
>());
// Regression test for https://github.com/flutter/flutter/issues/5856
metrics
=
position
.
c
loneMetrics
();
metrics
=
position
.
c
opyWith
();
expect
(
metrics
.
extentAfter
,
equals
(
400.0
));
expect
(
metrics
.
viewportDimension
,
equals
(
600.0
));
});
...
...
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