WebView里面没有高度滚动不起作用

时间:2019-11-04 13:23:35

标签: flutter webview

在Flutter上...我试图在所有支持滚动的可用容器中添加Webview,但在Android上不起作用。

ListView(
          shrinkWrap: true,
          children: <Widget>[
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
           Expanded(
                child: WebView(
                    key: Key("webview1"),
                    debuggingEnabled:  true,
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: "https://flutter.dev/")),
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
          ],
)

3 个答案:

答案 0 :(得分:2)

如果您想将可滚动的WebView放在其他窗口小部件之间,并且WebView将是可滚动的,而其他窗口小部件则不是可滚动的,那么只需将ListView for Column更改为这样:

Column(
  children: <Widget>[
    Text("hiiiiiiiiiiiii "),
    Text("hiiiiiiiiiiiii "),
    Container(
      height: 300,
      child: WebView(
          key: Key("webview1"),
          debuggingEnabled: true,
          javascriptMode: JavascriptMode.unrestricted,
          initialUrl: "https://flutter.dev/")
    ),
    Text("hiiiiiiiiiiiii "),
    Text("hiiiiiiiiiiiii "),
)

但是,如果您想在WebView上添加一些可滚动的小部件,请尝试 CustomScrollViewSliverList

您能说出您要做出什么确切的逻辑吗?

附加项:

如果要在其中具有可滚动区域和一些WebView,则必须知道WebView的高度:

ListView(
  physics: ClampingScrollPhysics(),
  children: <Widget>[
    Text("hiiiiiiiiiiiii "),
    Text("hiiiiiiiiiiiii "),
    ConstrainedBox(
      constraints: BoxConstraints(maxHeight: 10000),
        child: WebView(
          gestureRecognizers: Set()
            ..add(
              Factory<VerticalDragGestureRecognizer>(
                  () => VerticalDragGestureRecognizer(),
              ), // or null
            ),
            key: Key("webview1"),
            debuggingEnabled: true,
            javascriptMode: JavascriptMode.unrestricted,
            initialUrl: "https://flutter.dev/")),
    Text("hiiiiiiiiiiiii "),
    Text("hiiiiiiiiiiiii "),
  ],
);

您可以动态获得的WebView的高度,灵感在这里: https://gist.github.com/PonnamKarthik/877a90917a576ecff613d5169680d02c

答案 1 :(得分:1)

谢谢您提供解决方案的要诀。github.com/ PonnamKarthik / 877a90917a576ecff613d5169680d02c

问题已解决,现在我们可以在父级可滚动容器内添加Webview。

导入'dart:convert';

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Code Sample',
      builder: (BuildContext context, Widget navigator) {
        return MyStatelessWidget();
      },
    );
  }
}

class MyStatelessWidget extends StatefulWidget {
  MyStatelessWidget({Key key}) : super(key: key);

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

class _MyStatelessWidgetState extends State<MyStatelessWidget> {
  List<WebViewController> _listController = List();

  List<double> _heights =
      List<double>.generate(htmlStrings.length, (int index) => 20.0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        shrinkWrap: true,
        itemCount: htmlStrings.length,
        itemBuilder: (context, index) {
          return Container(
            height: _heights[index] ?? 100.0,
            child: WebView(
              initialUrl:
                  'data:text/html;base64,${base64Encode(const Utf8Encoder().convert(htmlStrings[index]))}',
              onPageFinished: (some) async {
                double height = double.parse(await _listController[index]
                    .evaluateJavascript(
                        "document.documentElement.scrollHeight;"));
                setState(() {
                  _heights[index] = height;
                });
              },
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (controller) async {
                _listController.add(controller);
              },
            ),
          );
        },
      ),
    );
  }
}

