Created
January 3, 2021 00:39
-
-
Save matthew-carroll/65411529a5fafa1b527a25b7130187c6 to your computer and use it in GitHub Desktop.
DryIntrinsicWidth and DryIntrinsicHeight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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; | |
} | |
} | |
} |
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
For null-safety version: