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
bd1583f3
Unverified
Commit
bd1583f3
authored
Aug 28, 2023
by
Polina Cherkasova
Committed by
GitHub
Aug 28, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FocusNode and FocusManager should dispatch creation in constructor. (#133490)
parent
5bd8579b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
17 deletions
+76
-17
drawer.dart
packages/flutter/lib/src/material/drawer.dart
+1
-0
focus_manager.dart
packages/flutter/lib/src/widgets/focus_manager.dart
+7
-0
focus_manager_test.dart
packages/flutter/test/widgets/focus_manager_test.dart
+35
-0
memory_allocations_test.dart
packages/flutter/test/widgets/memory_allocations_test.dart
+33
-17
No files found.
packages/flutter/lib/src/material/drawer.dart
View file @
bd1583f3
...
...
@@ -471,6 +471,7 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
void
dispose
()
{
_historyEntry
?.
remove
();
_controller
.
dispose
();
_focusScopeNode
.
dispose
();
super
.
dispose
();
}
...
...
packages/flutter/lib/src/widgets/focus_manager.dart
View file @
bd1583f3
...
...
@@ -437,6 +437,10 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
_descendantsAreTraversable
=
descendantsAreTraversable
{
// Set it via the setter so that it does nothing on release builds.
this
.
debugLabel
=
debugLabel
;
if
(
kFlutterMemoryAllocationsEnabled
)
{
maybeDispatchObjectCreation
();
}
}
/// If true, tells the focus traversal policy to skip over this node for
...
...
@@ -1463,6 +1467,9 @@ class FocusManager with DiagnosticableTreeMixin, ChangeNotifier {
/// handlers, callers must call [registerGlobalHandlers]. See the
/// documentation in that method for caveats to watch out for.
FocusManager
()
{
if
(
kFlutterMemoryAllocationsEnabled
)
{
maybeDispatchObjectCreation
();
}
rootScope
.
_manager
=
this
;
}
...
...
packages/flutter/test/widgets/focus_manager_test.dart
View file @
bd1583f3
...
...
@@ -1609,6 +1609,41 @@ void main() {
tester
.
binding
.
focusManager
.
removeListener
(
handleFocusChange
);
});
test
(
'
$FocusManager
dispatches object creation in constructor'
,
()
{
final
List
<
ObjectEvent
>
events
=
<
ObjectEvent
>[];
void
listener
(
ObjectEvent
event
)
{
if
(
event
.
object
.
runtimeType
==
FocusManager
)
{
events
.
add
(
event
);
}
}
MemoryAllocations
.
instance
.
addListener
(
listener
);
final
FocusManager
focusManager
=
FocusManager
();
expect
(
events
,
hasLength
(
1
));
focusManager
.
dispose
();
MemoryAllocations
.
instance
.
removeListener
(
listener
);
});
test
(
'
$FocusNode
dispatches object creation in constructor'
,
()
{
final
List
<
ObjectEvent
>
events
=
<
ObjectEvent
>[];
void
listener
(
ObjectEvent
event
)
{
if
(
event
.
object
.
runtimeType
==
FocusNode
)
{
events
.
add
(
event
);
}
}
MemoryAllocations
.
instance
.
addListener
(
listener
);
final
FocusNode
focusManager
=
FocusNode
();
expect
(
events
,
hasLength
(
1
));
focusManager
.
dispose
();
MemoryAllocations
.
instance
.
removeListener
(
listener
);
});
testWidgets
(
'FocusManager notifies listeners when a widget loses focus because it was removed.'
,
(
WidgetTester
tester
)
async
{
final
FocusNode
nodeA
=
FocusNode
(
debugLabel:
'a'
);
final
FocusNode
nodeB
=
FocusNode
(
debugLabel:
'b'
);
...
...
packages/flutter/test/widgets/memory_allocations_test.dart
View file @
bd1583f3
...
...
@@ -6,20 +6,26 @@ import 'package:flutter/foundation.dart';
import
'package:flutter/widgets.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
int
_creations
=
0
;
int
_disposals
=
0
;
void
main
(
)
{
final
MemoryAllocations
ma
=
MemoryAllocations
.
instance
;
setUp
(()
{
assert
(!
ma
.
hasListeners
);
});
test
(
'Publishers dispatch events in debug mode'
,
()
async
{
int
eventCount
=
0
;
void
listener
(
ObjectEvent
event
)
=>
eventCount
++;
void
listener
(
ObjectEvent
event
)
{
if
(
event
is
ObjectDisposed
)
{
_disposals
++;
}
if
(
event
is
ObjectCreated
)
{
_creations
++;
}
}
ma
.
addListener
(
listener
);
final
int
expectedEventCount
=
await
_activateFlutterObjectsAndReturnCountOfEvents
();
expect
(
eventCount
,
expectedEventCount
);
final
_EventStats
actual
=
await
_activateFlutterObjectsAndReturnCountOfEvents
();
expect
(
actual
.
creations
,
_creations
);
expect
(
actual
.
disposals
,
_disposals
);
ma
.
removeListener
(
listener
);
expect
(
ma
.
hasListeners
,
isFalse
);
...
...
@@ -29,6 +35,8 @@ void main() {
bool
stateCreated
=
false
;
bool
stateDisposed
=
false
;
expect
(
ma
.
hasListeners
,
false
);
void
listener
(
ObjectEvent
event
)
{
if
(
event
is
ObjectCreated
&&
event
.
object
is
State
)
{
stateCreated
=
true
;
...
...
@@ -47,7 +55,7 @@ void main() {
expect
(
stateCreated
,
isTrue
);
expect
(
stateDisposed
,
isTrue
);
ma
.
removeListener
(
listener
);
expect
(
ma
.
hasListeners
,
isF
alse
);
expect
(
ma
.
hasListeners
,
f
alse
);
});
}
...
...
@@ -62,7 +70,8 @@ class _TestElement extends RenderObjectElement with RootElementMixin {
_TestElement
():
super
(
_TestLeafRenderObjectWidget
());
void
makeInactive
()
{
assignOwner
(
BuildOwner
(
focusManager:
FocusManager
()));
final
FocusManager
newFocusManager
=
FocusManager
();
assignOwner
(
BuildOwner
(
focusManager:
newFocusManager
));
mount
(
null
,
null
);
deactivate
();
}
...
...
@@ -109,15 +118,22 @@ class _TestStatefulWidgetState extends State<_TestStatefulWidget> {
}
}
class
_EventStats
{
int
creations
=
0
;
int
disposals
=
0
;
}
/// Create and dispose Flutter objects to fire memory allocation events.
Future
<
int
>
_activateFlutterObjectsAndReturnCountOfEvents
()
async
{
int
count
=
0
;
Future
<
_EventStats
>
_activateFlutterObjectsAndReturnCountOfEvents
()
async
{
final
_EventStats
result
=
_EventStats
()
;
final
_TestElement
element
=
_TestElement
();
count
++;
final
RenderObject
renderObject
=
_TestRenderObject
();
count
++;
final
_TestElement
element
=
_TestElement
();
result
.
creations
++;
final
RenderObject
renderObject
=
_TestRenderObject
();
result
.
creations
++;
element
.
makeInactive
();
element
.
unmount
();
count
+=
3
;
renderObject
.
dispose
();
count
++;
element
.
makeInactive
();
result
.
creations
+=
3
;
// 1 for the new BuildOwner, 1 for the new FocusManager, 1 for the new FocusScopeNode
element
.
unmount
();
result
.
disposals
+=
2
;
// 1 for the old BuildOwner, 1 for the element
renderObject
.
dispose
();
result
.
disposals
+=
1
;
return
coun
t
;
return
resul
t
;
}
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