final List<String> htmlStrings = [
  '''<p>Table shows some compounds and the name of their manufacturing process</p>
<table style="border-collapse: collapse; width: 100%; height: 85px;" border="1">
<tbody>
<tr style="height: 17px;">
<td style="width: 50%; text-align: center; height: 17px;">Compounds/Elements</td>
<td style="width: 50%; text-align: center; height: 17px;">Manufacture process</td>
</tr>
<tr style="height: 17px;">
<td style="width: 50%; height: 17px;">Nitric acid</td>
<td style="width: 50%; height: 17px;">Ostwald's process</td>
</tr>
<tr style="height: 17px;">
<td style="width: 50%; height: 17px;">Ammonia</td>
<td style="width: 50%; height: 17px;">Haber's process</td>
</tr>
<tr style="height: 17px;">
<td style="width: 50%; height: 17px;">Sulphuric acid</td>
<td style="width: 50%; height: 17px;">Contact process</td>
</tr>
<tr style="height: 17px;">
<td style="width: 50%; height: 17px;">Sodium</td>
<td style="width: 50%; height: 17px;">Down's process</td>
</tr>
</tbody>
</table>''',
  '''<p>\(L=[M{L }^{2 }{T }^{-2 }{A }^{-2 }]\)</p>
<p>\(C=[{M }^{-1 }{L }^{-2 }{T }^{4 }{A }^{2 }]\)</p>
<p>\(R=[M{L }^{2 }{T }^{-3 }{A }^{-2 }]\)</p>
<p>\(\therefore \frac {R}{L}=\frac{[M{L }^{2 }{T }^{-2 }{A }^{-2 }]}{[M{L }^{2 }{T }^{-3 }{A }^{-2 }]}=[T]\)</p>''',
  '''<p>Displacement(s)\(=\left(13.8\pm0.2\right)m\)</p>
<p>Time(t)\(=\left(4.0\pm0.3\right)s\)</p>
<p>Velocity(v)\(=\frac{13.8}{4.0}=3.45m{s}^{-1}\)</p>
<p>\(\frac{\delta v}{v}=\pm\left(\frac{\delta s}{s}+\frac{\delta t}{t}\right)=\pm\left(\frac{0.2}{13.8}+\frac{0.3}{4.0}\right)=\pm0.0895\)</p>
<p>\(\delta v =\pm0.0895*3.45=\pm0.3\)(rounding off to one place of decimal)</p>
<p>\(v=\left(3.45\pm0.3\right)m{s}^{-1}\)</p>''',
  '''<p>The only real numbers satisfying \(x^2=4\) are 2 and -2. But none of them satisfy the final condition, \(x+2=3\). So, there is no real number such that these conditions are met. Hence this is null set.</p>
<p>Note that, for \(x\) to be a memner of \(\{x:p(x),q(x)\}\),&nbsp;<em><strong>both</strong></em> \(p(x)\) and \(q(x)\) should be true.</p>''',
];

答案 2 :(得分:1)

制作一个完整的可滚动小部件,其中包含WebView(具有动态高度)以及其上方和下方的其他容器。所有这些都应在父窗口小部件内滚动

请确保已正确安装flutter软件包:webview_flutter: ^1.0.5

1.create Future fxn

               `String hight;
                double webHight =400; //default some value
           Future funcThatMakesAsyncCall() async{
                  var result =
                 await controller.evaluateJavascript('document.body.scrollHeight'); 
    //here we call javascript for get browser data
                    setState(() {
                                hight = result;
                                 },
                                );
                                              }

`

2。我们使用SingleChildScrollView scrollDirection:Axis.vertical,

3。内部使用Column

4。最后,我们使用Conatiner

Container内,我们使用WebView

此处Container代码

               `Container(
                height: webHight+20,
                child: WebView(
                initialUrl: widget.url,
                onPageFinished: (webhight)async{
                   await funcThatMakesAsyncCall();
                   setState((){
                      webHight= double.parse(hight);                          
                   });                        
                  },
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (WebViewController webViewController) {
                  
                  controller = webViewController;

                  },
                ),
              ),`