From c487ce92d0fd1f458f757e445811c8fd14760c18 Mon Sep 17 00:00:00 2001 From: Deven Joshi Date: Fri, 11 Dec 2020 19:54:02 +0530 Subject: [PATCH] feat: Added file size in proper format --- lib/src/file_attachment.dart | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/src/file_attachment.dart b/lib/src/file_attachment.dart index 95e628f0..4bba7779 100644 --- a/lib/src/file_attachment.dart +++ b/lib/src/file_attachment.dart @@ -99,7 +99,7 @@ class _FileAttachmentState extends State { height: 3.0, ), Text( - '${widget.attachment.extraData['file_size'] ?? 'N/A'} bytes', + '${_getSizeText(widget.attachment.extraData['file_size'])}', style: TextStyle( color: Colors.black.withOpacity(0.5), fontSize: 14.0, @@ -284,4 +284,18 @@ class _FileAttachmentState extends State { break; } } + + String _getSizeText(int bytes) { + if (bytes == null) { + return 'Size N/A'; + } + + if (bytes <= 1000) { + return '${bytes} bytes'; + } else if (bytes <= 100000) { + return '${(bytes / 1000).toStringAsFixed(2)} KB'; + } else { + return '${(bytes / 1000000).toStringAsFixed(2)} MB'; + } + } }