diff --git a/packages/flutter/lib/src/material/tab_controller.dart b/packages/flutter/lib/src/material/tab_controller.dart index 3e1bbe362c..ea740eac50 100644 --- a/packages/flutter/lib/src/material/tab_controller.dart +++ b/packages/flutter/lib/src/material/tab_controller.dart @@ -6,6 +6,7 @@ import 'dart:math' as math; import 'package:flutter/widgets.dart'; +import '../foundation/memory_allocations.dart'; import 'constants.dart'; // Examples can assume: @@ -114,7 +115,11 @@ class TabController extends ChangeNotifier { _animationController = AnimationController.unbounded( value: initialIndex.toDouble(), vsync: vsync, - ); + ) { + if (kFlutterMemoryAllocationsEnabled) { + ChangeNotifier.maybeDispatchObjectCreation(this); + } + } // Private constructor used by `_copyWith`. This allows a new TabController to // be created without having to create a new animationController. diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 9976dd21e3..5ee5e61167 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -1380,6 +1380,7 @@ class _TabBarState extends State { _controller!.removeListener(_handleTabControllerTick); } _controller = null; + _scrollController?.dispose(); // We don't own the _controller Animation, so it's not disposed here. super.dispose(); } diff --git a/packages/flutter/test/material/page_selector_test.dart b/packages/flutter/test/material/page_selector_test.dart index b1bf60008c..27da67f094 100644 --- a/packages/flutter/test/material/page_selector_test.dart +++ b/packages/flutter/test/material/page_selector_test.dart @@ -70,6 +70,7 @@ void main() { vsync: const TestVSync(), length: 3, ); + addTearDown(tabController.dispose); await tester.pumpWidget(buildFrame(tabController)); expect(tabController.index, 0); @@ -91,6 +92,7 @@ void main() { vsync: const TestVSync(), length: 3, ); + addTearDown(tabController.dispose); await tester.pumpWidget(buildFrame(tabController)); expect(tabController.index, 0); @@ -135,6 +137,7 @@ void main() { initialIndex: 1, length: 3, ); + addTearDown(tabController.dispose); await tester.pumpWidget(buildFrame(tabController)); expect(tabController.index, 1); @@ -196,6 +199,7 @@ void main() { initialIndex: 1, length: 3, ); + addTearDown(tabController.dispose); await tester.pumpWidget(buildFrame(tabController, color: kRed, selectedColor: kBlue)); expect(tabController.index, 1); @@ -212,6 +216,7 @@ void main() { initialIndex: 1, length: 3, ); + addTearDown(tabController.dispose); await tester.pumpWidget(buildFrame(tabController, indicatorSize: 16.0)); final Iterable indicatorElements = find.descendant( @@ -233,6 +238,7 @@ void main() { initialIndex: 1, length: 3, ); + addTearDown(tabController.dispose); Iterable indicators; diff --git a/packages/flutter/test/material/tab_controller_test.dart b/packages/flutter/test/material/tab_controller_test.dart new file mode 100644 index 0000000000..50d266e812 --- /dev/null +++ b/packages/flutter/test/material/tab_controller_test.dart @@ -0,0 +1,16 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; + +void main() { + testWidgetsWithLeakTracking('$TabController dispatches creation in constructor.', (WidgetTester widgetTester) async { + await expectLater( + await memoryEvents(() async => TabController(length: 1, vsync: const TestVSync()).dispose(), TabController), + areCreateAndDispose, + ); + }); +} diff --git a/packages/flutter/test/widgets/framework_test.dart b/packages/flutter/test/widgets/framework_test.dart index bfa44287a2..d66f346691 100644 --- a/packages/flutter/test/widgets/framework_test.dart +++ b/packages/flutter/test/widgets/framework_test.dart @@ -1062,7 +1062,7 @@ void main() { element.createChild(0, after: null); }); - testWidgetsWithLeakTracking('GlobalKey - re-attach child to new parents, and the old parent is deactivated(unmounted)', (WidgetTester tester) async { + testWidgets('GlobalKey - re-attach child to new parents, and the old parent is deactivated(unmounted)', (WidgetTester tester) async { // This is a regression test for https://github.com/flutter/flutter/issues/62055 const Key key1 = GlobalObjectKey('key1'); const Key key2 = GlobalObjectKey('key2'); diff --git a/packages/flutter/test/widgets/scrollable_test.dart b/packages/flutter/test/widgets/scrollable_test.dart index 93efe9ff89..e7576552f6 100644 --- a/packages/flutter/test/widgets/scrollable_test.dart +++ b/packages/flutter/test/widgets/scrollable_test.dart @@ -861,7 +861,7 @@ void main() { expect(targetMidLeftPage1, findsOneWidget); }); - testWidgetsWithLeakTracking('ensureVisible does not move TabViews', (WidgetTester tester) async { + testWidgets('ensureVisible does not move TabViews', (WidgetTester tester) async { final TickerProvider vsync = TestTickerProvider(); final TabController controller = TabController( length: 3,