Files
fcs/lib/pages/widgets/pdf_screen.dart

120 lines
3.5 KiB
Dart
Raw Normal View History

2020-06-24 16:06:15 +06:30
import 'dart:async';
2020-10-16 21:38:39 +06:30
import 'dart:io';
2020-06-24 16:06:15 +06:30
2020-10-26 04:41:24 +06:30
import 'package:fcs/helpers/cache_mgr.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/helpers/theme.dart';
2020-10-26 04:41:24 +06:30
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/local_app_bar.dart';
2020-10-26 04:41:24 +06:30
import 'package:fcs/pages/widgets/progress.dart';
2020-06-24 16:06:15 +06:30
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
2024-12-27 22:36:48 +06:30
import 'package:share_plus/share_plus.dart';
2020-06-24 16:06:15 +06:30
2020-10-28 05:11:06 +06:30
class PDFScreen extends StatefulWidget {
2021-09-10 14:25:37 +06:30
final String? title;
final String? url;
2020-06-24 16:06:15 +06:30
2021-09-10 14:25:37 +06:30
PDFScreen({Key? key, this.url, this.title}) : super(key: key);
2020-06-24 16:06:15 +06:30
2020-10-28 05:11:06 +06:30
_PDFScreenState createState() => _PDFScreenState();
2020-06-24 16:06:15 +06:30
}
2020-10-28 05:11:06 +06:30
class _PDFScreenState extends State<PDFScreen> with WidgetsBindingObserver {
2020-06-24 16:06:15 +06:30
final Completer<PDFViewController> _controller =
Completer<PDFViewController>();
int pages = 0;
int currentPage = 0;
bool isReady = false;
String errorMessage = '';
2020-10-26 04:41:24 +06:30
bool _isLoading = true;
void initState() {
super.initState();
download();
}
2021-09-10 14:25:37 +06:30
File? file;
2020-10-26 04:41:24 +06:30
Future<void> download() async {
try {
2021-09-10 14:25:37 +06:30
File f = await PdfCacheMgr.pdfs.getSingleFile(widget.url!);
2020-10-26 04:41:24 +06:30
setState(() {
file = f;
});
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
2020-06-24 16:06:15 +06:30
@override
Widget build(BuildContext context) {
2020-10-26 04:41:24 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
appBar: LocalAppBar(
2020-10-26 04:41:24 +06:30
backgroundColor: Colors.white,
arrowColor: primaryColor,
titleWidget: Text(widget.title ?? "",
style: TextStyle(color: primaryColor, fontSize: 20)),
actions: [
2020-10-26 04:41:24 +06:30
IconButton(
icon: Icon(
Icons.share,
color: primaryColor,
),
onPressed: _share,
),
],
),
body: Stack(
children: <Widget>[
_isLoading
? Container()
: PDFView(
filePath: file?.path ?? "",
enableSwipe: true,
swipeHorizontal: true,
autoSpacing: false,
pageFling: true,
pageSnap: true,
defaultPage: currentPage,
fitPolicy: FitPolicy.BOTH,
preventLinkNavigation:
false, // if set to true the link is handled in flutter
onRender: (int? _pages) {
2020-10-26 04:41:24 +06:30
print(('pages => $pages'));
setState(() {
2021-09-10 14:25:37 +06:30
pages = _pages!;
2020-10-26 04:41:24 +06:30
isReady = true;
});
},
onViewCreated: (PDFViewController pdfViewController) {
_controller.complete(pdfViewController);
},
2021-09-10 14:25:37 +06:30
onLinkHandler: (String? uri) {
2020-10-26 04:41:24 +06:30
print('goto uri: $uri');
},
2021-09-10 14:25:37 +06:30
onPageChanged: (int? page, int? total) {
2020-10-26 04:41:24 +06:30
print('page change: $page/$total');
setState(() {
2021-09-10 14:25:37 +06:30
currentPage = page!;
2020-10-26 04:41:24 +06:30
});
},
),
],
),
2020-06-24 16:06:15 +06:30
),
);
}
2020-10-16 21:38:39 +06:30
2020-10-26 04:41:24 +06:30
_share() async {
2021-09-10 14:25:37 +06:30
final RenderBox box = context.findRenderObject() as RenderBox;
2024-12-27 22:36:48 +06:30
await Share.shareXFiles([XFile(file!.path)],
2020-10-28 05:11:06 +06:30
subject: "File",
2020-10-16 21:38:39 +06:30
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
}
2020-06-24 16:06:15 +06:30
}