Created
March 16, 2025 13:32
-
-
Save rubywai/4c905d7fb6266b113752f06af313f73a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dialogs | |
//date time(datetime picker) | |
//file storage | |
//Drectory | |
import 'dart:io'; | |
import 'package:flutter/material.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
void main() async { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({super.key}); | |
@override | |
State<Home> createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
//folder //file | |
final TextEditingController _dataController = TextEditingController(); | |
final TextEditingController _fileNameController = TextEditingController(); | |
@override | |
void initState() { | |
super.initState(); | |
_loadDirectory("path_provider.txt"); | |
} | |
void _loadDirectory(String fileName) async { | |
Directory directory = await _getDirectory(); | |
String filePath = "${directory.path}/$fileName"; | |
_readData(filePath); | |
} | |
void _readData(String filePath) async { | |
File file = File(filePath); | |
String data = await file.readAsString(); | |
setState(() { | |
_dataController.text = data; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Column( | |
children: [ | |
TextField( | |
controller: _dataController, | |
decoration: InputDecoration(hintText: 'File data'), | |
minLines: 3, | |
maxLines: 10, | |
), | |
TextField( | |
controller: _fileNameController, | |
decoration: InputDecoration(hintText: 'File Name'), | |
), | |
ElevatedButton( | |
onPressed: () async { | |
// ////filename.png | |
Directory appDirectory = await _getDirectory(); | |
String filePath = | |
"${appDirectory.path}/${_fileNameController.text}"; | |
File file = File(filePath); | |
await file.writeAsString(_dataController.text); | |
setState(() {}); | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar( | |
content: Text('Success'), | |
), | |
); | |
}, | |
child: Text("Save"), | |
), | |
FutureBuilder( | |
key: UniqueKey(), | |
future: _getDirectory(), | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
Directory appFlutter = snapshot.data!; | |
List<FileSystemEntity> entities = appFlutter.listSync(); | |
return Column( | |
children: [ | |
for (FileSystemEntity entity in entities) | |
_fileOrFolder(entity) | |
], | |
); | |
} | |
return SizedBox.shrink(); | |
}, | |
), | |
], | |
), | |
); | |
} | |
Widget _fileOrFolder(FileSystemEntity entity) { | |
final entityName = entity.path.split("/").last; | |
if (entity is Directory) { | |
return ListTile( | |
leading: Icon(Icons.folder), | |
title: Text(entityName), | |
); | |
} else if (entity is File) { | |
return ListTile( | |
onTap: () { | |
_loadDirectory(entityName); | |
}, | |
onLongPress: () async { | |
bool? isConfirmDelete = await showDialog( | |
context: context, | |
builder: (context) { | |
return AlertDialog( | |
title: Text("Delete"), | |
content: Text('Are you sure to delete $entityName?'), | |
actions: [ | |
OutlinedButton( | |
onPressed: () { | |
Navigator.pop(context); | |
}, | |
child: Text("No"), | |
), | |
FilledButton( | |
onPressed: () { | |
Navigator.pop(context, true); | |
}, | |
child: Text("Yes"), | |
) | |
], | |
); | |
}, | |
); | |
if (isConfirmDelete == true) { | |
File file = File(entity.path); | |
if (await file.exists()) { | |
await file.delete(); | |
setState(() {}); | |
ScaffoldMessenger.of(context).showSnackBar( | |
SnackBar( | |
content: Text('Delete success'), | |
), | |
); | |
} | |
} | |
}, | |
leading: Icon(Icons.picture_as_pdf_outlined), | |
title: Text( | |
(entityName), | |
), | |
subtitle: Text((entity.statSync().size).toString()), | |
trailing: Text( | |
"${entity.statSync().changed.day}/${entity.statSync().changed.month}/${entity.statSync().changed.year} ${entity.statSync().changed.hour}:${entity.statSync().changed.minute}:${entity.statSync().changed.second}"), | |
); | |
} | |
return SizedBox.shrink(); | |
} | |
Future<Directory> _getDirectory() { | |
// return getApplicationDocumentsDirectory(); | |
return getTemporaryDirectory(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment