Open callmejm opened 3 years ago
in my case the file is downloaded however there is no notification shown on ios
edit: upon reading the doc carefully I see that the ios doesn't have the mechanism for it so I've just put a snackbar with a button to preview the pdf it turn out much nicer
@edex96 Exactly what i am looking for. Can guide or share how u did it with the snackbar?
Sure, there you go mate
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:path_provider/path_provider.dart';
class MyWeb extends StatelessWidget {
MyWeb({this.url});
final String url;
InAppWebViewController webViewController;
@override
Widget build(BuildContext context) {
return InAppWebView(
initialUrl: url,
onWebViewCreated: (InAppWebViewController controller) {
webViewController = controller;
},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(useOnDownloadStart: true),
),
onDownloadStart:
(InAppWebViewController controller, String fileUrl) async {
String savedDir = Platform.isAndroid
? (await getExternalStorageDirectory()).path
: (await getApplicationDocumentsDirectory()).path;
FlutterDownloader.enqueue(
url: url,
savedDir: savedDir,
showNotification: true,
openFileFromNotification: true,
).then((_) {
showSnack(context, fileUrl);
});
},
);
}
void showSnack(context, String fileUrl) {
bool isNotPdf = fileUrl.substring(fileUrl.length - 4) != '.pdf';
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Your file is downloaded!'),
action: isNotPdf
? null
: SnackBarAction(
label: 'Open',
onPressed: () {
webViewController.loadUrl(
url:
'https://docs.google.com/gview?embedded=true&url=${fileUrl}',
);
},
),
),
);
}
}
@edex96 Thanks a ton mate!!! Works like a charm on Android. If possible can i ask is this working for iOS devices as well?
@edex96 Thanks a ton mate!!! Works like a charm on Android. If possible can i ask is this working for iOS devices as well?
of course I even published it and 3.5K users enjoying right now
@edex96 That's great. Just wanted to ask for images type (.png,.jpg,.jpeg) its not opening with this docs.google. Any solution for that?
Then you may want to do something like this
void showSnack(context, String fileUrl) {
VoidCallback onPressed;
final String ext = fileUrl.substring(fileUrl.length - 4).toLowerCase();
final bool isPdf = ext != '.pdf';
final bool isImage = ext != '.jpeg' || ext != '.jpg' || ext != '.png';
if (isPdf) {
onPressed = () {
webViewController.loadUrl(
url: 'https://docs.google.com/gview?embedded=true&url=${fileUrl}',
);
};
} else if (isImage) {
onPressed = () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ImageDetailScreen(imageUrl: fileUrl),
),
);
};
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Your file is downloaded!'),
action: isPdf || isImage
? SnackBarAction(
label: 'Open',
onPressed: onPressed,
)
: null,
),
);
}
class ImageDetailScreen extends StatelessWidget {
final String imageUrl;
ImageDetailScreen({Key key, @required this.imageUrl})
: assert(imageUrl != null),
super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.transparent,
brightness: Brightness.dark,
),
body: Container(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0, vertical: 100),
child: ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: CachedNetworkImage(
imageUrl: imageUrl,
imageBuilder: (context, imageProvider) => PhotoView(
imageProvider: imageProvider,
minScale: PhotoViewComputedScale.contained * 1,
maxScale: PhotoViewComputedScale.covered * 1,
),
),
),
),
),
);
}
}
I had followed all the ios set up instruction, and use https://www.qrcode-monkey.com/ as testing, permission all ok, when download , task id have return value , but I cant find any response / feedback and image at folder. Even no error also