Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
pdf_viewer_screen.dart
Go to the documentation of this file.
1import 'dart:io';
2import 'package:flutter/material.dart';
3import 'package:flutter_pdfview/flutter_pdfview.dart';
4import 'package:flutter_screenutil/flutter_screenutil.dart';
5import 'package:path_provider/path_provider.dart';
6import 'package:flutter/services.dart';
7
8import '../../../../core/ui/widgets/custom_scaffold.dart';
9
10class PdfViewerScreen extends StatefulWidget {
12 final int courseId;
13 final void Function(double progress) onComplete;
14
16 super.key,
17 required this.pdfPath,
18 required this.courseId,
19 required this.onComplete,
20 });
21
22 @override
23 State<PdfViewerScreen> createState() => _PdfViewerScreenState();
24}
25
26class _PdfViewerScreenState extends State<PdfViewerScreen> {
27 int _totalPages = 0;
28 int _currentPage = 0;
29 bool _isReady = false;
30 bool _hasMarkedComplete = false;
32 bool _isLoading = true;
33
34 @override
35 void initState() {
36 super.initState();
38 }
39
40 Future<void> _loadPdfFromAssets() async {
41 try {
42 final ByteData data = await rootBundle.load(widget.pdfPath);
43 final List<int> bytes = data.buffer.asUint8List();
44
45 // Get temporary directory
46 final dir = await getTemporaryDirectory();
47
48 // Create a file in the temporary directory
49 final file = File('${dir.path}/${widget.pdfPath.split('/').last}');
50
51 // Write the PDF data to the file
52 await file.writeAsBytes(bytes);
53
54 setState(() {
55 _localPdfPath = file.path;
56 _isLoading = false;
57 });
58 } catch (e) {
59 print('Error loading PDF: $e');
60 setState(() {
61 _isLoading = false;
62 });
63 }
64 }
65
66 @override
67 Widget build(BuildContext context) {
68 return CustomScaffold(
69 title: '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),
81 children: [
82 Row(
83 mainAxisAlignment: MainAxisAlignment.spaceBetween,
84 children: [
85 Text(
86 '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 ),
106 ? null
107 : () {
108 setState(() {
109 _hasMarkedComplete = true;
110 });
111 widget.onComplete(0.5);
112 Navigator.pop(context);
113 },
114 child: Text(
116 ? 'Marked as Complete'
117 : "I've Read the PDF",
118 style: Theme.of(context)
119 .textTheme
120 .bodySmall
121 ?.copyWith(
122 color: Theme.of(context)
123 .colorScheme
124 .onPrimary,
125 ),
126 ),
127 ),
128 ],
129 ),
130 ],
131 ),
132 ),
133
134 // PDF Viewer
135 Expanded(
137 ),
138
139 // Mark as complete button
140 ],
141 ),
142 ),
143 );
144 }
145
146 Widget _buildPdfView() {
147 if (_localPdfPath == null) {
148 return Center(
149 child: Text('Failed to load PDF'),
150 );
151 }
152
153 return PDFView(
154 filePath: _localPdfPath!,
155 enableSwipe: true,
156 swipeHorizontal: true,
157 autoSpacing: false,
158 pageFling: false,
159 pageSnap: true,
160 defaultPage: 0,
161 fitPolicy: FitPolicy.BOTH,
162 preventLinkNavigation: false,
163 onRender: (pages) {
164 setState(() {
165 _totalPages = pages!;
166 _isReady = true;
167 });
168 },
169 onError: (error) {
170 print('Error loading PDF: $error');
171 },
172 onPageError: (page, error) {
173 print('Error loading page $page: $error');
174 },
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}
override void initState()
class App extends StatefulWidget build(BuildContext context)
Definition app.dart:31
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
class Partner String
int _currentPage
bool _isLoading
Widget _buildPdfView()
String _localPdfPath
bool _hasMarkedComplete
bool _isReady
Future< void > _loadPdfFromAssets() async
class PdfViewerScreen extends StatefulWidget _totalPages
final Color color
Definition failures.dart:1
final VoidCallback onPressed
final String title
final double progress
style Text( '${ 'scheduling.reference'.tr()}:${collection.internalCode}', style:Theme.of(context).textTheme.bodySmall,)
style Column(crossAxisAlignment:CrossAxisAlignment.end, children:[Container(padding:EdgeInsets.symmetric(horizontal:8.w, vertical:4.h), decoration:BoxDecoration(color:ColorPalette.tiffanyBlue.withValues(alpha:0.1), borderRadius:BorderRadius.circular(12),), child:Text(collection.type ?? '', style:Theme.of(context).textTheme.bodySmall?.copyWith(color:ColorPalette.tiffanyBlue, fontWeight:FontWeight.bold,),),),],)