【问题标题】:Flutter: How to fit the web view size to screen?Flutter:如何使 Web 视图大小适合屏幕?
【发布时间】:2020-05-31 07:45:27
【问题描述】:

我是 Flutter 新手,

尝试在flutter中使用web view,但是web view的尺寸比屏幕大。

我想让网页大小适合手机屏幕大小。

我该怎么做?

这是我现在得到的:

我想看:

这是我的代码:

导入'package:flutter/material.dart';

导入'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,
        ));
  }
}

【问题讨论】:

    标签: flutter dart webview


    【解决方案1】:

    你可以试试我的插件flutter_inappwebview,这是一个 Flutter 插件,可以让你添加内联 WebViews 或打开应用内浏览器窗口,并且有很多事件、方法和选项来控制 WebViews。

    在您的情况下,您可以通过选项 preferredContentMode: UserPreferredContentMode.DESKTOP 启用 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 你安装插件了吗?还是您只是复制并粘贴了代码?您需要在 pubspec.yaml 文件中添加 flutter_inappwebview 依赖项
    • 我将 flutter_inappwebview: ^3.3.0 添加到 pubspec.yaml 文件中
    • 如果您有任何其他问题或错误,您可以在flutter_inappwebview github repository的问题部分发布。
    • 我收到此错误:[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:PlatformException(error, java.lang.IllegalStateException: Trying to use platform views with API 19、所需API等级为:20
    【解决方案2】:

    删除

    useWideViewPort: true,
    

    https://pub.dev/documentation/flutter_webview_plugin/latest/阅读更多文档

    现在看起来像

    【讨论】:

    • 查看截图
    • 用 webview_flutter 0.3.22+1 代替 flutter_webview_plugin 怎么样
    • 截图还是大了,我想看完整版
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多