fluttercommunity / flutter_downloader

Flutter Downloader - A plugin for creating and managing download tasks.
https://pub.dev/packages/flutter_downloader
BSD 3-Clause "New" or "Revised" License
913 stars 513 forks source link

ios download no response #389

Open callmejm opened 3 years ago

callmejm commented 3 years ago

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

edex96 commented 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

awaisahmad94 commented 3 years ago

@edex96 Exactly what i am looking for. Can guide or share how u did it with the snackbar?

edex96 commented 3 years ago

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}',
                  );
                },
              ),
      ),
    );
  }
}
awaisahmad94 commented 3 years ago

@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 commented 3 years ago

@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

awaisahmad94 commented 3 years ago

@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?

edex96 commented 3 years ago

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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}