Skip to content

SizedBox calculates intrinsic dimensions incorrectly #137546

@Albert221

Description

@Albert221

Let's start with example, you can paste it into https://dartpad.dev

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
  debugPaintSizeEnabled = true;   
  
  runApp(
    const MaterialApp(
      home: Scaffold(
        body: Center(
          child: IntrinsicHeight(
            child: SizedBox(
              width: 100,
              child: Column(
                children: [
                  Text(lipsum),
                ],
              ),
            ),
          ),
        ),
      ),
    ),
  );
}

const lipsum =
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam malesuada vulputate efficitur. Curabitur a nulla et nulla bibendum rhoncus. Duis nec porttitor nunc, vitae pharetra sapien. Ut et vulputate velit, eget porttitor nulla. Curabitur maximus dui id nunc ornare lacinia.';

The Column has height equal to intrinsic heigth of its child. But it's wrong. The computeMinIntrinsicHeight doesn't take into account the SizedBox's width when it should. It calculates the intrinsic height with the height it receives from the parent. You can see this height by commenting out the width line.

Reproducible both on Flutter stable (3.13.7) and master (3.16.0-11.0.pre.3).

Expected Actual What intrinsic height "sees" as width
image image image

The faulty code is in RenderConstrainedBox, so it touches not only SizedBox, but also ConstrainedBox and possibly other widgets. It's both in compute intrinsic height and width methods.

  @override
  double computeMinIntrinsicHeight(double width) {
    if (_additionalConstraints.hasBoundedHeight && _additionalConstraints.hasTightHeight) {
      return _additionalConstraints.minHeight;
    }
    final double height = super.computeMinIntrinsicHeight(width);
    assert(height.isFinite);
    if (!_additionalConstraints.hasInfiniteHeight) {
      return _additionalConstraints.constrainHeight(height);
    }
    return height;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listcustomer: crowdAffects or could affect many people, though not necessarily a specific customer.found in release: 3.13Found to occur in 3.13found in release: 3.16Found to occur in 3.16frameworkflutter/packages/flutter repository. See also f: labels.has partial patchThere is a PR awaiting someone to take it across the finish linehas reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions