3

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

Samson
  • 31
  • 1
  • 5
  • 1
    make sure you load the mobile version of the website if available. – Anirudh Bagri May 19 '20 at 07:54
  • May I ask how I can get webview to fetch mobile version of the site when available? Something like this? Or is there a way to limit the width so it will automatically show me the mobile version of the site? – Samson May 19 '20 at 10:38
  • I tried `initialUrl: "https://www.businessinsider.jp/"` with my plugin [flutter_inappwebview](https://github.com/pichillilorenzo/flutter_inappwebview) (which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews) using the base example available in the `README.md` of the Github repository and it fits correctly the screen. – Lorenzo Pichilli May 31 '20 at 12:28

2 Answers2

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.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65
0

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
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37