palm_bounding_box.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'package:flutter/material.dart';
  2. /// A shared widget for displaying MPOB-standard bounding boxes.
  3. class PalmBoundingBox extends StatelessWidget {
  4. final Rect normalizedRect;
  5. final String label;
  6. final double confidence;
  7. final BoxConstraints constraints;
  8. final bool isLocked;
  9. const PalmBoundingBox({
  10. super.key,
  11. required this.normalizedRect,
  12. required this.label,
  13. required this.confidence,
  14. required this.constraints,
  15. this.isLocked = false,
  16. });
  17. /// MPOB-standard color palette
  18. Color _getMPOBColor(String label) {
  19. if (isLocked) return const Color(0xFF22C55E); // Force Green when locked
  20. switch (label) {
  21. case 'Ripe':
  22. return const Color(0xFF22C55E); // Industrial Green
  23. case 'Underripe':
  24. return const Color(0xFFFBBF24); // Industrial Orange/Yellow
  25. case 'Unripe':
  26. return const Color(0xFF3B82F6); // Industrial Blue
  27. case 'Abnormal':
  28. return const Color(0xFFDC2626); // Critical Red
  29. case 'Empty_Bunch':
  30. return const Color(0xFF64748B); // Waste Gray
  31. default:
  32. return Colors.yellow;
  33. }
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. final color = _getMPOBColor(label);
  38. return Positioned(
  39. left: normalizedRect.left * constraints.maxWidth,
  40. top: normalizedRect.top * constraints.maxHeight,
  41. width: normalizedRect.width * constraints.maxWidth,
  42. height: normalizedRect.height * constraints.maxHeight,
  43. child: Container(
  44. decoration: BoxDecoration(
  45. border: Border.all(color: color, width: 3),
  46. borderRadius: BorderRadius.circular(4),
  47. ),
  48. child: Align(
  49. alignment: Alignment.topLeft,
  50. child: Container(
  51. padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
  52. color: color,
  53. child: Text(
  54. "$label ${(confidence * 100).toStringAsFixed(0)}%",
  55. style: const TextStyle(
  56. color: Colors.white,
  57. fontSize: 10,
  58. fontWeight: FontWeight.bold,
  59. ),
  60. ),
  61. ),
  62. ),
  63. ),
  64. );
  65. }
  66. }