From 5322c6f860639a724839c7da19a381d78585d883 Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Tue, 19 Jan 2021 16:24:34 -0800 Subject: [PATCH] Fix RenderCustomPaint intrinsics (#72103) Make RenderCustomPaint consult its preferred size for intrinsics when it has no child. Currently, it returns 0 for its intrinsics, even if it has a non-zero preferred size. --- .../lib/src/rendering/custom_paint.dart | 28 +++++++++++++++++++ .../test/widgets/custom_paint_test.dart | 16 +++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/flutter/lib/src/rendering/custom_paint.dart b/packages/flutter/lib/src/rendering/custom_paint.dart index 4233e418f6..049eecf77e 100644 --- a/packages/flutter/lib/src/rendering/custom_paint.dart +++ b/packages/flutter/lib/src/rendering/custom_paint.dart @@ -487,6 +487,34 @@ class RenderCustomPaint extends RenderProxyBox { /// to change in the next frame. bool willChange; + @override + double computeMinIntrinsicWidth(double height) { + if (child == null) + return preferredSize.width.isFinite ? preferredSize.width : 0; + return super.computeMinIntrinsicWidth(height); + } + + @override + double computeMaxIntrinsicWidth(double height) { + if (child == null) + return preferredSize.width.isFinite ? preferredSize.width : 0; + return super.computeMaxIntrinsicWidth(height); + } + + @override + double computeMinIntrinsicHeight(double width) { + if (child == null) + return preferredSize.height.isFinite ? preferredSize.height : 0; + return super.computeMinIntrinsicHeight(width); + } + + @override + double computeMaxIntrinsicHeight(double width) { + if (child == null) + return preferredSize.height.isFinite ? preferredSize.height : 0; + return super.computeMaxIntrinsicHeight(width); + } + @override void attach(PipelineOwner owner) { super.attach(owner); diff --git a/packages/flutter/test/widgets/custom_paint_test.dart b/packages/flutter/test/widgets/custom_paint_test.dart index 68013a3498..74e057ca7d 100644 --- a/packages/flutter/test/widgets/custom_paint_test.dart +++ b/packages/flutter/test/widgets/custom_paint_test.dart @@ -198,4 +198,20 @@ void main() { expect(() => CustomPaint(isComplex: true), throwsAssertionError); expect(() => CustomPaint(willChange: true), throwsAssertionError); }); + + test('RenderCustomPaint consults preferred size for intrinsics when it has no child', () { + final RenderCustomPaint inner = RenderCustomPaint(preferredSize: const Size(20, 30)); + expect(inner.getMinIntrinsicWidth(double.infinity), 20); + expect(inner.getMaxIntrinsicWidth(double.infinity), 20); + expect(inner.getMinIntrinsicHeight(double.infinity), 30); + expect(inner.getMaxIntrinsicHeight(double.infinity), 30); + }); + + test('RenderCustomPaint does not return infinity for its intrinsics', () { + final RenderCustomPaint inner = RenderCustomPaint(preferredSize: const Size.square(double.infinity)); + expect(inner.getMinIntrinsicWidth(double.infinity), 0); + expect(inner.getMaxIntrinsicWidth(double.infinity), 0); + expect(inner.getMinIntrinsicHeight(double.infinity), 0); + expect(inner.getMaxIntrinsicHeight(double.infinity), 0); + }); }