Skip to content

Commit

Permalink
Asserts if a TextPainter gets disposed more than once (#145124)
Browse files Browse the repository at this point in the history
The overflow indicator was sharing the same `TextPainter`.
  • Loading branch information
LongCatIsLooong authored Mar 14, 2024
1 parent 51d59a7 commit a69567a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/flutter/lib/src/painting/text_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ class TextPainter {
///
/// After disposal this painter is unusable.
void dispose() {
assert(!debugDisposed);
assert(() {
_disposed = true;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ mixin DebugOverflowIndicatorMixin on RenderObject {
);
static final Paint _labelBackgroundPaint = Paint()..color = const Color(0xFFFFFFFF);

final List<TextPainter> _indicatorLabel = List<TextPainter>.filled(
final List<TextPainter> _indicatorLabel = List<TextPainter>.generate(
_OverflowSide.values.length,
TextPainter(textDirection: TextDirection.ltr), // This label is in English.
(int i) => TextPainter(textDirection: TextDirection.ltr), // This label is in English.
growable: false,
);

@override
Expand Down
6 changes: 6 additions & 0 deletions packages/flutter/test/painting/text_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,12 @@ void main() {
expect(painter.debugDisposed, true);
});

test('TextPainter - asserts if disposed more than once', () {
final TextPainter painter = TextPainter()..dispose();
expect(painter.debugDisposed, isTrue);
expect(painter.dispose, throwsAssertionError);
});

test('TextPainter computeWidth', () {
const InlineSpan text = TextSpan(text: 'foobar');
final TextPainter painter = TextPainter(text: text, textDirection: TextDirection.ltr);
Expand Down

0 comments on commit a69567a

Please sign in to comment.