file_picker_cross 4.0.0 file_picker_cross: ^4.0.0 copied to clipboard
Select, open, choose or pick documents, images videos or other files on Android, iOS, the desktop and the web for reading, writing, use as String, byte list or HTTP uploads.
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FilePickerCross filePickerCross;
String _fileString = '';
Set<String> lastFiles;
@override
void initState() {
FilePickerCross.listInternalFiles()
.then((value) => setState(() => lastFiles = value.toSet()));
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.blueGrey, accentColor: Colors.lightGreen),
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: ListView(
padding: EdgeInsets.all(8),
children: <Widget>[
Text(
'Last files',
style: Theme.of(context).textTheme.headline5,
),
(lastFiles == null)
? Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
shrinkWrap: true,
primary: false,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => ListTile(
leading: Text('$index.'),
title: Text(lastFiles.toList()[index]),
onTap: () async => setFilePicker(
await FilePickerCross.fromInternalPath(
path: lastFiles.toList()[index])),
),
itemCount: lastFiles.length,
),
RaisedButton(
onPressed: _selectFile,
child: Text('Open File...'),
),
(filePickerCross == null)
? Text('Open a file first, to save')
: RaisedButton(
onPressed: _selectSaveFile,
child: Text('Save as...'),
),
Text(
'File path: ${filePickerCross?.path ?? 'unknown'} (Might cause issues on web)\n'),
Text('File length: ${filePickerCross?.length ?? 0}\n'),
Text('File as String: $_fileString\n'),
],
),
),
);
}
void _selectFile() {
FilePickerCross.importFromStorage()
.then((filePicker) => setFilePicker(filePicker));
}
void _selectSaveFile() {
filePickerCross.exportToStorage();
}
setFilePicker(FilePickerCross filePicker) => setState(() {
filePickerCross = filePicker;
filePickerCross.saveToPath(path: filePickerCross.fileName);
lastFiles.add(filePickerCross.fileName);
try {
_fileString = filePickerCross.toString();
} catch (e) {
_fileString = 'Not a text file. Showing base64.\n\n' +
filePickerCross.toBase64();
}
});
}