Aidra Connect 10.0.2+16
Aidra Connect Mobile Application
Loading...
Searching...
No Matches
pdf_viewer_screen.dart
Go to the documentation of this file.
1import 'dart:io';
2import 'package:connect/core/localization/app_localizations.dart';
3import 'package:flutter/material.dart';
4import 'package:flutter_pdfview/flutter_pdfview.dart';
5import 'package:flutter_screenutil/flutter_screenutil.dart';
6import 'package:path_provider/path_provider.dart';
7import 'package:flutter/services.dart';
8
9import '../../../../core/ui/widgets/custom_scaffold.dart';
10
11class PdfViewerScreen extends StatefulWidget {
12 final String pdfPath;
13 final int courseId;
14 final void Function(double progress) onComplete;
15
17 super.key,
18 required this.pdfPath,
19 required this.courseId,
20 required this.onComplete,
21 });
22
23 @override
24 State<PdfViewerScreen> createState() => _PdfViewerScreenState();
25}
26
27class _PdfViewerScreenState extends State<PdfViewerScreen> {
28 int _totalPages = 0;
29 int _currentPage = 0;
30 bool _isReady = false;
31 bool _hasMarkedComplete = false;
33 bool _isLoading = true;
34
35 @override
36 void initState() {
37 super.initState();
39 }
40
41 Future<void> _loadPdfFromAssets() async {
42 try {
43 final ByteData data = await rootBundle.load(widget.pdfPath);
44 final List<int> bytes = data.buffer.asUint8List();
45
46 // Get temporary directory
47 final dir = await getTemporaryDirectory();
48
49 // Create a file in the temporary directory
50 final file = File('${dir.path}/${widget.pdfPath.split('/').last}');
51
52 // Write the PDF data to the file
53 await file.writeAsBytes(bytes);
54
55 setState(() {
56 _localPdfPath = file.path;
57 _isLoading = false;
58 });
59 } catch (e) {
60 setState(() {
61 _isLoading = false;
62 });
63 }
64 }
65
66 @override
67 Widget build(BuildContext context) {
68 return CustomScaffold(
69 title: AppLocalizations.of(context).translate('Learning Material'),
70 isLeadingVisible: true,
71 body: SafeArea(
73 ? Center(child: CircularProgressIndicator())
74 : Column(
75 children: [
76 // PDF progress indicator
77 if (_isReady && _totalPages > 0)
78 Padding(
79 padding: EdgeInsets.all(12.r),
80 child: Column(
81 children: [
82 Row(
83 mainAxisAlignment: MainAxisAlignment.spaceBetween,
84 children: [
85 Text(
86 '${AppLocalizations.of(context).translate('Page')} $_currentPage of $_totalPages',
87 style: Theme.of(context)
88 .textTheme
89 .bodySmall
90 ?.copyWith(
91 color:
92 Theme.of(context).colorScheme.primary,
93 ),
94 ),
95 ElevatedButton(
96 style: ElevatedButton.styleFrom(
97 padding: EdgeInsets.symmetric(
98 horizontal: 15,
99 vertical: 13,
100 ),
101 shape: RoundedRectangleBorder(
102 borderRadius: BorderRadius.circular(200),
103 ),
104 ),
105 onPressed: _hasMarkedComplete
106 ? null
107 : () {
108 setState(() {
109 _hasMarkedComplete = true;
110 });
111 widget.onComplete(0.5);
112 Navigator.pop(context);
113 },
114 child: Text(
116 ? AppLocalizations.of(context)
117 .translate('Marked as Complete')
118 : AppLocalizations.of(context)
119 .translate('I\'ve Read the PDF'),
120 style: Theme.of(context)
121 .textTheme
122 .bodySmall
123 ?.copyWith(
124 color: Theme.of(context)
125 .colorScheme
126 .onPrimary,
127 ),
128 ),
129 ),
130 ],
131 ),
132 ],
133 ),
134 ),
135
136 // PDF Viewer
137 Expanded(
139 ),
140
141 // Mark as complete button
142 ],
143 ),
144 ),
145 );
146 }
147
148 Widget _buildPdfView() {
149 if (_localPdfPath == null) {
150 return Center(
151 child: Text(
152 AppLocalizations.of(context).translate('Failed to load PDF'),
153 ),
154 );
155 }
156
157 return PDFView(
158 filePath: _localPdfPath!,
159 enableSwipe: true,
160 swipeHorizontal: true,
161 autoSpacing: false,
162 pageFling: false,
163 pageSnap: true,
164 defaultPage: 0,
165 fitPolicy: FitPolicy.BOTH,
166 preventLinkNavigation: false,
167 onRender: (pages) {
168 setState(() {
169 _totalPages = pages!;
170 _isReady = true;
171 });
172 },
173 onError: (error) {},
174 onPageError: (page, error) {},
175 onViewCreated: (PDFViewController pdfViewController) {
176 // PDF view created
177 },
178 onPageChanged: (int? page, int? total) {
179 if (page != null) {
180 setState(() {
181 _currentPage = page + 1;
182 });
183 }
184 },
185 );
186 }
187}
AppLocalizations(this.locale)
String translate(String key)
static AppLocalizations of(BuildContext context)
override State< PdfViewerScreen > createState()
const PdfViewerScreen({ super.key, required this.pdfPath, required this.courseId, required this.onComplete, })
final void Function(double progress) onComplete
final Widget child
final EdgeInsets padding
override void initState()
final Color color
Definition failures.dart:1
override Widget build(BuildContext context)
late int _currentPage
late int _totalPages
bool _isLoading
Widget _buildPdfView()
String _localPdfPath
bool _hasMarkedComplete
bool _isReady
Future< void > _loadPdfFromAssets() async
final double total
final String title
final double progress