-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
615b5a0
commit c175ca7
Showing
12 changed files
with
105 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,68 @@ | ||
import 'package:enough_media/media_provider.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; | ||
import 'package:native_pdf_view/native_pdf_view.dart'; | ||
|
||
/// Displays PDFs | ||
class PdfInteractiveMedia extends StatefulWidget { | ||
final MediaProvider mediaProvider; | ||
PdfInteractiveMedia({Key key, @required this.mediaProvider}) | ||
PdfInteractiveMedia({Key? key, required this.mediaProvider}) | ||
: super(key: key); | ||
|
||
@override | ||
_PdfInteractiveMediaState createState() => _PdfInteractiveMediaState(); | ||
} | ||
|
||
class _PdfInteractiveMediaState extends State<PdfInteractiveMedia> { | ||
PdfViewerController pdfViewerController; | ||
OverlayEntry overlayEntry; | ||
late PdfController pdfController; | ||
|
||
@override | ||
void initState() { | ||
pdfViewerController = PdfViewerController(); | ||
pdfController = PdfController(document: initDocument(widget.mediaProvider)); | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final provider = widget.mediaProvider; | ||
Future<PdfDocument> initDocument(final MediaProvider provider) { | ||
if (provider is MemoryMediaProvider) { | ||
return SfPdfViewer.memory( | ||
provider.data, | ||
controller: pdfViewerController, | ||
onTextSelectionChanged: onTextSelectionChanged, | ||
); | ||
} else if (provider is UrlMediaProvider) { | ||
return SfPdfViewer.network( | ||
provider.url, | ||
controller: pdfViewerController, | ||
onTextSelectionChanged: onTextSelectionChanged, | ||
); | ||
return PdfDocument.openData(provider.data); | ||
// } else if (provider is UrlMediaProvider) { | ||
// provider.url | ||
// ); | ||
} else if (provider is AssetMediaProvider) { | ||
return SfPdfViewer.asset( | ||
provider.assetName, | ||
controller: pdfViewerController, | ||
onTextSelectionChanged: onTextSelectionChanged, | ||
); | ||
return PdfDocument.openAsset(provider.assetName); | ||
} else { | ||
throw StateError('Unsupported media provider $provider'); | ||
} | ||
} | ||
|
||
void onTextSelectionChanged(PdfTextSelectionChangedDetails details) { | ||
if (details.selectedText == null && overlayEntry != null) { | ||
overlayEntry.remove(); | ||
overlayEntry = null; | ||
} else if (details.selectedText != null && overlayEntry == null) { | ||
final OverlayState overlayState = Overlay.of(context); | ||
overlayEntry = OverlayEntry( | ||
builder: (context) => Positioned( | ||
top: details.globalSelectedRegion.center.dy - 55, | ||
left: details.globalSelectedRegion.bottomLeft.dx, | ||
child: RaisedButton( | ||
child: Text('Copy', style: TextStyle(fontSize: 17)), | ||
onPressed: () { | ||
Clipboard.setData(ClipboardData(text: details.selectedText)); | ||
pdfViewerController.clearSelection(); | ||
}, | ||
color: Colors.white, | ||
elevation: 10, | ||
), | ||
), | ||
); | ||
overlayState.insert(overlayEntry); | ||
} | ||
@override | ||
Widget build(BuildContext context) { | ||
return PdfView( | ||
controller: pdfController, | ||
); | ||
} | ||
|
||
// void onTextSelectionChanged(PdfTextSelectionChangedDetails details) { | ||
// if (details.selectedText == null && overlayEntry != null) { | ||
// overlayEntry.remove(); | ||
// overlayEntry = null; | ||
// } else if (details.selectedText != null && overlayEntry == null) { | ||
// final OverlayState overlayState = Overlay.of(context); | ||
// overlayEntry = OverlayEntry( | ||
// builder: (context) => Positioned( | ||
// top: details.globalSelectedRegion.center.dy - 55, | ||
// left: details.globalSelectedRegion.bottomLeft.dx, | ||
// child: RaisedButton( | ||
// child: Text('Copy', style: TextStyle(fontSize: 17)), | ||
// onPressed: () { | ||
// Clipboard.setData(ClipboardData(text: details.selectedText)); | ||
// pdfViewerController.clearSelection(); | ||
// }, | ||
// color: Colors.white, | ||
// elevation: 10, | ||
// ), | ||
// ), | ||
// ); | ||
// overlayState.insert(overlayEntry); | ||
// } | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.