syncfusion_flutter_pdfviewer未显示任何抖动

gv8xihay  于 2023-05-08  发布在  Flutter
关注(0)|答案(3)|浏览(118)

这是我的代码,我试图实现文档示例中给出的内容,但什么都看不见,我不知道问题出在哪里。我安装了依赖项。我正在试用IOS模拟器,我目前使用的是Mackbook Air M1。我该如何解决这个问题或者我应该尝试其他软件包

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';

class bookViewer extends StatefulWidget {
  // bookViewer({Key? key, required this.title}) : super(key: key);

  static const route = 'resources-tabs/book-resources/book-viewer';

  // final String title;

  @override
  _bookViewerState createState() => _bookViewerState();
}

class _bookViewerState extends State<bookViewer> {
  late PdfViewerController _pdfViewerController;
  final GlobalKey<SfPdfViewerState> _pdfViewerStateKey = GlobalKey();
  @override
  void initState() {
    _pdfViewerController = PdfViewerController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: SfPdfViewer.network(
          'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf',
          controller: _pdfViewerController,
          key: _pdfViewerStateKey),
      appBar: AppBar(
        actions: <Widget>[
          IconButton(
              onPressed: () {
                _pdfViewerStateKey.currentState!.openBookmarkView();
              },
              icon: Icon(
                Icons.bookmark,
                color: Colors.white,
              )),
          IconButton(
              onPressed: () {
                _pdfViewerController.jumpToPage(5);
              },
              icon: Icon(
                Icons.arrow_drop_down_circle,
                color: Colors.white,
              )),
          IconButton(
              onPressed: () {
                _pdfViewerController.zoomLevel = 1.25;
              },
              icon: Icon(
                Icons.zoom_in,
                color: Colors.white,
              ))
        ],
      ),
    ));
  }
}

下面是结果

的图像

7fyelxc5

7fyelxc51#

必须在appmanifest.xml文件中添加通过Internet访问该文件的用户权限

sbtkgmzw

sbtkgmzw2#

INTERNET权限添加到清单文件。
你必须添加这一行:

<uses-permission android:name="android.permission.INTERNET" />

在AndroidManifest.xml中的application标记外

yqlxgs2m

yqlxgs2m3#

这是一个页面,将帮助您查看PDF并下载它。

import 'package:ctevt_plus/page/home_page_body/notes/page/url_launcher_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'dart:io';

class PdfViewerAndDownloader extends StatefulWidget {
  String pdfURL;
  // = 'http://ctevt.org.np/uploads/docs/2020-05-11_Diploma%20in%20Forestry%202013.pdf';
  PdfViewerAndDownloader({Key? key, required this.pdfURL}) : super(key: key);

  @override
  State<PdfViewerAndDownloader> createState() => _PdfViewerAndDownloaderState();
}

class _PdfViewerAndDownloaderState extends State<PdfViewerAndDownloader> {
  late PdfViewerController _pdfViewerController;
  late PdfTextSearchResult _searchResult;

  Future<void> _downloadPdf() async {
    //show popup
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Downloading...'),
          content: LinearProgressIndicator(),
        );
      },
    );

    // download the file
    final response = await http.get(Uri.parse(widget.pdfURL));
    final directory = await getApplicationDocumentsDirectory();
    String fileName =
        widget.pdfURL.split('/').last.replaceAll(RegExp('%20'), ' ');
    print('fileName $fileName');
    final file = File('${directory.path}/$fileName');
    await file.writeAsBytes(response.bodyBytes);

    // remove popup
    Navigator.pop(context); // Hide the downloading popup
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text('$fileName downloaded.'),
      action: SnackBarAction(
        label: 'Go to \nDownloads',
        onPressed: () {
          Navigator.popUntil(context, (route) => false);
          Navigator.of(context).pushNamed('/downloads');
        },
      ),
    ));
  }

  @override
  void initState() {
    _pdfViewerController = PdfViewerController();
    _searchResult = PdfTextSearchResult();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Column(
          children: [
            const Text(
              'View and download',
              textScaleFactor: 0.8,
            ),
            Text(
              'Double tap to zoom',
              textScaleFactor: 0.6,
            )
          ],
        ),
        actions: <Widget>[
          IconButton(
            icon: const Icon(
              Icons.search,
              color: Colors.white,
            ),
            onPressed: () {
              _searchResult = _pdfViewerController.searchText(
                'the',
              );
              if (kIsWeb) {
                setState(() {});
              } else {
                _searchResult.addListener(() {
                  if (_searchResult.hasResult) {
                    setState(() {});
                  }
                });
              }
            },
          ),
          Visibility(
            visible: _searchResult.hasResult,
            child: IconButton(
              icon: const Icon(
                Icons.clear,
                color: Colors.white,
              ),
              onPressed: () {
                setState(() {
                  _searchResult.clear();
                });
              },
            ),
          ),
          Visibility(
            visible: _searchResult.hasResult,
            child: IconButton(
              icon: const Icon(
                Icons.keyboard_arrow_up,
                color: Colors.white,
              ),
              onPressed: () {
                _searchResult.previousInstance();
              },
            ),
          ),
          Visibility(
            visible: _searchResult.hasResult,
            child: IconButton(
              icon: const Icon(
                Icons.keyboard_arrow_down,
                color: Colors.white,
              ),
              onPressed: () {
                _searchResult.nextInstance();
              },
            ),
          ),
        ],
      ),
      body: SfPdfViewer.network(
        widget.pdfURL,
        controller: _pdfViewerController,
        currentSearchTextHighlightColor: Colors.yellow.withOpacity(0.6),
        otherSearchTextHighlightColor: Colors.yellow.withOpacity(0.3),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _downloadPdf();
        },
        tooltip: 'Download PDF',
        child: const Icon(Icons.download),
      ),
    );
  }
}

相关问题