Home > database >  Flutter: is it possible for platformView to have automatic size/height?
Flutter: is it possible for platformView to have automatic size/height?

Time:12-19

I need to use the native UITextView on iOS and similar equivalent on Android because it provides lots of options such as "Look up", "Translate" etc on selected text.

I am successfully able to use platform views in flutter for UITextView on iOS. However, I am wondering if it’s possible to automatic size it? I have disabled scrolling on the UITextView on the iOS side. This is usually what’s required to have it size automatically. On the flutter side, I have tried enclosing it in Flexible but it doesn’t auto size. It appears to take as much space available to it.

Is it possible to have it auto size when the platform view has intrinsic size?

CodePudding user response:

I was able to figure it out.

I wrapped the entire thing inside a SizedBox which is inside a SingleChildScrollView which is inside a Flexible as such:

Flexible(
  child: SingleChildScrollView(
    scrollDirection: Axis.vertical,
    child: SizedBox(
      height: height,
      child: UiKitView(
        viewType: viewType,
        layoutDirection: TextDirection.ltr,
        creationParams: creationParams,
        creationParamsCodec: const StandardMessageCodec(),
      ),
    ),
  ),
);

The height is a variable. I start the UITextView with a zero height, use the layoutSubviews on the native side to get changes to the size of the view, then transfer it over to flutter side over a channel and once received on flutter side, I check if the new height is different than the previous height, and if so, I use setState to reset the height of the SizedBox.

It works surprisingly well!

  • Related