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
d0e72d61
Commit
d0e72d61
authored
Aug 26, 2016
by
Hans Muller
Committed by
GitHub
Aug 26, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prevent looking up inherited widget values from initState() (#5609)
parent
fa8dc5f8
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
103 additions
and
57 deletions
+103
-57
animation_demo.dart
examples/flutter_gallery/lib/demo/animation_demo.dart
+14
-12
refresh_indicator.dart
packages/flutter/lib/src/material/refresh_indicator.dart
+12
-13
tabs.dart
packages/flutter/lib/src/material/tabs.dart
+15
-20
framework.dart
packages/flutter/lib/src/widgets/framework.dart
+18
-0
image.dart
packages/flutter/lib/src/widgets/image.dart
+9
-6
pageable_list.dart
packages/flutter/lib/src/widgets/pageable_list.dart
+0
-6
inherited_test.dart
packages/flutter/test/widget/inherited_test.dart
+35
-0
No files found.
examples/flutter_gallery/lib/demo/animation_demo.dart
View file @
d0e72d61
...
...
@@ -121,9 +121,6 @@ class _PointDemoState extends State<_PointDemo> {
@override
void
initState
()
{
super
.
initState
();
final
Size
screenSize
=
MediaQuery
.
of
(
context
).
size
;
_begin
=
new
Point
(
screenSize
.
width
*
0.5
,
screenSize
.
height
*
0.2
);
_end
=
new
Point
(
screenSize
.
width
*
0.1
,
screenSize
.
height
*
0.4
);
_animation
=
new
CurvedAnimation
(
parent:
config
.
controller
,
curve:
Curves
.
ease
);
}
...
...
@@ -179,6 +176,10 @@ class _PointDemoState extends State<_PointDemo> {
@override
Widget
build
(
BuildContext
context
)
{
final
Size
screenSize
=
MediaQuery
.
of
(
context
).
size
;
_begin
=
new
Point
(
screenSize
.
width
*
0.5
,
screenSize
.
height
*
0.2
);
_end
=
new
Point
(
screenSize
.
width
*
0.1
,
screenSize
.
height
*
0.4
);
final
MaterialPointArcTween
arc
=
new
MaterialPointArcTween
(
begin:
_begin
,
end:
_end
);
return
new
RawGestureDetector
(
behavior:
_dragTarget
==
null
?
HitTestBehavior
.
deferToChild
:
HitTestBehavior
.
opaque
,
...
...
@@ -281,15 +282,6 @@ class _RectangleDemoState extends State<_RectangleDemo> {
@override
void
initState
()
{
super
.
initState
();
final
Size
screenSize
=
MediaQuery
.
of
(
context
).
size
;
_begin
=
new
Rect
.
fromLTWH
(
screenSize
.
width
*
0.5
,
screenSize
.
height
*
0.2
,
screenSize
.
width
*
0.4
,
screenSize
.
height
*
0.2
);
_end
=
new
Rect
.
fromLTWH
(
screenSize
.
width
*
0.1
,
screenSize
.
height
*
0.4
,
screenSize
.
width
*
0.3
,
screenSize
.
height
*
0.3
);
_animation
=
new
CurvedAnimation
(
parent:
config
.
controller
,
curve:
Curves
.
ease
);
}
...
...
@@ -344,6 +336,16 @@ class _RectangleDemoState extends State<_RectangleDemo> {
@override
Widget
build
(
BuildContext
context
)
{
final
Size
screenSize
=
MediaQuery
.
of
(
context
).
size
;
_begin
=
new
Rect
.
fromLTWH
(
screenSize
.
width
*
0.5
,
screenSize
.
height
*
0.2
,
screenSize
.
width
*
0.4
,
screenSize
.
height
*
0.2
);
_end
=
new
Rect
.
fromLTWH
(
screenSize
.
width
*
0.1
,
screenSize
.
height
*
0.4
,
screenSize
.
width
*
0.3
,
screenSize
.
height
*
0.3
);
final
MaterialRectArcTween
arc
=
new
MaterialRectArcTween
(
begin:
_begin
,
end:
_end
);
return
new
RawGestureDetector
(
behavior:
_dragTarget
==
null
?
HitTestBehavior
.
deferToChild
:
HitTestBehavior
.
opaque
,
...
...
packages/flutter/lib/src/material/refresh_indicator.dart
View file @
d0e72d61
...
...
@@ -157,25 +157,12 @@ class RefreshIndicatorState extends State<RefreshIndicator> {
_sizeFactor
=
new
Tween
<
double
>(
begin:
0.0
,
end:
_kDragSizeFactorLimit
).
animate
(
_sizeController
);
_scaleFactor
=
new
Tween
<
double
>(
begin:
1.0
,
end:
0.0
).
animate
(
_scaleController
);
final
ThemeData
theme
=
Theme
.
of
(
context
);
// The "value" of the circular progress indicator during a drag.
_value
=
new
Tween
<
double
>(
begin:
0.0
,
end:
0.75
)
.
animate
(
_sizeController
);
// Fully opaque when we've reached config.displacement.
_valueColor
=
new
ColorTween
(
begin:
(
config
.
color
??
theme
.
accentColor
).
withOpacity
(
0.0
),
end:
(
config
.
color
??
theme
.
accentColor
).
withOpacity
(
1.0
)
)
.
animate
(
new
CurvedAnimation
(
parent:
_sizeController
,
curve:
new
Interval
(
0.0
,
1.0
/
_kDragSizeFactorLimit
)
));
}
@override
...
...
@@ -346,8 +333,20 @@ class RefreshIndicatorState extends State<RefreshIndicator> {
@override
Widget
build
(
BuildContext
context
)
{
final
ThemeData
theme
=
Theme
.
of
(
context
);
final
bool
showIndeterminateIndicator
=
_mode
==
_RefreshIndicatorMode
.
refresh
||
_mode
==
_RefreshIndicatorMode
.
dismiss
;
// Fully opaque when we've reached config.displacement.
_valueColor
=
new
ColorTween
(
begin:
(
config
.
color
??
theme
.
accentColor
).
withOpacity
(
0.0
),
end:
(
config
.
color
??
theme
.
accentColor
).
withOpacity
(
1.0
)
)
.
animate
(
new
CurvedAnimation
(
parent:
_sizeController
,
curve:
new
Interval
(
0.0
,
1.0
/
_kDragSizeFactorLimit
)
));
return
new
Listener
(
onPointerDown:
_handlePointerDown
,
onPointerMove:
_handlePointerMove
,
...
...
packages/flutter/lib/src/material/tabs.dart
View file @
d0e72d61
...
...
@@ -768,19 +768,20 @@ class _TabBarState<T> extends ScrollableState<TabBar<T>> implements TabBarSelect
bool
_valueIsChanging
=
false
;
int
_lastSelectedIndex
=
-
1
;
void
_initSelection
(
TabBarSelectionState
<
T
>
selection
)
{
void
_initSelection
(
TabBarSelectionState
<
T
>
newSelection
)
{
if
(
_selection
==
newSelection
)
return
;
_selection
?.
unregisterAnimationListener
(
this
);
_selection
=
s
election
;
_selection
=
newS
election
;
_selection
?.
registerAnimationListener
(
this
);
if
(
_selection
!=
null
)
_lastSelectedIndex
=
_selection
.
index
;
}
@override
void
initState
()
{
super
.
initState
();
scrollBehavior
.
isScrollable
=
config
.
isScrollable
;
_initSelection
(
TabBarSelection
.
of
(
context
));
if
(
_selection
!=
null
)
_lastSelectedIndex
=
_selection
.
index
;
}
@override
...
...
@@ -990,8 +991,7 @@ class _TabBarState<T> extends ScrollableState<TabBar<T>> implements TabBarSelect
@override
Widget
buildContent
(
BuildContext
context
)
{
TabBarSelectionState
<
T
>
newSelection
=
TabBarSelection
.
of
(
context
);
if
(
_selection
!=
newSelection
)
_initSelection
(
newSelection
);
_initSelection
(
newSelection
);
assert
(
config
.
labels
.
isNotEmpty
);
assert
(
Material
.
of
(
context
)
!=
null
);
...
...
@@ -1097,18 +1097,14 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
@override
TargetPlatform
get
platform
=>
Theme
.
of
(
context
).
platform
;
void
_initSelection
(
TabBarSelectionState
<
T
>
selection
)
{
_selection
=
selection
;
if
(
_selection
!=
null
)
{
_selection
.
registerAnimationListener
(
this
);
void
_initSelection
(
TabBarSelectionState
<
T
>
newSelection
)
{
if
(
_selection
==
newSelection
)
return
;
_selection
?.
unregisterAnimationListener
(
this
);
_selection
=
newSelection
;
_selection
?.
registerAnimationListener
(
this
);
if
(
_selection
!=
null
)
_updateItemsAndScrollBehavior
();
}
}
@override
void
initState
()
{
super
.
initState
();
_initSelection
(
TabBarSelection
.
of
(
context
));
}
@override
...
...
@@ -1243,8 +1239,7 @@ class _TabBarViewState<T> extends PageableListState<TabBarView<T>> implements Ta
@override
Widget
buildContent
(
BuildContext
context
)
{
TabBarSelectionState
<
T
>
newSelection
=
TabBarSelection
.
of
(
context
);
if
(
_selection
!=
newSelection
)
_initSelection
(
newSelection
);
_initSelection
(
newSelection
);
return
new
PageViewport
(
itemsWrap:
config
.
itemsWrap
,
mainAxis:
config
.
scrollDirection
,
...
...
packages/flutter/lib/src/widgets/framework.dart
View file @
d0e72d61
...
...
@@ -2183,6 +2183,24 @@ class StatefulElement extends ComponentElement {
_state
=
null
;
}
@override
InheritedWidget
inheritFromWidgetOfExactType
(
Type
targetType
)
{
assert
(()
{
if
(
state
.
_debugLifecycleState
==
_StateLifecycle
.
ready
)
return
true
;
throw
new
FlutterError
(
"inheritFromWidgetOfExactType(
$targetType
) was called before
${_state.runtimeType}
.initState() completed.
\n
"
"When an inherited widget changes, for example if the value of Theme.of() changes, "
"its dependent widgets are rebuilt. If the dependent widget's reference to "
"the inherited widget is in an constructor or an initState() method, "
"then the rebuilt dependent widget will not reflect the changes in the "
"inherited widget.
\n
"
"Typically references to to inherited widgets should occur in widget build() methods.
\n
"
);
});
return
super
.
inheritFromWidgetOfExactType
(
targetType
);
}
@override
void
dependenciesChanged
()
{
super
.
dependenciesChanged
();
...
...
packages/flutter/lib/src/widgets/image.dart
View file @
d0e72d61
...
...
@@ -187,12 +187,6 @@ class _ImageState extends State<Image> {
ImageStream
_imageStream
;
ImageInfo
_imageInfo
;
@override
void
initState
()
{
super
.
initState
();
_resolveImage
();
}
@override
void
didUpdateConfig
(
Image
oldConfig
)
{
if
(
config
.
image
!=
oldConfig
.
image
)
...
...
@@ -240,6 +234,15 @@ class _ImageState extends State<Image> {
@override
Widget
build
(
BuildContext
context
)
{
// This one-time initialization could have been done in initState() since
// changes to the inherited widgets that _resolveImage depends on, notably
// DefaultAssetBundle, are handle by the dependenciesChanged() method. We're
// doing it here instead to avoid the assert that disallows references to
// inherited widgets at initState() time. We've found that assert to be a
// reliable source of real bugs, and that it is worth this minor inconvenience.
if
(
_imageStream
==
null
)
_resolveImage
();
return
new
RawImage
(
image:
_imageInfo
?.
image
,
width:
config
.
width
,
...
...
packages/flutter/lib/src/widgets/pageable_list.dart
View file @
d0e72d61
...
...
@@ -255,12 +255,6 @@ abstract class PageableState<T extends Pageable> extends ScrollableState<T> {
return
null
;
}
@override
void
initState
()
{
super
.
initState
();
_updateScrollBehavior
();
}
@override
void
didUpdateConfig
(
Pageable
oldConfig
)
{
super
.
didUpdateConfig
(
oldConfig
);
...
...
packages/flutter/test/widget/inherited_test.dart
View file @
d0e72d61
...
...
@@ -29,6 +29,29 @@ class ValueInherited extends InheritedWidget {
bool
updateShouldNotify
(
ValueInherited
oldWidget
)
=>
value
!=
oldWidget
.
value
;
}
class
ExpectFail
extends
StatefulWidget
{
ExpectFail
(
this
.
onError
);
final
VoidCallback
onError
;
@override
ExpectFailState
createState
()
=>
new
ExpectFailState
();
}
class
ExpectFailState
extends
State
<
ExpectFail
>
{
@override
void
initState
()
{
super
.
initState
();
try
{
context
.
inheritFromWidgetOfExactType
(
TestInherited
);
// should fail
}
catch
(
e
)
{
config
.
onError
();
}
}
@override
Widget
build
(
BuildContext
context
)
=>
new
Container
();
}
void
main
(
)
{
testWidgets
(
'Inherited notifies dependents'
,
(
WidgetTester
tester
)
async
{
List
<
TestInherited
>
log
=
<
TestInherited
>[];
...
...
@@ -437,4 +460,16 @@ void main() {
);
expect
(
buildCount
,
equals
(
2
));
});
testWidgets
(
'initState() dependency on Inherited asserts'
,
(
WidgetTester
tester
)
async
{
// This is a regression test for https://github.com/flutter/flutter/issues/5491
bool
exceptionCaught
=
false
;
TestInherited
parent
=
new
TestInherited
(
child:
new
ExpectFail
(()
{
exceptionCaught
=
true
;
}));
await
tester
.
pumpWidget
(
parent
);
expect
(
exceptionCaught
,
isTrue
);
});
}
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