Flutter:如何将Web视图大小调整为屏幕大小?

3

我是Flutter的新手,尝试在Flutter中使用web view,但是web view的大小超过了屏幕大小。

我想把网页尺寸调整为手机屏幕大小。

我该怎么做呢?

以下是我的代码和当前屏幕截图:

enter image description here

以下是我想要看到的:

enter image description here

这是我的代码:

import 'package:flutter/material.dart';

import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class CoursesInformation extends StatefulWidget {
  @override
  _WebViewExampleState createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<CoursesInformation> {
  TextEditingController controller = TextEditingController();
  FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
  var urlString = "https://shoham.biu.ac.il/BiuCoursesViewer/MainPage.aspx";

  launchUrl() {
    setState(() {
      urlString = controller.text;
     flutterWebviewPlugin.reloadUrl(urlString);
       flutterWebviewPlugin.launch(urlString,
              rect: new Rect.fromLTWH(
                  0.0, 
                  0.0, 
                  MediaQuery.of(context).size.width, 
                  300.0));
    });
  }

  @override
  void initState() {
    super.initState();

    flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged wvs) {
      print(wvs.type);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Directionality(
    textDirection: TextDirection.rtl,
    child: WebviewScaffold(
      appBar: AppBar(
        title: Text('מידע על קורסים'),
        centerTitle: true
      ),
      url: urlString,
      withZoom: true,
      scrollBar: true,
      withJavascript: true,
      useWideViewPort: true,

      withLocalStorage: true,
        ));
  }
}
3个回答

7
你可以尝试我的插件flutter_inappwebview,它是一个Flutter插件,允许你添加内联WebViews或打开应用内浏览器窗口,具有许多事件、方法和选项来控制WebViews。
在你的情况下,你可以通过选项preferredContentMode: UserPreferredContentMode.DESKTOP启用“桌面”模式(就像常规浏览器如Chrome等一样)。
这里是一个使用你的URL的示例:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  InAppWebViewController webView;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
              child: InAppWebView(
            initialUrl:
                "https://shoham.biu.ac.il/BiuCoursesViewer/MainPage.aspx",
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                  debuggingEnabled: true,
                  preferredContentMode: UserPreferredContentMode.DESKTOP),
            ),
            onWebViewCreated: (InAppWebViewController controller) {
              webView = controller;
            },
            onLoadStart: (InAppWebViewController controller, String url) {

            },
            onLoadStop: (InAppWebViewController controller, String url) async {

            },
          ))
        ])),
      ),
    );
  }
}

这是屏幕截图:

在此输入图片描述


当我导入以下内容时出现错误:import 'package:flutter_inappwebview/flutter_inappwebview.dart' - einav
如果您遇到任何其他问题或错误,可以在flutter_inappwebview github 存储库的问题部分发布。 - Lorenzo Pichilli
请@einav,阅读插件的文档和要求。明确写明InAppWebView需要Android 20+。如果您想使用InAppWebView小部件,则这是最低的Android版本。不幸的是,这不取决于我,而是取决于官方AndroidView API在Flutter小部件树中嵌入本地视图。如果您需要支持较低的Android版本,则应使用InAppBrowserChromeSafariBrowser - Lorenzo Pichilli
使用此插件及其选项需要仅支持iOS 13+。 - GGirotto
flutter_inappwebview 目前存在许多问题,且已经不再维护。请参考 https://github.com/pichillilorenzo/flutter_inappwebview/issues。 - Pawan
显示剩余4条评论

1

我使用了InAppWebView,这对我很有用。这将在您的包的pubspec.yaml文件中添加如下行:

dependencies:
  flutter_inappwebview: ^5.3.2

例如:

return InAppWebView(
      initialOptions: InAppWebViewGroupOptions(
          android: AndroidInAppWebViewOptions(
              useHybridComposition: true,
              textZoom: 100 * 2 // it makes 2 times bigger
          )
      ),
      onWebViewCreated: (InAppWebViewController controller) {
        webViewController = controller;
      },
    );

如果您想使其更小,只需要按以下方式进行划分:
textZoom: 100 / 2 // it makes 2 times smaller

0

请检查截图。 - Dev
1
使用webview_flutter 0.3.22+1代替flutter_webview_plugin怎么样? - Dev
截图仍然很大,我想要看到整个页面。 - einav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接