error handling with nice snackbar on fabs

This commit is contained in:
RafayAhmad7548 2025-07-28 08:24:22 +05:00
parent eb723b74fd
commit 3d86afa592

View file

@ -157,23 +157,13 @@ class _SftpExplorerState extends State<SftpExplorer> {
await widget.sftpClient.mkdir('${widget.path}${nameController.text}'); await widget.sftpClient.mkdir('${widget.path}${nameController.text}');
_listDir(); _listDir();
} }
on SftpStatusError catch (e) { on SftpStatusError catch (e) {
if (context.mounted) { if (context.mounted) {
if (e.code == 4) { if (e.code == 4) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Folder Already Exists'));
SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Folder Already Exists')
)
);
} }
else { else {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Error: ${e.message}'));
SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Error: ${e.message}')
)
);
} }
} }
} }
@ -194,20 +184,32 @@ class _SftpExplorerState extends State<SftpExplorer> {
onPressed: () async { onPressed: () async {
final List<XFile> files = await openFiles(); final List<XFile> files = await openFiles();
for (XFile file in files) { for (XFile file in files) {
final remoteFile = await widget.sftpClient.open('${widget.path}${file.name}', mode: SftpFileOpenMode.create | SftpFileOpenMode.write | SftpFileOpenMode.exclusive); try {
final fileSize = await file.length(); final remoteFile = await widget.sftpClient.open('${widget.path}${file.name}', mode: SftpFileOpenMode.create | SftpFileOpenMode.write | SftpFileOpenMode.exclusive);
final uploader = remoteFile.write( final fileSize = await file.length();
file.openRead().cast(), final uploader = remoteFile.write(
onProgress: (progress) => setState(() => _progress = progress/fileSize) file.openRead().cast(),
); onProgress: (progress) => setState(() => _progress = progress/fileSize)
setState(() { );
_loader = uploader; setState(() {
_loadingFileName = file.name; _loader = uploader;
}); _loadingFileName = file.name;
await uploader.done; });
await uploader.done;
setState(() => _loader = null);
_listDir();
}
on SftpStatusError catch (e) {
if (context.mounted) {
if (e.code == 4) {
ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'File Already Exists'));
}
else {
ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Error: ${e.message}'));
}
}
}
} }
setState(() => _loader = null);
_listDir();
}, },
child: Icon(Icons.upload), child: Icon(Icons.upload),
), ),
@ -215,6 +217,20 @@ class _SftpExplorerState extends State<SftpExplorer> {
); );
} }
SnackBar _buildErrorSnackBar(BuildContext context, String error) {
return SnackBar(
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
behavior: SnackBarBehavior.floating,
content: Row(
spacing: 10,
children: [
Icon(Icons.error, color: Colors.red,),
Text(error, style: TextStyle(color: Theme.of(context).colorScheme.onSecondaryContainer),),
],
)
);
}
Widget _buildLoadingWidget(BuildContext context) { Widget _buildLoadingWidget(BuildContext context) {
return _loader != null ? Container( return _loader != null ? Container(
color: Theme.of(context).colorScheme.secondaryContainer, color: Theme.of(context).colorScheme.secondaryContainer,