Skip to content

Instantly share code, notes, and snippets.

@matthew-carroll
Created January 3, 2021 00:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthew-carroll/65411529a5fafa1b527a25b7130187c6 to your computer and use it in GitHub Desktop.
Save matthew-carroll/65411529a5fafa1b527a25b7130187c6 to your computer and use it in GitHub Desktop.
DryIntrinsicWidth and DryIntrinsicHeight
/// Same as `IntrinsicWidth` except that when this widget is instructed
/// to `computeDryLayout()`, it doesn't invoke that on its child, instead
/// it computes the child's intrinsic width.
///
/// This widget is useful in situations where the `child` does not
/// support dry layout, e.g., `TextField` as of 01/02/2021.
class DryIntrinsicWidth extends SingleChildRenderObjectWidget {
const DryIntrinsicWidth({Key key, Widget child})
: super(key: key, child: child);
@override
RenderDryIntrinsicWidth createRenderObject(BuildContext context) =>
RenderDryIntrinsicWidth();
}
class RenderDryIntrinsicWidth extends RenderIntrinsicWidth {
@override
Size computeDryLayout(BoxConstraints constraints) {
if (child != null) {
final width = child.computeMinIntrinsicWidth(constraints.maxHeight);
final height = child.computeMinIntrinsicHeight(width);
return Size(width, height);
} else {
return Size.zero;
}
}
}
/// Same as `IntrinsicHeight` except that when this widget is instructed
/// to `computeDryLayout()`, it doesn't invoke that on its child, instead
/// it computes the child's intrinsic height.
///
/// This widget is useful in situations where the `child` does not
/// support dry layout, e.g., `TextField` as of 01/02/2021.
class DryIntrinsicHeight extends SingleChildRenderObjectWidget {
const DryIntrinsicHeight({Key key, Widget child})
: super(key: key, child: child);
@override
RenderDryIntrinsicHeight createRenderObject(BuildContext context) =>
RenderDryIntrinsicHeight();
}
class RenderDryIntrinsicHeight extends RenderIntrinsicHeight {
@override
Size computeDryLayout(BoxConstraints constraints) {
if (child != null) {
final height = child.computeMinIntrinsicHeight(constraints.maxWidth);
final width = child.computeMinIntrinsicWidth(height);
return Size(width, height);
} else {
return Size.zero;
}
}
}
@fzyzcjy
Copy link

fzyzcjy commented Jul 30, 2021

For null-safety version:

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

/// NOTE ref: https://github.com/flutter/flutter/issues/71687 & https://gist.github.com/matthew-carroll/65411529a5fafa1b527a25b7130187c6
/// Same as `IntrinsicWidth` except that when this widget is instructed
/// to `computeDryLayout()`, it doesn't invoke that on its child, instead
/// it computes the child's intrinsic width.
///
/// This widget is useful in situations where the `child` does not
/// support dry layout, e.g., `TextField` as of 01/02/2021.
class DryIntrinsicWidth extends SingleChildRenderObjectWidget {
  const DryIntrinsicWidth({Key? key, Widget? child}) : super(key: key, child: child);

  @override
  RenderDryIntrinsicWidth createRenderObject(BuildContext context) => RenderDryIntrinsicWidth();
}

class RenderDryIntrinsicWidth extends RenderIntrinsicWidth {
  @override
  Size computeDryLayout(BoxConstraints constraints) {
    if (child != null) {
      final width = child!.computeMinIntrinsicWidth(constraints.maxHeight);
      final height = child!.computeMinIntrinsicHeight(width);
      return Size(width, height);
    } else {
      return Size.zero;
    }
  }
}

/// NOTE ref: https://github.com/flutter/flutter/issues/71687 & https://gist.github.com/matthew-carroll/65411529a5fafa1b527a25b7130187c6
/// Same as `IntrinsicHeight` except that when this widget is instructed
/// to `computeDryLayout()`, it doesn't invoke that on its child, instead
/// it computes the child's intrinsic height.
///
/// This widget is useful in situations where the `child` does not
/// support dry layout, e.g., `TextField` as of 01/02/2021.
class DryIntrinsicHeight extends SingleChildRenderObjectWidget {
  const DryIntrinsicHeight({Key? key, Widget? child}) : super(key: key, child: child);

  @override
  RenderDryIntrinsicHeight createRenderObject(BuildContext context) => RenderDryIntrinsicHeight();
}

class RenderDryIntrinsicHeight extends RenderIntrinsicHeight {
  @override
  Size computeDryLayout(BoxConstraints constraints) {
    if (child != null) {
      final height = child!.computeMinIntrinsicHeight(constraints.maxWidth);
      final width = child!.computeMinIntrinsicWidth(height);
      return Size(width, height);
    } else {
      return Size.zero;
    }
  }
}

@V3ntus
Copy link

V3ntus commented Jun 4, 2023

Three years and Flutter still hasn't fixed this. Thanks for this workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment