simc / auto_size_text

Flutter widget that automatically resizes text to fit perfectly within its bounds.
https://pub.dev/packages/auto_size_text
MIT License
2.06k stars 240 forks source link

Support intrinsic dimensions (ditch LayoutBuilder) #77

Open creativecreatorormaybenot opened 3 years ago

creativecreatorormaybenot commented 3 years ago

The problem

So, AutoSizeText does not work using IntrinsicWidth or IntrinsicHeight because:

LayoutBuilder does not support returning intrinsic dimensions.

You might say: just do not use either of these widgets, however, I believe that that is a naive approach. The reason for this is that intrinsic sizing is an integral part of the Flutter framework. It can be used anywhere.

Example

We have a custom table widget. And because tables are intricate things, they will rely on intrisic sizes. We do not use IntrinsicWidth or IntrinsicHeight - we simply use the RenderBox contract:

There are four methods to implement for intrinsic sizing, to compute the minimum and maximum intrinsic width and height of the box. The documentation for these methods discusses the protocol in detail: computeMinIntrinsicWidth, computeMaxIntrinsicWidth, computeMinIntrinsicHeight, computeMaxIntrinsicHeight.

Consequences

If you have any AutoSizeText widget in a widget that makes use of the intrinsic sizing contract, AutoSizeText will completely break that widget because it uses LayoutBuilder.

Solution

Here is a solution that could work (have not thought it through yet):

We should probably rewrite AutoSizeText to ditch LayoutBuilder.
We could do this by writing our own RenderBox (leaf render object) subclass. This would allow us to fulfill the intrinsic sizing contract and also properly perform the layout.
We would be able to directly access the text painter instead of proxying through the layout builder.

Related issues

luqmanhakem commented 3 years ago

Hello @leisim, is there any future plan for this enhancement or hack turnaround solution?

Much thanks!

simc commented 3 years ago

@creativecreatorormaybenot @luqmanhakem This seems like a problem we should fix. I probably won't have time to implement it in the near future but I'll review any PR proposal as quickly as possible.

ciriousjoker commented 1 year ago

This also means AutoSize can't be used inside SliverFillRemaining :(

cyberpwnn commented 1 month ago

Using a layoutbuilder to find the width i can "Take" to auto size button actions on the app bar and overflow the rest to a ... more menu.

I cant do this because there is an intrinsic height sizing the app bar in my ui framework. I dont even care about the height constraint in my layout builder, even though I agree with the issue, my situation is just dumb, i should be able to use intrinsic height and get the width constraint at later.