首页 > 解决方案 > Flutter Web iFrame 奇怪的行为

问题描述

一个月前我的项目工作得很好,现在经过几次颤振更新,我的 iFrame 小部件在其所需的渲染框中居中是错误的。第一张图片是错误的行为,第二张是旧的工作版本。我正在使用一个插件,它可以防止分析器在使用platformViewRegistry时提示错误。下面是我的 iframe-widget 代码。有人知道如何解决这个问题吗?我不想降级到较旧的颤振版本。谢谢你的帮助!

PS:简单的 Center() 不起作用

错误的行为 正确的行为

我的 IFrame 小部件

// ignore: avoid_web_libraries_in_flutter
import 'dart:html';
// ignore: undefined_prefixed_name
import 'package:universal_ui/universal_ui.dart';

import 'package:flutter/material.dart';

class Iframe extends StatefulWidget {
  final String source;
  final Size size;
  Iframe(this.source, {this.size});
  @override
  _IframeState createState() => _IframeState();
}

class _IframeState extends State<Iframe> {
  Widget _iframeWidget;
  String source;
  @override
  void initState() {
    newFrame();
    super.initState();
  }

  void newFrame() async {
    print(widget.size);
    final String id = widget.source.hashCode.toString();
    final IFrameElement _iframeElement = IFrameElement();
    _iframeElement.height = widget.size?.height?.toString() ?? '500';
    _iframeElement.width = widget.size?.width?.toString() ?? '500';
    source = widget.source;
    _iframeElement.src = widget.source;
    _iframeElement.style.border = 'none';

    ui.platformViewRegistry
        .registerViewFactory(id, (int viewID) => _iframeElement);
    _iframeWidget = HtmlElementView(
      key: UniqueKey(),
      viewType: id,
    );
  }

  @override
  Widget build(BuildContext context) {
    if (source != widget.source) newFrame();
    return _iframeWidget;
  }
}

使用 IFrame 小部件

class ChatClientAnon extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamCacheBuilder<Package>(
      stream: Database().streamPackage(),
      builder: (data) => Container(
        child: Row(
          children: [
            Expanded(child: SmartphoneClient('Anonym', isAnon: true), flex: 2),
            Expanded(
                child: LayoutBuilder(
                    builder: (_, c) =>
                        Iframe(data.source, size: c.biggest)),
                flex: 8),
          ],
        ),
      ),
    );
  }
}

标签: flutterdartiframeflutter-webdart-html

解决方案


更新:问题在下一次颤振更新中得到解决


推荐阅读