首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SingleChildScrollView中的Flutter InAppWebView

SingleChildScrollView中的Flutter InAppWebView
EN

Stack Overflow用户
提问于 2021-11-04 08:42:24
回答 2查看 349关注 0票数 0

我有一个要求,使卡包含一个网页视图,后面跟着更多的小部件。视图应该类似于this

我做了这样的实现:

代码语言:javascript
复制
SingleChildScrollView(
  ...
  child: Column(
    children: [
      Container(
        ...
        child: Column(
          children: [
            SizedBox(
              height: _webviewHeightSetInOnLoadStop
              child: InAppWebview(
                ...
              )
            ),
            ...
          )
      ),
      Widget1(),
      Widget2(),
      Widget3(),
    ]

其中_webviewHeightSetInOnLoadStop的设置如下:

代码语言:javascript
复制
  onLoadStop: (controller, url) async {
    final height = await controller.evaluateJavascript(
      source: "document.documentElement.scrollHeight;",
    );
    ...
    setState(() {
      _webviewHeightSetInOnLoadStop = height;
      ...
    });
  }

这个实现的问题是,当webview太大时,Android会崩溃,并显示:

代码语言:javascript
复制
IllegalStateException: Unable to create a layer for InAppWebView, size 960x39192 exceeds max size 16384

据我所知,这是由于webview的高度引起的,这是系统限制的。

所以我希望webview是可滚动的,它的容器有一个固定的高度,比屏幕稍大一点(当需要的时候),当webview的滚动结束时,在任何一个方向上都会将拖动事件传递给SingleChildScrollView

EN

回答 2

Stack Overflow用户

发布于 2021-11-15 09:17:40

还有另一个插件可以做你想要做的事情

代码语言:javascript
复制
webview_flutter_plus: ^0.2.3

试试这段代码

代码语言:javascript
复制
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:webview_flutter_plus/webview_flutter_plus.dart';

class ImageEditor extends StatefulWidget {
  const ImageEditor({Key? key}) : super(key: key);

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

class _ImageEditorState extends State<ImageEditor> {
  WebViewPlusController? _controller;
  double _height = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height:MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(
                height: _height,
                child: WebViewPlus(
                  onWebViewCreated: (controller) {
                    this._controller = controller;      controller.loadUrl('https://pub.dev/packages/webview_flutter_plus');
              },
              onPageFinished: (url) {

                _controller!.getHeight().then((double height) {
                  print("Height:  " + height.toString());
                  setState(() {
                    _height = height;
                  });
                });
              },
              javascriptMode: JavascriptMode.unrestricted,
            ),
          ),
          Container(
            height:200,
            width: MediaQuery.of(context).size.width,
            color: Colors.red,
          ),
          Container(
            height:200,
            width: MediaQuery.of(context).size.width,
            color: Colors.black,
          ),
          Container(
            height:200,
            width: MediaQuery.of(context).size.width,
            color: Colors.green,
            ),
           ],
          ),
        ),
      ),
    );
  }
}
票数 0
EN

Stack Overflow用户

发布于 2021-11-21 19:09:47

我认为你应该实现一个自定义的大小,或者只是限制你的webview容器的预期行为,并使其具有响应性,这样屏幕就不会崩溃,或者像旧设备那样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69836438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档