JamesZhang hace 1 año
padre
commit
a679aaefee
Se han modificado 3 ficheros con 189 adiciones y 35 borrados
  1. 162 15
      lib/components/imageButton.dart
  2. 7 0
      lib/util/fileUtils.dart
  3. 20 20
      lib/util/tools.dart

+ 162 - 15
lib/components/imageButton.dart

@@ -10,7 +10,7 @@ class ImageButton extends StatelessWidget {
   final double borderRadius;
   final Color borderColor;
   final double borderWidth;
-  final VoidCallback onTap;
+  final VoidCallback? onTap;
 
   ImageButton({
     required Size buttonSize,
@@ -20,7 +20,7 @@ class ImageButton extends StatelessWidget {
     this.borderRadius = 5,
     Color? borderColor,
     this.borderWidth = 1,
-    required this.onTap,
+    this.onTap,
   })
     : buttonSize = buttonSize,
       imageSize = imageSize ?? buttonSize,
@@ -32,20 +32,167 @@ class ImageButton extends StatelessWidget {
   Widget build(BuildContext context) {
     return GestureDetector(
       onTap: onTap,
-      child: Container(
-        width: buttonSize.width,
-        height: buttonSize.height,
-        decoration: BoxDecoration(
-          color: bgColor,
-          shape: BoxShape.rectangle,
-          borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
-          border: Border.all(color: borderColor),
+      child: Opacity(
+        opacity: onTap == null ? 0.5 : 1.0,
+        child: Container(
+          width: buttonSize.width,
+          height: buttonSize.height,
+          decoration: BoxDecoration(
+            color: bgColor,
+            shape: BoxShape.rectangle,
+            borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
+            border: Border.all(color: borderColor),
+          ),
+          child: Center(
+            child: Image.asset(
+              image,
+              width: imageSize.width,
+              height: imageSize.height,
+            ),
+          ),
+        ),
+      ),
+    );
+  }
+}
+
+
+class ImageColumnTextButton extends StatelessWidget {
+  final Size buttonSize;
+  final String image;
+  final Size imageSize;
+  final String text;
+  final TextStyle? textStyle;
+  final double space;
+  final Color bgColor;
+  final double borderRadius;
+  final Color borderColor;
+  final double borderWidth;
+  final VoidCallback? onTap;
+
+  ImageColumnTextButton({
+    required Size buttonSize,
+    required this.image,
+    Size? imageSize,
+    required this.text,
+    this.textStyle,
+    this.space = 4,
+    Color bgColor = Colors.white,
+    this.borderRadius = 5,
+    Color? borderColor,
+    this.borderWidth = 1,
+    this.onTap,
+  })
+      : buttonSize = buttonSize,
+        imageSize = imageSize ?? buttonSize,
+        bgColor = bgColor,
+        borderColor = borderColor ?? bgColor
+  ;
+
+  @override
+  Widget build(BuildContext context) {
+    return GestureDetector(
+      onTap: onTap,
+      child: Opacity(
+        opacity: onTap == null ? 0.5 : 1.0,
+        child: Container(
+          width: buttonSize.width,
+          height: buttonSize.height,
+          decoration: BoxDecoration(
+            color: bgColor,
+            shape: BoxShape.rectangle,
+            borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
+            border: Border.all(color: borderColor),
+          ),
+          child: Center(
+            child: Column(
+              mainAxisAlignment: MainAxisAlignment.center,
+              crossAxisAlignment: CrossAxisAlignment.center,
+              children: [
+                Image.asset(
+                  image,
+                  width: imageSize.width,
+                  height: imageSize.height,
+                ),
+                SizedBox(height: space),
+                Text(
+                  text,
+                  style: textStyle,
+                ),
+              ],
+            ),
+          ),
         ),
-        child: Center(
-          child: Image.asset(
-            image,
-            width: imageSize.width,
-            height: imageSize.height,
+      ),
+    );
+  }
+}
+
+
+class ImageRowTextButton extends StatelessWidget {
+  final Size buttonSize;
+  final String image;
+  final Size imageSize;
+  final String text;
+  final TextStyle? textStyle;
+  final double space;
+  final Color bgColor;
+  final double borderRadius;
+  final Color borderColor;
+  final double borderWidth;
+  final VoidCallback? onTap;
+
+  ImageRowTextButton({
+    required Size buttonSize,
+    required this.image,
+    Size? imageSize,
+    required this.text,
+    this.textStyle,
+    this.space = 2,
+    Color bgColor = Colors.white,
+    this.borderRadius = 5,
+    Color? borderColor,
+    this.borderWidth = 1,
+    this.onTap,
+  })
+      : buttonSize = buttonSize,
+        imageSize = imageSize ?? buttonSize,
+        bgColor = bgColor,
+        borderColor = borderColor ?? bgColor
+  ;
+
+  @override
+  Widget build(BuildContext context) {
+    return GestureDetector(
+      onTap: onTap,
+      child: Opacity(
+        opacity: onTap == null ? 0.5 : 1.0,
+        child: Container(
+          width: buttonSize.width,
+          height: buttonSize.height,
+          decoration: BoxDecoration(
+            color: bgColor,
+            shape: BoxShape.rectangle,
+            borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
+            border: Border.all(color: borderColor),
+          ),
+          child: Center(
+            child: Row(
+              mainAxisAlignment: MainAxisAlignment.center,
+              crossAxisAlignment: CrossAxisAlignment.center,
+              children: [
+                Image.asset(
+                  image,
+                  width: imageSize.width,
+                  height: imageSize.height,
+                ),
+                SizedBox(width: space),
+                Text(
+                  text,
+                  style: textStyle,
+                ),
+              ],
+            ),
           ),
         ),
       ),

+ 7 - 0
lib/util/fileUtils.dart

@@ -1,5 +1,6 @@
 
 import 'dart:io';
+import 'dart:math';
 import 'package:path/path.dart' as path;
 import 'logger.dart';
 
@@ -36,6 +37,12 @@ Future<int> fileSizeOf(path) async {
   return size;
 }
 
+String formatFileSize(int bytes) {
+  if (bytes <= 0) return "0 B";
+  const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
+  var i = (log(bytes) / log(1024)).floor();
+  return ((bytes / pow(1024, i)).toStringAsFixed(2)) + ' ' + suffixes[i];
+}
 
 
 createFolder(String folderPath) async {

+ 20 - 20
lib/util/tools.dart

@@ -54,26 +54,26 @@ void printJson(Map input) {
   prettyString.split('\n').forEach((dynamic element) => print(element));
 }
 
-
-String formatFileSize(int size) {
-  final fileSizeInKB = size / 1000;
-  if (fileSizeInKB < 1) {
-    return '$size bytes';
-  }
-  final fileSizeInMB = fileSizeInKB / 1000;
-  if (fileSizeInMB < 1) {
-    return '${fileSizeInKB.toStringAsFixed(2)} KB';
-  }
-  final fileSizeInGB = fileSizeInMB / 1000;
-  if (fileSizeInGB < 1) {
-    return '${fileSizeInMB.toStringAsFixed(2)} MB';
-  }
-  final fileSizeInTB = fileSizeInGB / 1000;
-  if (fileSizeInTB < 1) {
-    return '${fileSizeInGB.toStringAsFixed(2)} GB';
-  }
-  return '${fileSizeInTB.toStringAsFixed(2)} TB';
-}
+// move to fileUtils.dart
+// String formatFileSize(int size) {
+//   final fileSizeInKB = size / 1000;
+//   if (fileSizeInKB < 1) {
+//     return '$size bytes';
+//   }
+//   final fileSizeInMB = fileSizeInKB / 1000;
+//   if (fileSizeInMB < 1) {
+//     return '${fileSizeInKB.toStringAsFixed(2)} KB';
+//   }
+//   final fileSizeInGB = fileSizeInMB / 1000;
+//   if (fileSizeInGB < 1) {
+//     return '${fileSizeInMB.toStringAsFixed(2)} MB';
+//   }
+//   final fileSizeInTB = fileSizeInGB / 1000;
+//   if (fileSizeInTB < 1) {
+//     return '${fileSizeInGB.toStringAsFixed(2)} GB';
+//   }
+//   return '${fileSizeInTB.toStringAsFixed(2)} TB';
+// }
 
 
 // 均匀分组