'Flutter / webview_flutter too big to fit screen

I am running flutter 1.17.1, using webview_flutter: ^0.3.21 dependencies added to pubspec.yaml and added this to the end of info.plist

<key>io.flutter.embedded_views_preview</key>
    <string>YES</string>

Problem: Webpage loaded into webview is too big to fit mobile phone screen. screenshot

Here is the code with the webview:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewContainer extends StatefulWidget {
  final url;
  WebViewContainer(this.url);
  @override
  createState() => _WebViewContainerState(this.url);
}

class _WebViewContainerState extends State<WebViewContainer> {
  var _url;
  final _key = UniqueKey();
  _WebViewContainerState(this._url);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: Column(
          children: [
            Expanded(
                child: WebView(
                    key: _key,
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: _url))

          ],
        ));
  }
}

Link to the full app: https://github.com/bi-samson/mreader



Solution 1:[1]

I've tried a minimal repro with the url "https://www.businessinsider.jp/" and the latest version of webview_flutter, this is the output:

enter image description here enter image description here

Minimal repro:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:async';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: WebView(
        initialUrl: "https://www.businessinsider.jp/",
        onWebViewCreated: (WebViewController webViewController) {
          _controller.complete(webViewController);
        },
        javascriptMode: JavascriptMode.unrestricted,
      ),
    );
  }
}

It seems that the issue doesn't occur in the latest version.

Solution 2:[2]

Use InAppWebView instead of WebView. This will add a line like this to your package's pubspec.yaml:

dependencies:
  flutter_inappwebview: ^5.3.2

Here is a code example:

return InAppWebView(
      initialOptions: InAppWebViewGroupOptions(
          android: AndroidInAppWebViewOptions(
              useHybridComposition: true,
              textZoom: 100 * 2 // it makes 2 times bigger
          )
      ),
      onWebViewCreated: (InAppWebViewController controller) {
        webViewController = controller;
        controller.loadData(data: widget.html, mimeType: "text/html");
      },
    );

If you want to make it smaller, you just need to divide it as below:

textZoom: 100 / 2 // it makes 2 times smaller

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 MαπμQμαπkγVπ.0
Solution 2 Fakhriddin Abdullaev