1

我正在使用 Flutter 并尝试使用 file_picker 作为缩略图从手机本地存储的 PDF 中获取 pdf 第一个图像,并在屏幕上显示图像。当我运行这段代码时,一切正常,直到

文件选择器结果?结果 = 等待 FilePicker.platform.pickFiles(type: FileType.custom,allowedExtensions: ['pdf']);

问题是我可以选择文件,但图像没有返回。我不知道我的代码有什么问题。代码示例就在下面。请帮帮我。

import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:native_pdf_renderer/native_pdf_renderer.dart';
import 'constant.dart';
import 'file_picker.dart';
class Splashscreens extends StatefulWidget {                                
  _SplashscreensState createState() => _SplashscreensState();}                                                                      
 class _SplashscreensState extends State<Splashscreens> {
   PdfPageImage? image;
   void initState() {
        super.initState();
        getFile();
    }

    getFile() async {


FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: [
  'pdf'
]);
PlatformFile files = result!.files.first;

String pathes = files.path!;

final document = await PdfDocument.openFile('$pathes');

final page = await document.getPage(1);
final img = await page.render(width: page.width, height: page.height);              
setState(() {
  image = img;
 });}                                                                                 
@override                                                                                
Widget build(BuildContext context) {
return SafeArea(
    child: Scaffold(
        drawer: Drawer(
            child: ListView(
          padding: EdgeInsets.all(5),
          children: [
            DrawerHeader(decoration: BoxDecoration(color: Colors.cyan), child: Text(" Pdf Viewer", style: fontStyle)),
            ListTile(
                title: Text('Pick Files', style: styles),
                onTap: () {
                  FilePickers().PickFile();
                })
          ],
        )),
        appBar: AppBar(
          title: Text('Jensen Pdf Viewer', style: fontStyle),
        ),
        body: 
            Container(
          child: Column(
            children: [
              Expanded(
                child: Container(
                    margin: EdgeInsets.only(left: 3, right: 3),
                    decoration: BoxDecoration(
                        color: Color(0xFFA9C9CC),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black.withOpacity(0.5),
                            spreadRadius: 5,
                            blurRadius: 7,
                            offset: Offset(4, 3), // changes position of shadow
                          ),
                        ],
                        borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(40),
                          bottomRight: Radius.circular(40),
                        )),
                    child: Container(
                        margin: EdgeInsets.all(10),
                        padding: EdgeInsets.all(4),
                        child: Swiper(
                          itemBuilder: (BuildContext context, int index) {
                            return InkWell(
                                onTap: () async {
                                  await Future.delayed(const Duration(seconds: 1), () {
                                  
                                  });
                                },
                                child: Card(
                                  elevation: 20,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.all(Radius.circular(20)),
                                  ),
                                  child: Container(
                                   
                                    padding: EdgeInsets.all(10),
                                    width: MediaQuery.of(context).size.width * 0.8,
                                    height: MediaQuery.of(context).size.height * 0.8,
                                    decoration: BoxDecoration(
                                      image: DecorationImage(image: MemoryImage(image!.bytes),
                               fit: BoxFit.fill),
                                      borderRadius: BorderRadius.all(Radius.circular(20)),
                                    ),
                                  ),
                                ));
                          },
                          itemCount: 1,
                          itemWidth: 250.0,
                          layout: SwiperLayout.STACK,
                        ))),
              ),
              SizedBox(height: 10),
              Container(
                child: Text("Categories", style: fontStyle),
              ),
              SizedBox(height: 15),
             
              SizedBox(height: 20),
              
            ],
          ),
        )));}}
4

0 回答 0