I followed the approach from this https://community.flutterflow.io/ask-the-community/post/dynamic-webview-height-sDXL6NpzqOXlIbc and implemented the following code:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:async';
import 'dart:html' as html;
Future<double> calculateWebViewHeight(String bodyContent) async {
final completer = Completer<double>();
final div = html.DivElement();
div.style.visibility = 'hidden';
div.style.position = 'absolute';
div.style.width = '100%';
div.setInnerHtml(bodyContent, validator: html.NodeValidatorBuilder.common());
html.document.body?.append(div);
Future.delayed(Duration(milliseconds: 50), () {
final height = div.offsetHeight.toDouble();
completer.complete(height);
div.remove();
});
return completer.future;
}
This solution calculates the height of the HTML content by creating an invisible div
and reading its offsetHeight
. It works perfectly on the web, but unfortunately, it doesn't work on mobile devices.