A customable file saver and file picker package that makes it easy for user to browse folder and save file or pick files in android.
Add this to your pubspec.yaml
.
dependencies:
filesaverz: ^3.0.0
Continue by adding permission in your AndroidManifest.xml
.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Just in case if you got MissingPluginException(No implementation found for method checkPermissionStatus on channel flutter.baseflow.com/permissions/methods)
error, try fix it by adding this to your app/build.gradle
.
buildTypes {
release {
shrinkResources false
minifyEnabled false
signingConfig signingConfigs.release
}
}
And then import the filesaver (with z) package.
import 'package:filesaverz/filesaverz.dart';
First, setting up the FileSaver widget like this.
FileSaver fileSaver = FileSaver(
fileTypes: const ['txt','pdf'],
initialFileName: 'Untitled File',
);
or this customable FileSaver.
FileSaver fileSaver = FileSaver.builder(
fileTypes: const ['txt','pdf'],
initialFileName: 'Untitled File',
headerBuilder: (context, state) => /* Your Widget */,
bodyBuilder: (context, state) => /* Your Widget */,
footerBuilder: (context, state) => /* Your Widget */,
);
And then in async function call these:
Purpose | Code |
Getting selected path from saving file. |
String? path = await fileSaver.getPath(context); |
Calling writeAsBytes method. |
fileSaver.writeAsBytes(bytes, context: context); |
Calling writeAsBytesSync method. |
fileSaver.writeAsBytesSync(bytes, context: context); |
Calling writeAsString method. |
fileSaver.writeAsString(contents, context: context); |
Calling writeAsStringSync method. |
fileSaver.writeAsStringSync(contents, context: context); |
Picking single file. |
File? file = await fileSaver.pickFile(context); |
Picking multiple files. |
List<File>? files = await fileSaver.pickFiles(context); |
Property | Code |
headerBuilder, an optional builder. |
(context, state) {
return Text('My Custom Header Widget');
},
|
bodyBuilder, an optional builder. |
(context, state) {
return Text('My Custom Body Widget');
},
|
footerBuilder, an optional builder |
(context, state) {
return Text('My Custom Footer Widget');
},
|
style, set custom Color, TextStyle, Icon and displayed Text. |
FileSaverStyle(
primaryColor: Colors.blue,
secondaryColor: Colors.white,
primaryTextStyle: TextStyle(),
secondaryTextStyle: TextStyle(),
icons: [
FileSaverIcon(icon: (path) => Icon(Icons.default)),
FileSaverIcon.directory(icon: (path) => Icon(Icons.folder)),
FileSaverIcon.file(fileType: 'jpg', icon: (path) => Image.file(File(path)),
],
text: FileSaverText(
popupNo: 'Nay',
popupYes: 'Sí',
),
),
|
initialFileName, this property is used when you call saving method. |
'Untitled File',
|
initialDirectory, in Android by default it's calling Environment.getExternalStorageDirectory. |
Directory('Storage Path'),
|
fileTypes, this property is used to limit what kind of fileTypes that we want to display, and in saving method this fileTypes also used as an option for user to set the desired fileTypes to write. |
const ['jpg','gif','png'],
|
Full Documentation here.