flutter - 防止Flutter WebView溢出水平滚动轮播?

标签 flutter dart mobile flutter-dependencies flutter-ios

我正在使用Flutter中的Perspectives Pageview库来实现水平轮播。当我只用图像或文本等渲染常规Containers时,一切正常。
enter image description here
但是,当我嵌入WebView时,它溢出到其父容器之外,并在移动Widget时调整大小:
enter image description here
我不确定是什么原因造成的,以及如何解决它,使其像其他元素一样留在父级内部。
我的代码:

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)])))
我也尝试过将ExpandedWebView父级替换为
Container(
          width: 300,
          height: 200,
          child: WebViewPlus(
          ...
和我遇到同样的问题。水平滚动轮播时,WebView的大小会发生变化,这与轮播中包含的其他内容不同。
非常感谢您的见解。

最佳答案

您可以在下面复制粘贴运行完整代码
您可以使用https://pub.dev/packages/flutter_inappwebview软件包
程式码片段

Expanded(
      child: InAppWebView(
        initialUrl: url,
        initialHeaders: {},
        initialOptions: InAppWebViewGroupOptions(
          crossPlatform: InAppWebViewOptions(
            debuggingEnabled: true,
          ),
        ),
        onWebViewCreated:
            (InAppWebViewController controller) {
          webView = controller;
          print("onWebViewCreated");
          webView.loadData(
              data: _htmlForCardsList[0]);
        },
工作演示
enter image description here
完整的代码
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)
          ]))),
    );
  }
}

关于flutter - 防止Flutter WebView溢出水平滚动轮播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64600649/

相关文章:

flutter - 运行 flutter web 时出现错误,FormatException : Unexpected character (at character 1) <html>

flutter - Dart/Flutter 持续时间(毫秒)

flutter - Dart 编程中是否需要 getter 和 setter?

jquery - 使用子菜单 css/jquery 进行导航的移动和平板设备

java - 对于输入字符串: “Mat [ 0*0*CV_32FC1, isCont=true, isSubmat=false, nativeObj=0x78a0dff700, dataAddr=0x0 ]”-错误填充矩阵

flutter - 按钮 OnPressed 在 Flutter 中不起作用

android - Flutter - 'enableInteractiveSelection' 不适用于 EditableText

android - Flutter - 从现有的 android 项目导入

android - 如何在没有上下文的情况下在应用程序的任何位置显示对话框?

html - 如何在移动 View 中将 wordpress tesseract 主题中的按钮居中