Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BoxPainter should dispatch creation and disposal events. #141526

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/flutter/lib/src/painting/decoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,18 @@ abstract class Decoration with Diagnosticable {
/// happens, the [onChanged] callback will be invoked. To stop this callback
/// from being called after the painter has been discarded, call [dispose].
abstract class BoxPainter {
/// Abstract const constructor. This constructor enables subclasses to provide
/// const constructors so that they can be used in const expressions.
const BoxPainter([this.onChanged]);
/// Default abstract constructor for box painters.
BoxPainter([this.onChanged]) {
// TODO(polina-c): stop duplicating code across disposables
// https://github.com/flutter/flutter/issues/137435
if (kFlutterMemoryAllocationsEnabled) {
FlutterMemoryAllocations.instance.dispatchObjectCreated(
library: 'package:flutter/painting.dart',
className: '$BoxPainter',
object: this,
);
}
}

/// Paints the [Decoration] for which this object was created on the
/// given canvas using the given configuration.
Expand Down Expand Up @@ -243,5 +252,9 @@ abstract class BoxPainter {
/// The [onChanged] callback will not be invoked after this method has been
/// called.
@mustCallSuper
void dispose() { }
void dispose() {
if (kFlutterMemoryAllocationsEnabled) {
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
}
}
}
13 changes: 13 additions & 0 deletions packages/flutter/test/painting/decoration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:fake_async/fake_async.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

import '../image_data.dart';
import '../painting/mocks_for_image_cache.dart';
Expand Down Expand Up @@ -813,4 +814,16 @@ void main() {

info.dispose();
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87442

test('$BoxPainter dispatches memory events', () async {
await expectLater(
await memoryEvents(() => _BoxPainter().dispose(), _BoxPainter),
areCreateAndDispose,
);
});
}

class _BoxPainter extends BoxPainter {
@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {}
}