filehandler 1.0.1+1 filehandler: ^1.0.1+1 copied to clipboard
A package created to assist you on creating and reading of files.
import 'package:filehandler/filehandler.dart';
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';
/*Change the value of this const to your own custom value.
Use an empty string if you wish to drop your files on root*/
const FOLDER_NAME = "FileHandler";
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: 'FileHandlerApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text("File Handler. Example"),
),
body: Home(),
),
);
}
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() => _HomeState();
}
class _HomeState extends State<Home> {
var fileHandler = FileHandler(folderName: FOLDER_NAME);
@override
Widget build(BuildContext context) {
var filenameController = TextEditingController();
var contentController = TextEditingController();
return Container(
padding: EdgeInsets.all(16),
child: ListView(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () async {
var result = await fileHandler.openFile(context);
if (result != null) {
Toast.show("File successfully opened", context,
duration: Toast.LENGTH_LONG,
gravity: Toast.BOTTOM,
backgroundColor: Colors.green);
filenameController.text = result.filename;
contentController.text = result.content;
} else {
Toast.show("Error when opening file", context,
duration: Toast.LENGTH_LONG,
gravity: Toast.BOTTOM,
backgroundColor: Colors.red);
}
},
child: Text("Open File"),
),
ElevatedButton(
onPressed: () async {
var result = await fileHandler.saveFile(
filenameController.text, contentController.text);
if (result) {
Toast.show("File successfully saved", context,
duration: Toast.LENGTH_LONG,
gravity: Toast.BOTTOM,
backgroundColor: Colors.green);
} else {
Toast.show("Error when saving file", context,
duration: Toast.LENGTH_LONG,
gravity: Toast.BOTTOM,
backgroundColor: Colors.red);
}
},
child: Text("Save File"),
)
],
),
Column(
children: [
TextFormField(
controller: filenameController,
decoration: InputDecoration(
labelText: "File name:",
labelStyle: TextStyle(
color: Colors.blue,
)),
),
TextFormField(
controller: contentController,
decoration: InputDecoration(
labelText: "File content:",
labelStyle: TextStyle(
color: Colors.blue,
)),
)
],
)
],
));
}
}