palm_bounding_box.dart 2.0 KB

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