Unverified Commit 6ff02dbc authored by Maximilian Fischer's avatar Maximilian Fischer Committed by GitHub

Include size factors when computing the intrinsic size of a `RenderPositionedBox` (#135823)

This PR includes the `widthFactor` and `heightFactor` when computing the intrinsic size of a `RenderPositionedBox`.

<details><summary>Code sample</summary>

Red should have a height of 100, blue one of 200.

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Intrinsic Bug',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Scaffold(
        body: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: IntrinsicHeight(
                child: Align(
                  heightFactor: 0.5,
                  child: Container(
                    height: 200,
                    color: Colors.red,
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                height: 200,
                color: Colors.blue,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
```
</details>

Before:
![grafik](https://github.com/flutter/flutter/assets/45403027/447d8b9b-e7f3-482a-900d-53e436830321)
After:
![grafik](https://github.com/flutter/flutter/assets/45403027/6d1a104b-3327-47e2-8b36-798ed0136793)

Fix #135822
parent d773c5a9
...@@ -407,6 +407,26 @@ class RenderPositionedBox extends RenderAligningShiftedBox { ...@@ -407,6 +407,26 @@ class RenderPositionedBox extends RenderAligningShiftedBox {
markNeedsLayout(); markNeedsLayout();
} }
@override
double computeMinIntrinsicWidth(double height) {
return super.computeMinIntrinsicWidth(height) * (_widthFactor ?? 1);
}
@override
double computeMaxIntrinsicWidth(double height) {
return super.computeMaxIntrinsicWidth(height) * (_widthFactor ?? 1);
}
@override
double computeMinIntrinsicHeight(double width) {
return super.computeMinIntrinsicHeight(width) * (_heightFactor ?? 1);
}
@override
double computeMaxIntrinsicHeight(double width) {
return super.computeMaxIntrinsicHeight(width) * (_heightFactor ?? 1);
}
@override @override
Size computeDryLayout(BoxConstraints constraints) { Size computeDryLayout(BoxConstraints constraints) {
final bool shrinkWrapWidth = _widthFactor != null || constraints.maxWidth == double.infinity; final bool shrinkWrapWidth = _widthFactor != null || constraints.maxWidth == double.infinity;
......
...@@ -55,6 +55,10 @@ void main() { ...@@ -55,6 +55,10 @@ void main() {
final RenderPositionedBox positioner = RenderPositionedBox(child: sizer, widthFactor: 1.0, heightFactor: 0.0); final RenderPositionedBox positioner = RenderPositionedBox(child: sizer, widthFactor: 1.0, heightFactor: 0.0);
layout(positioner, constraints: BoxConstraints.loose(const Size(200.0, 200.0))); layout(positioner, constraints: BoxConstraints.loose(const Size(200.0, 200.0)));
expect(positioner.computeMinIntrinsicWidth(200), equals(100.0));
expect(positioner.computeMaxIntrinsicWidth(200), equals(100.0));
expect(positioner.computeMinIntrinsicHeight(200), equals(0));
expect(positioner.computeMaxIntrinsicHeight(200), equals(0));
expect(positioner.size.width, equals(100.0)); expect(positioner.size.width, equals(100.0));
expect(positioner.size.height, equals(0.0)); expect(positioner.size.height, equals(0.0));
...@@ -62,6 +66,10 @@ void main() { ...@@ -62,6 +66,10 @@ void main() {
positioner.heightFactor = 0.5; positioner.heightFactor = 0.5;
pumpFrame(); pumpFrame();
expect(positioner.computeMinIntrinsicWidth(200), equals(50.0));
expect(positioner.computeMaxIntrinsicWidth(200), equals(50.0));
expect(positioner.computeMinIntrinsicHeight(200), equals(50.0));
expect(positioner.computeMaxIntrinsicHeight(200), equals(50.0));
expect(positioner.size.width, equals(50.0)); expect(positioner.size.width, equals(50.0));
expect(positioner.size.height, equals(50.0)); expect(positioner.size.height, equals(50.0));
...@@ -69,6 +77,10 @@ void main() { ...@@ -69,6 +77,10 @@ void main() {
positioner.heightFactor = null; positioner.heightFactor = null;
pumpFrame(); pumpFrame();
expect(positioner.computeMinIntrinsicWidth(200), equals(100.0));
expect(positioner.computeMaxIntrinsicWidth(200), equals(100.0));
expect(positioner.computeMinIntrinsicHeight(200), equals(100.0));
expect(positioner.computeMaxIntrinsicHeight(200), equals(100.0));
expect(positioner.size.width, equals(200.0)); expect(positioner.size.width, equals(200.0));
expect(positioner.size.height, equals(200.0)); expect(positioner.size.height, equals(200.0));
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment