Skip to content

Instantly share code, notes, and snippets.

@romanejaquez
Last active March 6, 2022 02:59
Show Gist options
  • Save romanejaquez/eedd8007dc24516b6e91edec6e6d8ae7 to your computer and use it in GitHub Desktop.
Save romanejaquez/eedd8007dc24516b6e91edec6e6d8ae7 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(
Provider(
create: (_) => FileProcessingService(),
child: MyApp()
)
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MainPageTest(),
),
),
);
}
}
class MainPageTest extends StatefulWidget {
const MainPageTest({Key? key}) : super(key: key);
@override
State<MainPageTest> createState() => _MainPageTestState();
}
class _MainPageTestState extends State<MainPageTest> {
final double _buttonTextSize = 20.0;
final double _widgetWidth = 200.0;
List<String>? _files;
late final TextEditingController _textController;
@override
void initState() {
super.initState();
_textController = TextEditingController();
}
@override
void dispose() {
_textController.dispose();
super.dispose();
}
List<String> pickFiles() {
return [
'FileName1.png',
'AnotherFile2.jpg',
'ThisIsAnotherfile.mp3',
'LastFileInTheList.mp4'
];
}
@override
Widget build(BuildContext context) {
var fileService = Provider.of<FileProcessingService>(context, listen: false);
return Center(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: _widgetWidth,
child: TextButton(
onPressed: () {
fileService.filesToPic.value = pickFiles();
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Column(
children: [
Text(
'Choose Files',
style: TextStyle(
fontSize: _buttonTextSize,
),
),
ValueListenableBuilder<List<String>>(
valueListenable: fileService.filesToPic,
builder: (context, value, child) {
if (value.isEmpty) {
return const Text('no files');
}
return Text('${value.length} files picked');
}
)
]
),
),
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: SizedBox(
width: _widgetWidth,
child: TextButton(
onPressed: () async {
await EnigmaTest().encrypt(fileService.filesToPic.value, context);
MyLoadingAlertDialog.showLoadingAlert(context, fileService.filesToPic.value.length);
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child: Text(
'Encrypt',
style: TextStyle(
fontSize: _buttonTextSize,
),
),
),
),
),
),
],
),
);
}
}
class EnigmaTest {
Future<void> encrypt(List<String> files, BuildContext context) async {
// THIS IS FAKING YOUR ENCRYPTION LOGIC/ REPLACE WITH YOUR LOGIC
// AND MAKE SURE THAT, AS FILES ARE PROCESSED,
// YOU CALL notifyFileBeingProcessed SO THE DIALOG GETS NOTIFIED
var fileService = Provider.of<FileProcessingService>(context, listen: false);
for(var i = 0; i < files.length; i++) {
Future.delayed(Duration(seconds: i * 1), () {
fileService.notifyFileBeingProcessed(files[i]);
});
}
}
}
class MyLoadingAlertDialog {
static showLoadingAlert(BuildContext context, int totalFiles) {
var fileService = Provider.of<FileProcessingService>(context, listen: false);
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Progress'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.all(4.0),
child: ValueListenableBuilder<String>(
valueListenable: fileService.fileName,
builder: (context, value, child) {
var numOfFiles = fileService.numOfFilesProcessed;
if (numOfFiles == totalFiles) {
return const Text('All Files done!');
}
if (value.isEmpty) {
return const Text('No value being processed!');
}
return Column(
children: [
Text(
'currently encrypting $value...',
style: const TextStyle(
fontSize: 10.0, fontStyle: FontStyle.italic),
),
SizedBox(
width: double.infinity,
child: LinearProgressIndicator(value: fileService.numOfFilesProcessed * 0.25),
),
]
);
}
),
),
],
),
);
},
);
}
}
// SIMPLE SERVICE CLASS TO SERVE AS THE COMMUNICATION BETWEEN WIDGETS
class FileProcessingService {
// ValueNotifiers trigger notifications
// whoever is listening on these properties (i.e. filesToPic, fileName)
// will rebuild, showing the current state of their values
ValueNotifier<List<String>> filesToPic = ValueNotifier([]);
ValueNotifier<String> fileName = ValueNotifier('');
int numOfFilesProcessed = 0;
// helper method to update the files and increment the count
void notifyFileBeingProcessed(String f) {
numOfFilesProcessed++;
fileName.value = f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment