【问题标题】:Prevent Flutter WebView from Overflowing a Horizontally Scrolling Carousel?防止 Flutter WebView 溢出水平滚动轮播?
【发布时间】:2021-02-12 11:01:58
【问题描述】:

我正在使用 Flutter 中的 Perspectives Pageview 库来实现水平轮播。当我使用图像或文本等渲染常规 Containers 时,一切正常。

但是,当我嵌入 WebView 时,它会溢出其父 Container,并在移动 Widget 时调整大小:

我不确定是什么原因造成的,以及如何修复它,以便它像其他元素一样留在父元素中。

我的代码:

Container(
      child: Center(
        // Adding Child Widget of Perspective PageView
        child: PerspectivePageView(
          hasShadow: true, // Enable-Disable Shadow
          shadowColor: Colors.black12,
          children: <Widget>[
            Container(
                child: Column(
                  children: [
                    Expanded(
                      child: Container(
                        color: Colors.black54,
                        child: Container(
                            color: Colors.white,
                            child: Column(
                              children: [
                                Expanded(
                                  child: WebViewPlus(
                                    onWebViewCreated: (controller) {
                                      this._controller = controller;
                                      controller
                                          .loadString(_htmlForCardsList[0]);
                                    },
                                    javascriptMode: JavascriptMode.unrestricted,
                                  ),
                                )
                              ],
                            )),
                        padding: EdgeInsets.all(2),
                      ),
                    ),
                    Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            "View Next",
                            style: TextStyle(
                                fontSize: 24.0,
                                fontWeight: FontWeight.w400,
                                color: Colors.orange),
                          ),
                        ],
                      ),
                      height: 60,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border(
                          top: BorderSide(color: Colors.black54, width: .3),
                          bottom: BorderSide(color: Colors.black54, width: 1.5),
                          left: BorderSide(color: Colors.black54, width: 1.5),
                          right: BorderSide(color: Colors.black54, width: 1.5),
                        ),
                      ),
                    ),
                  ],
                ),
                color: Colors.orange)])))

我还尝试将WebViewExpanded 父级替换为

Container(
          width: 300,
          height: 200,
          child: WebViewPlus(
          ...

我遇到了同样的问题。水平滚动轮播后,WebView 的大小会发生变化,这与轮播中包含的其他内容不同。

非常感谢您的任何见解。

【问题讨论】:

    标签: flutter dart mobile flutter-dependencies flutter-ios


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    你可以使用包https://pub.dev/packages/flutter_inappwebview
    代码sn-p

    Expanded(
          child: InAppWebView(
            initialUrl: url,
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                debuggingEnabled: true,
              ),
            ),
            onWebViewCreated:
                (InAppWebViewController controller) {
              webView = controller;
              print("onWebViewCreated");
              webView.loadData(
                  data: _htmlForCardsList[0]);
            },
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    import 'package:flutter_inappwebview/flutter_inappwebview.dart';
    import 'package:perspective_pageview/perspective_pageview.dart';
    
    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, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      InAppWebViewController webView;
      String url = "about:blank";
      double progress = 0;
      bool status = false;
    
      @override
      dispose() {
        webView.stopLoading();
        super.dispose();
      }
    
      List<String> _htmlForCardsList = [
        '''<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>'''
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Container(
              child: Center(
                  // Adding Child Widget of Perspective PageView
                  child: PerspectivePageView(
                      hasShadow: true, // Enable-Disable Shadow
                      shadowColor: Colors.black12,
                      children: <Widget>[
                Container(
                    child: Column(
                      children: [
                        Expanded(
                          child: Container(
                            color: Colors.black54,
                            child: Container(
                                color: Colors.white,
                                child: Column(
                                  children: [
                                    Expanded(
                                      child: InAppWebView(
                                        initialUrl: url,
                                        initialHeaders: {},
                                        initialOptions: InAppWebViewGroupOptions(
                                          crossPlatform: InAppWebViewOptions(
                                            debuggingEnabled: true,
                                          ),
                                        ),
                                        onWebViewCreated:
                                            (InAppWebViewController controller) {
                                          webView = controller;
                                          print("onWebViewCreated");
                                          webView.loadData(
                                              data: _htmlForCardsList[0]);
                                        },
                                        onLoadStart:
                                            (InAppWebViewController controller,
                                                String url) {
                                          print("start $status");
                                          status = false;
                                        },
                                        onLoadStop:
                                            (InAppWebViewController controller,
                                                String url) {
                                          print("stop $status");
                                          status = true;
                                        },
                                        onProgressChanged:
                                            (InAppWebViewController controller,
                                                int progress) {
                                          this.progress = progress / 100;
                                        },
                                      ),
                                    )
                                  ],
                                )),
                            padding: EdgeInsets.all(2),
                          ),
                        ),
                        Container(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                "View Next",
                                style: TextStyle(
                                    fontSize: 24.0,
                                    fontWeight: FontWeight.w400,
                                    color: Colors.orange),
                              ),
                            ],
                          ),
                          height: 60,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border(
                              top: BorderSide(color: Colors.black54, width: .3),
                              bottom: BorderSide(color: Colors.black54, width: 1.5),
                              left: BorderSide(color: Colors.black54, width: 1.5),
                              right: BorderSide(color: Colors.black54, width: 1.5),
                            ),
                          ),
                        ),
                      ],
                    ),
                    color: Colors.orange)
              ]))),
        );
      }
    }
    

    【讨论】:

    • 非常感谢!我似乎遇到了一些奇怪的事情:如果我创建一个新项目并在 Android 上运行它,它会按预期显示。但是,如果我在 iOS 上运行相同的代码,问题仍然存在。知道为什么会这样吗?
    • 我已将上述问题作为一个新问题添加到:stackoverflow.com/questions/64612099/…,因为您的答案是针对该库的,并且确实按要求将 WebView 放置在容器内,而其他问题似乎是将 WebView 放入 Transform 中时特定于 ios,并适用于此轮播库之外。感谢您的任何意见!
    猜你喜欢
    • 2019-08-17
    • 2021-04-03
    • 2022-01-03
    • 2020-03-16
    • 2019-08-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多