import 'package:flutter/material.dart'; import '../models/filament_model.dart'; class FilamentItemWidget extends StatelessWidget { final FilamentModel filament; final VoidCallback? onTap; const FilamentItemWidget({ super.key, required this.filament, this.onTap, }); // Hilfsmethode zur Bestimmung der Farbe basierend auf dem String "Color" Color _parseColor(String colorName) { switch (colorName.toLowerCase().trim()) { case 'weiß': return Colors.white; case 'schwarz': return Colors.black; case 'hell braun': case 'braun': return Colors.brown.shade400; case 'knochenfarbe': return const Color(0xFFE3DAC9); // Bone / Beige default: return Colors.grey; } } // Hilfsmethode zur Umwandlung von "1kg", "7kg" etc. in Gramm double _parseTotalWeightInGramm(String mengeStr) { final cleaned = mengeStr.toLowerCase().replaceAll('kg', '').trim(); final kg = double.tryParse(cleaned) ?? 1.0; return kg * 1000.0; } @override Widget build(BuildContext context) { final parsedColor = _parseColor(filament.color!); final restGramm = double.tryParse(filament.restMengeAktuelleRolleGramm!) ?? 0.0; final totalGramm = _parseTotalWeightInGramm(filament.menge!); final progress = (restGramm / totalGramm).clamp(0.0, 1.0); return Card( elevation: 0, margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), side: BorderSide(color: Colors.grey.shade200), ), child: InkWell( borderRadius: BorderRadius.circular(16), onTap: onTap, child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Header: Typ, Farbindikator, Spule/Menge Row( children: [ // Farbpunkte mit Schatten für weiße/helle Farben Container( width: 24, height: 24, decoration: BoxDecoration( color: parsedColor, shape: BoxShape.circle, border: Border.all(color: Colors.grey.shade300, width: 1), boxShadow: [ BoxShadow( color: parsedColor.withValues(alpha: 0.4), blurRadius: 6, spreadRadius: 1, ) ], ), ), const SizedBox(width: 12), // Material & Farbe Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( filament.art!, style: const TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), Text( filament.color!, style: TextStyle( fontSize: 14, color: Colors.grey.shade600, ), ), ], ), ), // Spulengröße Badge Chip( padding: EdgeInsets.zero, labelStyle: const TextStyle(fontSize: 12), label: Text(filament.menge!), backgroundColor: Colors.teal.shade50, side: BorderSide.none, ), ], ), const SizedBox(height: 16), // Temperaturen (Nozzle & Bed) Row( children: [ _buildIconText( icon: Icons.thermostat, label: 'Nozzle', value: filament.nozzelTemperatur!, color: Colors.orange.shade700, ), const SizedBox(width: 24), _buildIconText( icon: Icons.hot_tub, label: 'Bett', value: filament.bettTemperatur!, color: Colors.blue.shade700, ), ], ), const SizedBox(height: 16), // Restmengen Progress Indicator Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Restmenge', style: TextStyle( fontSize: 12, color: Colors.grey.shade600, ), ), Text( '${restGramm.toInt()}g / ${totalGramm.toInt()}g', style: const TextStyle( fontSize: 12, fontWeight: FontWeight.bold, ), ), ], ), const SizedBox(height: 6), ClipRRect( borderRadius: BorderRadius.circular(4), child: LinearProgressIndicator( value: progress, minHeight: 6, backgroundColor: Colors.grey.shade200, color: progress < 0.2 ? Colors.red : Colors.teal.shade400, ), ), ], ), ], ), ), ), ); } Widget _buildIconText({ required IconData icon, required String label, required String value, required Color color, }) { return Row( children: [ Icon(icon, size: 18, color: color), const SizedBox(width: 4), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( label, style: TextStyle(fontSize: 10, color: Colors.grey.shade600), ), Text( value, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600), ), ], ), ], ); } }