如何在小部件容器内显示pdf [flutter-web]

时间:2020-11-09 06:53:13

标签: flutter pdf flutter-web

我想在flutter-web项目中打开pdf文件,只要我知道...当我打开pdf时,它总是将我导航到新标签页,有没有一种方法可以在小部件容器中显示pdf扑? 所以,到目前为止,我只能通过这样做打开pdf文件

html.window.open('http://xxxx.pdf',"my_pdf");

我仍然不知道如何将其放入窗口小部件内

1 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
在演示代码中,我在blue border上添加了Container
代码段

class Iframe extends StatelessWidget {
  Iframe() {
    // ignore: undefined_prefixed_name
    ui.platformViewRegistry.registerViewFactory('iframe', (int viewId) {
      var iframe = html.IFrameElement();
      iframe.src = 'http://www.africau.edu/images/default/sample.pdf';
      return iframe;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(border: Border.all(color: Colors.blueAccent)),
        width: 400,
        height: 300,
        child: HtmlElementView(viewType: 'iframe'));
  }
}

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';
// import 'dart:io' if (dart.library.html) 'dart:ui' as ui;
import 'dart:ui' as ui;
import 'dart:html' as html;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Iframe()),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class Iframe extends StatelessWidget {
  Iframe() {
    // ignore: undefined_prefixed_name
    ui.platformViewRegistry.registerViewFactory('iframe', (int viewId) {
      var iframe = html.IFrameElement();
      iframe.src = 'http://www.africau.edu/images/default/sample.pdf';
      return iframe;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(border: Border.all(color: Colors.blueAccent)),
        width: 400,
        height: 300,
        child: HtmlElementView(viewType: 'iframe'));
  }
}