i want to open pdf from previously opened page is there a way to do that without using custom widget ?
Opening pdf from previously opened page
Widgets & Design
i have made a custom widget using pdfx package but i am getting error null check used on null value and the test project won't open either ๐
here's my code // Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/actions/actions.dart' as action_blocks;
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/custom_code/actions/index.dart'; // Imports custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:flutter/foundation.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'package:pdfx/pdfx.dart';
class FFPDFViewerWithResume extends StatefulWidget {
final String pdfUrl;
final double? width;
final double? height;
const FFPDFViewerWithResume({
Key? key,
required this.pdfUrl,
this.width,
this.height,
}) : super(key: key);
@override
_FFPDFViewerWithResumeState createState() => _FFPDFViewerWithResumeState();
}
class _FFPDFViewerWithResumeState extends State<FFPDFViewerWithResume> {
PdfController? _pdfController;
int _currentPage = 0;
bool _isLoading = true;
String? _errorMessage;
@override
void initState() {
super.initState();
_loadDocument();
}
Future<void> _loadDocument() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
_currentPage = prefs.getInt(widget.pdfUrl) ?? 0;
if (kIsWeb) {
_pdfController = PdfController(
document: PdfDocument.openData(await _loadPdfData()),
);
} else {
final file = await DefaultCacheManager().getSingleFile(widget.pdfUrl);
_pdfController = PdfController(
document: PdfDocument.openFile(file.path),
);
}
if (mounted) {
setState(() {
_isLoading = false;
});
_pdfController?.jumpToPage(_currentPage); // Add null check here
}
} catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
_errorMessage = "Error loading PDF: $e";
});
print("Full error details: $e");
}
}
}
Future<Uint8List> _loadPdfData() async {
final response = await http.get(Uri.parse(widget.pdfUrl));
if (response.statusCode == 200) {
return response.bodyBytes;
} else {
throw Exception('Failed to load PDF: ${response.statusCode}');
}
}
void _saveCurrentPage(int page) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt(widget.pdfUrl, page);
}
@override
Widget build(BuildContext context) {
return SizedBox(
width: widget.width,
height: widget.height,
child: _isLoading
? const Center(child: CircularProgressIndicator())
: (_errorMessage != null)
? Center(child: Text(_errorMessage!))
: PdfView(
controller: _pdfController!,
onPageChanged: (page) {
_currentPage = page;
_saveCurrentPage(page);
},
builders: PdfViewBuilders<DefaultBuilderOptions>(
options: const DefaultBuilderOptions(),
),
), // Show PdfView only if _pdfController is not null
);
}
@override
void dispose() {
_pdfController?.dispose();
super.dispose();
}
}
Yes
1
4 replies