How to open and PDF or word document in the [Flutter]

前端 未结 6 1207
别那么骄傲
别那么骄傲 2020-12-16 13:01

Question is simple, I would like to open any pdf or doc file via default App with using Flutter.

Think a Raised button that related my pdf asset, when user press it

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 13:20

    class EmployeeViewModel {
      EmployeeModel _employeeModel = EmployeeModel();
    
       String fname;   
                                                                                                                                  void downloadFile(BuildContext context, String fileUrl, String fileName, ProgressListener listener) async {
    
        String _filePath = '';
    
        if (Platform.isAndroid) {
          String _directory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
          print(_directory);
          _filePath = '$_directory/$fileName';
          //todo getting file name here
          print("file name" + fileName);
          fname = fileName;
          print("file fname" + fname);
          //APIKey.FILE_NAME: fileName;
        } else if (Platform.isIOS) {
          Directory _directory = await getApplicationDocumentsDirectory();
          _filePath = '${_directory.path}/$fileName';
    
          print("file name" + fileName);
          //log(fileName);
          debugPrint(_directory.path);
          print("directory path" + _directory.path);
        }
    
        var response = await Dio().downloadUri(Uri().resolve(fileUrl), _filePath);
    
        if (response.statusCode == 200) {
          listener.isProcessing(false);
          AlertMessageDialog(context,  UtilString.downloadCompleted, UtilString.downloadCompletedMessage, UtilString.open, AlertMessageDialogActionHandler());
        } else {
          listener.isProcessing(false);
          UtilAction.showSnackBar(context, response.statusMessage);
        }
      }                                                                                                  class AlertMessageDialogActionHandler implements AlertMessageDialogListener {
    
      @override
      Future onPositiveButtonClick() async {
        String _filePath = '';
        String fileName;
        String _directory = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
        //todo geeting right directory path here
        print("directory" + _directory);
        _filePath = '$_directory/$fileName';
        print("file path" + _filePath);
       // print("filename" + fileName);
    
        OpenFile.open("/storage/emulated/0/Download/GA55-Estimated-SHRIGOPAL-VERMA-2020-2021.pdf");  }}
    

提交回复
热议问题