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
dbaf12b8
Commit
dbaf12b8
authored
Jun 12, 2017
by
Hans Muller
Committed by
GitHub
Jun 12, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not read or write state if PageStorageKeys cannot be found (#10612)
parent
81eb1404
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
48 additions
and
30 deletions
+48
-30
page_storage.dart
packages/flutter/lib/src/widgets/page_storage.dart
+34
-22
page_storage_test.dart
packages/flutter/test/widgets/page_storage_test.dart
+1
-1
page_view_test.dart
packages/flutter/test/widgets/page_view_test.dart
+3
-1
remember_scroll_position_test.dart
...s/flutter/test/widgets/remember_scroll_position_test.dart
+1
-0
scroll_controller_test.dart
packages/flutter/test/widgets/scroll_controller_test.dart
+9
-6
No files found.
packages/flutter/lib/src/widgets/page_storage.dart
View file @
dbaf12b8
...
...
@@ -39,21 +39,19 @@ class PageStorageKey<T> extends ValueKey<T> {
}
class
_StorageEntryIdentifier
{
_StorageEntryIdentifier
(
this
.
clientType
,
this
.
keys
)
{
assert
(
clientType
!=
null
);
_StorageEntryIdentifier
(
this
.
keys
)
{
assert
(
keys
!=
null
);
}
final
Type
clientType
;
final
List
<
PageStorageKey
<
dynamic
>>
keys
;
bool
get
isNotEmpty
=>
keys
.
isNotEmpty
;
@override
bool
operator
==(
dynamic
other
)
{
if
(
other
.
runtimeType
!=
runtimeType
)
return
false
;
final
_StorageEntryIdentifier
typedOther
=
other
;
if
(
clientType
!=
typedOther
.
clientType
||
keys
.
length
!=
typedOther
.
keys
.
length
)
return
false
;
for
(
int
index
=
0
;
index
<
keys
.
length
;
index
+=
1
)
{
if
(
keys
[
index
]
!=
typedOther
.
keys
[
index
])
return
false
;
...
...
@@ -62,11 +60,11 @@ class _StorageEntryIdentifier {
}
@override
int
get
hashCode
=>
hash
Values
(
clientType
,
hashList
(
keys
)
);
int
get
hashCode
=>
hash
List
(
keys
);
@override
String
toString
()
{
return
'StorageEntryIdentifier(
$
clientType
,
$
{keys?.join(":")}
)'
;
return
'StorageEntryIdentifier(
${keys?.join(":")}
)'
;
}
}
...
...
@@ -94,31 +92,45 @@ class PageStorageBucket {
}
_StorageEntryIdentifier
_computeIdentifier
(
BuildContext
context
)
{
return
new
_StorageEntryIdentifier
(
context
.
widget
.
runtimeType
,
_allKeys
(
context
));
return
new
_StorageEntryIdentifier
(
_allKeys
(
context
));
}
Map
<
Object
,
dynamic
>
_storage
;
/// Write the given data into this page storage bucket using
an identifier
///
computed from the given context. The identifier is based on the keys
///
found in the path from context to the root of the widget tree for thi
s
///
page. Keys are collected until the widget tree's root is reached or
///
a GlobalKey is found
.
/// Write the given data into this page storage bucket using
the
///
specified identifier or an identifier computed from the given context.
///
The computed identifier is based on the [PageStorageKey]
s
///
found in the path from context to the [PageStorage] widget that
///
owns this page storage bucket
.
///
/// An explicit identifier can be used in cases where the list of keys
/// is not stable. For example if the path concludes with a GlobalKey
/// that's created by a stateful widget, if the stateful widget is
/// recreated when it's exposed by [Navigator.pop], then its storage
/// identifier will change.
/// If an explicit identifier is not provided and no [PageStorageKey]s
/// are found, then the `data` is not saved.
void
writeState
(
BuildContext
context
,
dynamic
data
,
{
Object
identifier
})
{
_storage
??=
<
Object
,
dynamic
>{};
_storage
[
identifier
??
_computeIdentifier
(
context
)]
=
data
;
if
(
identifier
!=
null
)
{
_storage
[
identifier
]
=
data
;
}
else
{
final
_StorageEntryIdentifier
contextIdentifier
=
_computeIdentifier
(
context
);
if
(
contextIdentifier
.
isNotEmpty
)
_storage
[
contextIdentifier
]
=
data
;
}
}
/// Read given data from into this page storage bucket using an identifier
/// computed from the given context. More about [identifier] in [writeState].
/// Read given data from into this page storage bucket using the specified
/// identifier or an identifier computed from the given context.
/// The computed identifier is based on the [PageStorageKey]s
/// found in the path from context to the [PageStorage] widget that
/// owns this page storage bucket.
///
/// If an explicit identifier is not provided and no [PageStorageKey]s
/// are found, then null is returned.
dynamic
readState
(
BuildContext
context
,
{
Object
identifier
})
{
return
_storage
!=
null
?
_storage
[
identifier
??
_computeIdentifier
(
context
)]
:
null
;
if
(
_storage
==
null
)
return
null
;
if
(
identifier
!=
null
)
return
_storage
[
identifier
];
final
_StorageEntryIdentifier
contextIdentifier
=
_computeIdentifier
(
context
);
return
contextIdentifier
.
isNotEmpty
?
_storage
[
contextIdentifier
]
:
null
;
}
}
...
...
packages/flutter/test/widgets/page_storage_test.dart
View file @
dbaf12b8
...
...
@@ -7,7 +7,7 @@ import 'package:flutter/material.dart';
void
main
(
)
{
testWidgets
(
'PageStorage read and write'
,
(
WidgetTester
tester
)
async
{
final
Key
builderKey
=
const
Key
(
'builderKey'
);
final
Key
builderKey
=
const
PageStorageKey
<
String
>
(
'builderKey'
);
StateSetter
setState
;
int
storedValue
=
0
;
...
...
packages/flutter/test/widgets/page_view_test.dart
View file @
dbaf12b8
...
...
@@ -407,6 +407,7 @@ void main() {
new
PageStorage
(
bucket:
bucket
,
child:
new
PageView
(
key:
const
PageStorageKey
<
String
>(
'PageView'
),
controller:
controller
,
children:
<
Widget
>[
const
Placeholder
(),
...
...
@@ -431,6 +432,7 @@ void main() {
new
PageStorage
(
bucket:
bucket
,
child:
new
PageView
(
key:
const
PageStorageKey
<
String
>(
'PageView'
),
controller:
controller
,
children:
<
Widget
>[
const
Placeholder
(),
...
...
@@ -447,7 +449,7 @@ void main() {
new
PageStorage
(
bucket:
bucket
,
child:
new
PageView
(
key:
const
Key
(
'Check it again against your list and see consistency!'
),
key:
const
PageStorageKey
<
String
>
(
'Check it again against your list and see consistency!'
),
controller:
controller2
,
children:
<
Widget
>[
const
Placeholder
(),
...
...
packages/flutter/test/widgets/remember_scroll_position_test.dart
View file @
dbaf12b8
...
...
@@ -18,6 +18,7 @@ class ThePositiveNumbers extends StatelessWidget {
@override
Widget
build
(
BuildContext
context
)
{
return
new
ListView
.
builder
(
key:
const
PageStorageKey
<
String
>(
'ThePositiveNumbers'
),
itemExtent:
100.0
,
controller:
_controller
,
itemBuilder:
(
BuildContext
context
,
int
index
)
{
...
...
packages/flutter/test/widgets/scroll_controller_test.dart
View file @
dbaf12b8
...
...
@@ -266,6 +266,8 @@ void main() {
Widget
buildFrame
(
ScrollController
controller
)
{
return
new
PageStorage
(
bucket:
bucket
,
child:
new
KeyedSubtree
(
key:
const
PageStorageKey
<
String
>(
'ListView'
),
child:
new
ListView
(
key:
new
UniqueKey
(),
// it's a different ListView every time
controller:
controller
,
...
...
@@ -273,6 +275,7 @@ void main() {
return
new
Container
(
height:
100.0
,
child:
new
Text
(
'Item
$index
'
));
}).
toList
(),
),
),
);
}
...
...
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