'Why in Flutter, PlatformView cannot shrink to its own size?

For example:

child: Row(
  children: [
    const Expanded(child: AndroidNativeView()),
    ElevatedButton(
        child: const Text("PLUS"))
  ],
)

In layouts without parent constraints like Flex(Row/Column), The AndroidNativeView (a PlatformViewLink) cannot determine its size thus an exception will be thrown. So the instant solutions are wrapping it inside an Expanded/Flexible, or a Container-like widget with width/height hardcoded. Both solutions are not flexible for the layout.

From the current Dart-side implementations' perspective, I know the sizedByParent function of PlatformViewRenderBox is overridden to true. So it takes the constraint from parent, without measuring itself, and fill up whatever space it gets.

What I don't understand is, since there is "WRAP_CONTENT" on Android side, what's the reason that they didn't make it possible to shrink a PlatformView to its own size in Flutter? What are the technical difficulties/tradeups?



Solution 1:[1]

If you use a Flex(Row/Column) in a constraints independent width like if you don't provide width/height to your container then it's child has right to decide that how much space it want to cover in Row/Column we've a property named mainAxisSize by default it is set to MainAxisSize.max but you can set it to min as well then Row/Column will also cover space whatever is needed by their children.

But if you provide constraints to the parent widget then as you know already that in Flutter constraints goes parent to child so it will cover all the available space.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Diwyansh