AspectRatio has incorrect intrinsic sizing (#3666)

If there's a max height or width, we should factor that into the intrinsic
sizing for the other dimension.
This commit is contained in:
Adam Barth
2016-05-02 15:51:45 -07:00
parent 0910a78fd6
commit 6072552868
2 changed files with 34 additions and 2 deletions

View File

@@ -374,6 +374,8 @@ class RenderAspectRatio extends RenderProxyBox {
RenderBox child,
double aspectRatio
}) : _aspectRatio = aspectRatio, super(child) {
assert(_aspectRatio > 0.0);
assert(_aspectRatio.isFinite);
assert(_aspectRatio != null);
}
@@ -385,6 +387,8 @@ class RenderAspectRatio extends RenderProxyBox {
double _aspectRatio;
void set aspectRatio (double newAspectRatio) {
assert(newAspectRatio != null);
assert(newAspectRatio > 0.0);
assert(newAspectRatio.isFinite);
if (_aspectRatio == newAspectRatio)
return;
_aspectRatio = newAspectRatio;
@@ -393,22 +397,26 @@ class RenderAspectRatio extends RenderProxyBox {
@override
double getMinIntrinsicWidth(BoxConstraints constraints) {
assert(constraints.debugAssertIsValid());
return constraints.minWidth;
}
@override
double getMaxIntrinsicWidth(BoxConstraints constraints) {
return constraints.maxWidth;
assert(constraints.debugAssertIsValid());
return constraints.constrainWidth(constraints.maxHeight * aspectRatio);
}
@override
double getMinIntrinsicHeight(BoxConstraints constraints) {
assert(constraints.debugAssertIsValid());
return constraints.minHeight;
}
@override
double getMaxIntrinsicHeight(BoxConstraints constraints) {
return constraints.maxHeight;
assert(constraints.debugAssertIsValid());
return constraints.constrainHeight(constraints.maxWidth / aspectRatio);
}
Size _applyAspectRatio(BoxConstraints constraints) {

View File

@@ -0,0 +1,24 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/rendering.dart';
import 'package:test/test.dart';
void main() {
test('Intrinsic sizing', () {
RenderAspectRatio box = new RenderAspectRatio(aspectRatio: 2.0);
BoxConstraints constraints = new BoxConstraints.loose(new Size(200.0, 200.0));
expect(box.getMinIntrinsicWidth(constraints), equals(0.0));
expect(box.getMaxIntrinsicWidth(constraints), equals(200.0));
expect(box.getMinIntrinsicHeight(constraints), equals(0.0));
expect(box.getMaxIntrinsicHeight(constraints), equals(100.0));
constraints = new BoxConstraints(maxHeight: 400.0);
expect(box.getMinIntrinsicWidth(constraints), equals(0.0));
expect(box.getMaxIntrinsicWidth(constraints), equals(800.0));
expect(box.getMinIntrinsicHeight(constraints), equals(0.0));
expect(box.getMaxIntrinsicHeight(constraints), equals(400.0));
});
}