Main coves client
at main 49 lines 1.6 kB view raw
1import 'package:flutter/material.dart'; 2 3import '../constants/app_colors.dart'; 4 5/// Utility class for shared display formatting and styling 6/// 7/// Centralizes common display logic to avoid duplication across widgets: 8/// - Avatar fallback colors (consistent color generation by name hash) 9/// - Number formatting (K/M suffixes for large numbers) 10class DisplayUtils { 11 DisplayUtils._(); 12 13 /// Fallback colors for avatars when no image is available 14 /// 15 /// Used by community avatars, user avatars, and other fallback displays. 16 /// Color is deterministically selected based on name hash for consistency. 17 static const fallbackColors = [ 18 AppColors.coral, 19 AppColors.teal, 20 Color(0xFF9B59B6), // Purple 21 Color(0xFF3498DB), // Blue 22 Color(0xFF27AE60), // Green 23 Color(0xFFE74C3C), // Red 24 ]; 25 26 /// Get a consistent fallback color for a given name 27 /// 28 /// Uses hash code to deterministically select a color from [fallbackColors]. 29 /// The same name will always return the same color. 30 static Color getFallbackColor(String name) { 31 final colorIndex = name.hashCode.abs() % fallbackColors.length; 32 return fallbackColors[colorIndex]; 33 } 34 35 /// Format a number with K/M suffixes for compact display 36 /// 37 /// Examples: 38 /// - 500 -> "500" 39 /// - 1,234 -> "1.2K" 40 /// - 1,500,000 -> "1.5M" 41 static String formatCount(int count) { 42 if (count >= 1000000) { 43 return '${(count / 1000000).toStringAsFixed(1)}M'; 44 } else if (count >= 1000) { 45 return '${(count / 1000).toStringAsFixed(1)}K'; 46 } 47 return count.toString(); 48 } 49}