From cf91262f75dde4a3a56e14a657e5c870fa1478a9 Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Mon, 28 Aug 2023 09:38:05 -0700 Subject: [PATCH] ShortcutManager should dispatch creation in constructor. (#133356) --- .../flutter/lib/src/widgets/shortcuts.dart | 7 +++++- .../flutter/test/widgets/shortcuts_test.dart | 22 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/flutter/lib/src/widgets/shortcuts.dart b/packages/flutter/lib/src/widgets/shortcuts.dart index 5c66c42997..c0118e633f 100644 --- a/packages/flutter/lib/src/widgets/shortcuts.dart +++ b/packages/flutter/lib/src/widgets/shortcuts.dart @@ -747,7 +747,11 @@ class ShortcutManager with Diagnosticable, ChangeNotifier { ShortcutManager({ Map shortcuts = const {}, this.modal = false, - }) : _shortcuts = shortcuts; + }) : _shortcuts = shortcuts { + if (kFlutterMemoryAllocationsEnabled) { + maybeDispatchObjectCreation(); + } + } /// True if the [ShortcutManager] should not pass on keys that it doesn't /// handle to any key-handling widgets that are ancestors to this one. @@ -1440,6 +1444,7 @@ class _ShortcutRegistrarState extends State { void dispose() { registry.removeListener(_shortcutsChanged); registry.dispose(); + manager.dispose(); super.dispose(); } diff --git a/packages/flutter/test/widgets/shortcuts_test.dart b/packages/flutter/test/widgets/shortcuts_test.dart index 064fdfb231..fedf7f9cd2 100644 --- a/packages/flutter/test/widgets/shortcuts_test.dart +++ b/packages/flutter/test/widgets/shortcuts_test.dart @@ -666,6 +666,19 @@ void main() { expect(pressedKeys, isEmpty); }); + test('$ShortcutManager dispatches object creation in constructor', () { + int eventCount = 0; + void listener(ObjectEvent event) => eventCount++; + MemoryAllocations.instance.addListener(listener); + + final ShortcutManager registry = ShortcutManager(); + + expect(eventCount, 1); + + registry.dispose(); + MemoryAllocations.instance.removeListener(listener); + }); + testWidgets("Shortcuts passes to the next Shortcuts widget if it doesn't map the key", (WidgetTester tester) async { final GlobalKey containerKey = GlobalKey(); final List pressedKeys = []; @@ -1853,20 +1866,17 @@ void main() { token.dispose(); }); - testWidgets('dispatches object creation in constructor', (WidgetTester tester) async { - final MemoryAllocations ma = MemoryAllocations.instance; - assert(!ma.hasListeners); + test('dispatches object creation in constructor', () { int eventCount = 0; void listener(ObjectEvent event) => eventCount++; - ma.addListener(listener); + MemoryAllocations.instance.addListener(listener); final ShortcutRegistry registry = ShortcutRegistry(); expect(eventCount, 1); registry.dispose(); - ma.removeListener(listener); - assert(!ma.hasListeners); + MemoryAllocations.instance.removeListener(listener); }); }); }