-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
143 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:pdfrx/pdfrx.dart'; | ||
import 'package:tail_app/Frontend/utils.dart'; | ||
import 'package:tail_app/constants.dart'; | ||
|
||
part 'view_pdf.freezed.dart'; | ||
|
||
@freezed | ||
class PDFInfo with _$PDFInfo { | ||
const factory PDFInfo({ | ||
required String url, | ||
required String title, | ||
}) = _PDFInfo; | ||
} | ||
|
||
class ViewPDF extends StatefulWidget { | ||
final PDFInfo pdfInfo; | ||
|
||
const ViewPDF({required this.pdfInfo, super.key}); | ||
|
||
@override | ||
State<ViewPDF> createState() => _ViewPDFState(); | ||
} | ||
|
||
class _ViewPDFState extends State<ViewPDF> { | ||
CancelToken cancelToken = CancelToken(); | ||
double progress = 0; | ||
|
||
Uint8List? data; | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
cancelToken.cancel(); | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
downloadPDF(); | ||
} | ||
|
||
Future<void> downloadPDF() async { | ||
final Response<List<int>> rs = await (await initDio()).get( | ||
widget.pdfInfo.url, | ||
cancelToken: cancelToken, | ||
options: Options( | ||
contentType: 'application/pdf', | ||
responseType: ResponseType.bytes, | ||
), | ||
onReceiveProgress: (current, total) { | ||
setState( | ||
() { | ||
progress = current / total; | ||
}, | ||
); | ||
}, | ||
); | ||
if (rs.statusCode! < 400) { | ||
if (context.mounted) { | ||
progress = 0; | ||
setState(() { | ||
data = Uint8List.fromList(rs.data!); | ||
}); | ||
} | ||
} else { | ||
setState( | ||
() { | ||
progress = 0; | ||
}, | ||
); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text(widget.pdfInfo.title), | ||
), | ||
body: AnimatedSwitcher( | ||
duration: animationTransitionDuration, | ||
child: Builder( | ||
key: ValueKey(data != null), | ||
builder: (context) { | ||
if (data != null) { | ||
return PdfViewer.data( | ||
data!, | ||
sourceName: widget.pdfInfo.title, | ||
); | ||
} else { | ||
return Center( | ||
child: CircularProgressIndicator( | ||
value: progress, | ||
), | ||
); | ||
} | ||
}), | ||
)); | ||
} | ||
} |
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