WIP
This commit is contained in:
parent
575cfc5a83
commit
aa1bd55818
|
@ -3,6 +3,7 @@ import 'dart:io';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:hub/features/backend/index.dart';
|
import 'package:hub/features/backend/index.dart';
|
||||||
import 'package:hub/flutter_flow/index.dart';
|
import 'package:hub/flutter_flow/index.dart';
|
||||||
|
import 'package:hub/shared/utils/dialog_util.dart';
|
||||||
import 'package:hub/shared/widgets.dart';
|
import 'package:hub/shared/widgets.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:pdfx/pdfx.dart';
|
import 'package:pdfx/pdfx.dart';
|
||||||
|
@ -44,58 +45,78 @@ class ReadView extends StatefulWidget {
|
||||||
|
|
||||||
class ReadViewState extends State<ReadView> {
|
class ReadViewState extends State<ReadView> {
|
||||||
Future<ReadViewController> _initializePdf() async {
|
Future<ReadViewController> _initializePdf() async {
|
||||||
final file = await downloadPdf(widget.url);
|
try {
|
||||||
final Future<DocumentType> document = DocumentType.openFile(file.path);
|
final file = await downloadPdf(widget.url);
|
||||||
return ReadViewController(document: document);
|
final Future<DocumentType> document = DocumentType.openFile(file.path);
|
||||||
|
return ReadViewController(document: document);
|
||||||
|
} catch (e) {
|
||||||
|
logError('Erro ao baixar o PDF', e);
|
||||||
|
return Future.error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<File> downloadPdf(String url) async {
|
Future<File> downloadPdf(String url) async {
|
||||||
final response = await http.get(Uri.parse(url));
|
try {
|
||||||
if (response.statusCode == 200) {
|
final response = await http.get(Uri.parse(url));
|
||||||
final bytes = response.bodyBytes;
|
if (response.statusCode == 200) {
|
||||||
final dir = await getTemporaryDirectory();
|
final bytes = response.bodyBytes;
|
||||||
final file = File('${dir.path}/downloaded.pdf');
|
final dir = await getTemporaryDirectory();
|
||||||
await file.writeAsBytes(bytes);
|
final file = File('${dir.path}/downloaded.pdf');
|
||||||
return file;
|
await file.writeAsBytes(bytes);
|
||||||
} else {
|
return file;
|
||||||
throw Exception('Falha ao baixar o PDF');
|
} else {
|
||||||
|
throw Exception('Falha ao baixar o PDF');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logError('Erro ao baixar o PDF', e);
|
||||||
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void logError(String message, dynamic error) {
|
||||||
|
log('$message: $error');
|
||||||
|
DialogUtil.error(context, message);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Stack(
|
return Stack(
|
||||||
children: [
|
children: [
|
||||||
_buildPDFViewer(),
|
_buildPDFViewer(context),
|
||||||
buildShareButton(),
|
buildShareButton(context),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Positioned buildShareButton() {
|
Positioned buildShareButton(BuildContext context) {
|
||||||
|
final theme = FlutterFlowTheme.of(context);
|
||||||
return Positioned(
|
return Positioned(
|
||||||
bottom: 10,
|
bottom: 10,
|
||||||
right: 10,
|
right: 10,
|
||||||
child: IconButton(
|
child: IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
Icons.share,
|
Icons.share,
|
||||||
color: Colors.black,
|
color: theme.primaryText,
|
||||||
),
|
),
|
||||||
color: Colors.black,
|
color: theme.primaryText,
|
||||||
onPressed: onShare,
|
onPressed: onShare,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onShare() async {
|
void onShare() async {
|
||||||
final Uri uri = Uri.parse(widget.url);
|
try {
|
||||||
final response = await http.get(uri);
|
final Uri uri = Uri.parse(widget.url);
|
||||||
if (response.statusCode == 200) {
|
final response = await http.get(uri);
|
||||||
final XFile xfile = XFile.fromData(response.bodyBytes,
|
if (response.statusCode == 200) {
|
||||||
name: '${widget.title}.pdf', mimeType: 'application/pdf');
|
final XFile xfile = XFile.fromData(response.bodyBytes,
|
||||||
await Share.shareXFiles([xfile], text: 'Confira este PDF!');
|
name: '${widget.title}.pdf', mimeType: 'application/pdf');
|
||||||
} else {
|
await Share.shareXFiles([xfile], text: 'Confira este PDF!');
|
||||||
log('Erro ao baixar o arquivo: ${response.statusCode}');
|
} else {
|
||||||
|
throw Exception('Erro ao compartilhar o arquivo: ${response.statusCode}');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logError('Erro ao compartilhar o arquivo', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,12 +133,19 @@ class ReadViewState extends State<ReadView> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildPDFViewer() => Padding(
|
Widget _buildPDFViewer(BuildContext context) => Padding(
|
||||||
padding: EdgeInsets.all(10),
|
padding: EdgeInsets.all(10),
|
||||||
child: FutureBuilder<ReadViewController>(
|
child: FutureBuilder<ReadViewController>(
|
||||||
future: _initializePdf(),
|
future: _initializePdf(),
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (!snapshot.hasData) return buildLoadingIndicator(context);
|
if (!snapshot.hasData) return buildLoadingIndicator(context);
|
||||||
|
if (snapshot.error != null) {
|
||||||
|
return buildLoadingIndicator(context);
|
||||||
|
}
|
||||||
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
|
return buildLoadingIndicator(context);
|
||||||
|
}
|
||||||
|
|
||||||
return PdfView(
|
return PdfView(
|
||||||
controller: snapshot.data!,
|
controller: snapshot.data!,
|
||||||
renderer: (PdfPage page) => page.render(
|
renderer: (PdfPage page) => page.render(
|
||||||
|
@ -134,6 +162,4 @@ class ReadViewState extends State<ReadView> {
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Widget get progressIndicator => LoadingUtil.buildLoadingIndicator(context);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue