无法从Flutter中的素材资源获取PDF文件,如何直接获取PDF

时间:2020-04-09 17:26:46

标签: pdf flutter pdf-viewer

我已附上代码。如何从资产中获取PDF?我认为调用init函数可以解决问题,但我称它不是完美的。我想不使用任何按钮或其他东西来获取pdf。 因此,我需要在Class InformationTechnology调用后立即直接获取它。

class InformationTechnology extends StatefulWidget {
  @override
  _InformationTechnologyState createState() => _InformationTechnologyState();
}  

class InformationTechnology extends StatefulWidget {
      @override
      _InformationTechnologyState createState() => _InformationTechnologyState();
    }

    class _InformationTechnologyState extends State<InformationTechnology> {
      String pdfasset = "assets/IT.pdf";
      PDFDocument _doc;
      bool _loading = false;
      @override
      void initState() { 
        super.initState();

      }
      _initPDF()async{
        setState(() {
          _loading = true;
        });
        final doc = await PDFDocument.fromAsset(pdfasset);
        setState(() {
          _doc = doc;
          _loading = false;
        });
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
           title: Text("Introduction to Information Technology"),
           centerTitle: true,
           backgroundColor: Colors.blueAccent,
          ),
          body: _loading?
          Center(
           child: CircularProgressIndicator(),
          ):PDFViewer(document: _doc), 
          );
      }
    }

0 个答案:

没有答案