Line data Source code
1 : import 'dart:io'; 2 : import 'package:cwtch/models/appstate.dart'; 3 : import 'package:file_picker/file_picker.dart'; 4 : import 'package:flutter/widgets.dart'; 5 : import 'package:provider/provider.dart'; 6 : 7 0 : void showFilePicker(BuildContext ctx, int maxBytes, Function(File) onSuccess, Function onError, Function onCancel) async { 8 : // only allow one file picker at a time 9 : // note: ideally we would destroy file picker when leaving a conversation 10 : // but we don't currently have that option. 11 : // we need to store AppState in a variable because ctx might be destroyed 12 : // while awaiting for pickFiles. 13 0 : var appstate = Provider.of<AppState>(ctx, listen: false); 14 0 : appstate.disableFilePicker = true; 15 : // currently lockParentWindow only works on Windows... 16 0 : FilePickerResult? result = await FilePicker.platform.pickFiles(lockParentWindow: true); 17 0 : appstate.disableFilePicker = false; 18 0 : if (result != null && result.files.first.path != null) { 19 0 : File file = File(result.files.first.path!); 20 : // We have a maximum number of bytes we can represent in terms of 21 : // a manifest (see : https://git.openprivacy.ca/cwtch.im/cwtch/src/branch/master/protocol/files/manifest.go#L25) 22 0 : if (file.lengthSync() <= maxBytes) { 23 0 : onSuccess(file); 24 : } else { 25 0 : onError(); 26 : } 27 : } else { 28 0 : onCancel(); 29 : } 30 : } 31 : 32 0 : Future<String?> showCreateFilePicker(BuildContext ctx) async { 33 : // only allow one file picker at a time 34 : // note: ideally we would destroy file picker when leaving a conversation 35 : // but we don't currently have that option. 36 : // we need to store AppState in a variable because ctx might be destroyed 37 : // while awaiting for pickFiles. 38 0 : var appstate = Provider.of<AppState>(ctx, listen: false); 39 0 : appstate.disableFilePicker = true; 40 : // currently lockParentWindow only works on Windows... 41 0 : String? result = await FilePicker.platform.saveFile(lockParentWindow: true); 42 0 : appstate.disableFilePicker = false; 43 : return result; 44 : } 45 : 46 0 : Future<String?> showSelectDirectoryPicker(BuildContext ctx) async { 47 : // only allow one file picker at a time 48 : // note: ideally we would destroy file picker when leaving a conversation 49 : // but we don't currently have that option. 50 : // we need to store AppState in a variable because ctx might be destroyed 51 : // while awaiting for pickFiles. 52 0 : var appstate = Provider.of<AppState>(ctx, listen: false); 53 0 : appstate.disableFilePicker = true; 54 : // currently lockParentWindow only works on Windows... 55 0 : String? result = await FilePicker.platform.getDirectoryPath(lockParentWindow: true); 56 0 : appstate.disableFilePicker = false; 57 : return result; 58 : }