Skip to content

Commit 1a2c315

Browse files
authored
BoxPainter should dispatch creation and disposal events. (#141526)
1 parent 947d7ca commit 1a2c315

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

packages/flutter/lib/src/painting/decoration.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,18 @@ abstract class Decoration with Diagnosticable {
198198
/// happens, the [onChanged] callback will be invoked. To stop this callback
199199
/// from being called after the painter has been discarded, call [dispose].
200200
abstract class BoxPainter {
201-
/// Abstract const constructor. This constructor enables subclasses to provide
202-
/// const constructors so that they can be used in const expressions.
203-
const BoxPainter([this.onChanged]);
201+
/// Default abstract constructor for box painters.
202+
BoxPainter([this.onChanged]) {
203+
// TODO(polina-c): stop duplicating code across disposables
204+
// https://github.com/flutter/flutter/issues/137435
205+
if (kFlutterMemoryAllocationsEnabled) {
206+
FlutterMemoryAllocations.instance.dispatchObjectCreated(
207+
library: 'package:flutter/painting.dart',
208+
className: '$BoxPainter',
209+
object: this,
210+
);
211+
}
212+
}
204213

205214
/// Paints the [Decoration] for which this object was created on the
206215
/// given canvas using the given configuration.
@@ -243,5 +252,9 @@ abstract class BoxPainter {
243252
/// The [onChanged] callback will not be invoked after this method has been
244253
/// called.
245254
@mustCallSuper
246-
void dispose() { }
255+
void dispose() {
256+
if (kFlutterMemoryAllocationsEnabled) {
257+
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
258+
}
259+
}
247260
}

packages/flutter/test/painting/decoration_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:fake_async/fake_async.dart';
99
import 'package:flutter/foundation.dart';
1010
import 'package:flutter/painting.dart';
1111
import 'package:flutter_test/flutter_test.dart';
12+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1213

1314
import '../image_data.dart';
1415
import '../painting/mocks_for_image_cache.dart';
@@ -813,4 +814,16 @@ void main() {
813814

814815
info.dispose();
815816
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442
817+
818+
test('$BoxPainter dispatches memory events', () async {
819+
await expectLater(
820+
await memoryEvents(() => _BoxPainter().dispose(), _BoxPainter),
821+
areCreateAndDispose,
822+
);
823+
});
824+
}
825+
826+
class _BoxPainter extends BoxPainter {
827+
@override
828+
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {}
816829
}

0 commit comments

Comments
 (0)