From 50ecd5700f27eeb8bd5615836f64875267dd80ba Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Sat, 28 Oct 2023 22:23:32 +0200 Subject: [PATCH] BannerPainter should dispatch creation and disposal events. (#137472) --- packages/flutter/lib/src/widgets/banner.dart | 19 ++++++++++++++++++- .../flutter/test/widgets/banner_test.dart | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/banner.dart b/packages/flutter/lib/src/widgets/banner.dart index d1118394b1..00dc6c537f 100644 --- a/packages/flutter/lib/src/widgets/banner.dart +++ b/packages/flutter/lib/src/widgets/banner.dart @@ -23,6 +23,8 @@ const TextStyle _kTextStyle = TextStyle( height: 1.0, ); +const String _flutterWidgetsLibrary = 'package:flutter/widgets.dart'; + /// Where to show a [Banner]. /// /// The start and end locations are relative to the ambient [Directionality] @@ -61,7 +63,17 @@ class BannerPainter extends CustomPainter { required this.layoutDirection, this.color = _kColor, this.textStyle = _kTextStyle, - }) : super(repaint: PaintingBinding.instance.systemFonts); + }) : super(repaint: PaintingBinding.instance.systemFonts) { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectCreated( + library: _flutterWidgetsLibrary, + className: '$BannerPainter', + object: this, + ); + } + } /// The message to show in the banner. final String message; @@ -117,6 +129,11 @@ class BannerPainter extends CustomPainter { /// /// After calling this method, this object is no longer usable. void dispose() { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectDisposed(object: this); + } _textPainter?.dispose(); _textPainter = null; } diff --git a/packages/flutter/test/widgets/banner_test.dart b/packages/flutter/test/widgets/banner_test.dart index ca3d32b9df..c1a75656a3 100644 --- a/packages/flutter/test/widgets/banner_test.dart +++ b/packages/flutter/test/widgets/banner_test.dart @@ -280,4 +280,19 @@ void main() { ); debugDisableShadows = true; }); + + test('BannerPainter dispatches memory events', () async { + await expectLater( + await memoryEvents( + () => BannerPainter( + message: 'foo', + textDirection: TextDirection.rtl, + location: BannerLocation.topStart, + layoutDirection: TextDirection.ltr, + ).dispose(), + BannerPainter, + ), + areCreateAndDispose, + ); + }); }