128 lines
3.1 KiB
Dart
128 lines
3.1 KiB
Dart
part of '../widgets.dart';
|
|
|
|
// typedef PDFViewerKey = GlobalKey<SfPdfViewerState>;
|
|
|
|
abstract interface class Viewer extends StatelessComponent {
|
|
final String src;
|
|
|
|
const Viewer({
|
|
super.key,
|
|
required this.src,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return buildViewer(context);
|
|
}
|
|
|
|
Widget buildViewer(BuildContext context);
|
|
}
|
|
|
|
class PDFViewer extends StatefulWidget {
|
|
final String url;
|
|
final String title;
|
|
|
|
const PDFViewer({
|
|
super.key,
|
|
required this.url,
|
|
required this.title,
|
|
});
|
|
|
|
@override
|
|
State<PDFViewer> createState() => PDFViewerState();
|
|
}
|
|
|
|
class PDFViewerState extends State<PDFViewer> {
|
|
late PdfController _pdfController;
|
|
|
|
Future<PdfController> _initializePdf() async {
|
|
final file = await downloadPdf(widget.url);
|
|
final Future<PdfDocument> document = PdfDocument.openFile(file.path);
|
|
return PdfController(document: document);
|
|
}
|
|
|
|
Future<File> downloadPdf(String url) async {
|
|
final response = await http.get(Uri.parse(url));
|
|
if (response.statusCode == 200) {
|
|
final bytes = response.bodyBytes;
|
|
final dir = await getTemporaryDirectory();
|
|
final file = File('${dir.path}/downloaded.pdf');
|
|
await file.writeAsBytes(bytes);
|
|
return file;
|
|
} else {
|
|
throw Exception('Falha ao baixar o PDF');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Stack(
|
|
children: [
|
|
_buildPDFViewer(),
|
|
buildShareButton(),
|
|
],
|
|
);
|
|
}
|
|
|
|
Positioned buildShareButton() {
|
|
return Positioned(
|
|
bottom: 10,
|
|
right: 10,
|
|
child: IconButton(
|
|
icon: Icon(
|
|
Icons.share,
|
|
color: Colors.black,
|
|
),
|
|
color: Colors.black,
|
|
onPressed: onShare,
|
|
),
|
|
);
|
|
}
|
|
|
|
void onShare() async {
|
|
final Uri uri = Uri.parse(widget.url);
|
|
final response = await http.get(uri);
|
|
if (response.statusCode == 200) {
|
|
final XFile xfile = XFile.fromData(response.bodyBytes,
|
|
name: '${widget.title}.pdf', mimeType: 'application/pdf');
|
|
await Share.shareXFiles([xfile], text: 'Confira este PDF!');
|
|
} else {
|
|
print('Erro ao baixar o arquivo: ${response.statusCode}');
|
|
}
|
|
}
|
|
|
|
Widget buildLoadingIndicator(BuildContext context) {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 15),
|
|
child: Center(
|
|
child: CircularProgressIndicator(
|
|
valueColor: AlwaysStoppedAnimation<Color>(
|
|
FlutterFlowTheme.of(context).primary,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildPDFViewer() => Padding(
|
|
padding: EdgeInsets.all(10),
|
|
child: FutureBuilder<PdfController>(
|
|
future: _initializePdf(),
|
|
builder: (context, snapshot) {
|
|
if (!snapshot.hasData) return buildLoadingIndicator(context);
|
|
return PdfView(
|
|
controller: snapshot.data!,
|
|
scrollDirection: Axis.vertical,
|
|
);
|
|
}),
|
|
);
|
|
|
|
// Widget get progressIndicator => LoadingUtil.buildLoadingIndicator(context);
|
|
|
|
@override
|
|
void dispose() {
|
|
_pdfController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|