ANTIPALSU Label template editor using flutter

battery_painter.dart 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) 2021 Razeware LLC
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
  14. // distribute, sublicense, create a derivative work, and/or sell copies of the
  15. // Software in any work that is designed, intended, or marketed for pedagogical
  16. // or instructional purposes related to programming, coding, application
  17. // development, or information technology. Permission for such use, copying,
  18. // modification, merger, publication, distribution, sublicensing, creation of
  19. // derivative works, or sale is expressly withheld.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. // THE SOFTWARE.
  28. import 'package:flutter/material.dart';
  29. import 'dart:math' as math;
  30. class BatteryPainter extends CustomPainter {
  31. final margin = 30.0; // The space between the battery and the parent widget
  32. final padding = 10.0; // The space between the charge & pin and the border
  33. final pinWidth = 22.0; // The width of the pin
  34. final minCharge = 0.2; // The minimum charge until the battery disappears
  35. final double charge;
  36. final borderPaint = Paint()
  37. ..color = Colors.black
  38. ..strokeWidth = 4
  39. ..isAntiAlias = true
  40. ..style = PaintingStyle.stroke;
  41. final pinPaint = Paint()
  42. ..color = Colors.black
  43. ..isAntiAlias = true
  44. ..style = PaintingStyle.fill;
  45. final chargePaint = Paint()
  46. ..color = Colors.greenAccent[700]!
  47. ..isAntiAlias = true
  48. ..style = PaintingStyle.fill;
  49. BatteryPainter({required this.charge});
  50. @override
  51. void paint(Canvas canvas, Size size) {
  52. RRect _borderRRect(Size size) {
  53. // 1
  54. final symmetricalMargin = margin * 2;
  55. // 2
  56. final width = size.width - symmetricalMargin - padding - pinWidth;
  57. // 3
  58. final height = width / 2;
  59. // 4
  60. final top = (size.height / 2) - (height / 2);
  61. // 5
  62. final radius = Radius.circular(height * 0.2);
  63. // 6
  64. final bounds = Rect.fromLTWH(margin, top, width, height);
  65. // 7
  66. return RRect.fromRectAndRadius(bounds, radius);
  67. }
  68. final bdr = _borderRRect(size);
  69. canvas.drawRRect(bdr, borderPaint);
  70. Rect _pinRect(RRect bdr) {
  71. // 1
  72. final center = Offset(bdr.right + padding, bdr.top + (bdr.height / 2.0));
  73. // 2
  74. final height = bdr.height * 0.38;
  75. // 3
  76. final width = pinWidth * 2;
  77. // 4
  78. return Rect.fromCenter(center: center, width: width, height: height);
  79. }
  80. // Battery pin
  81. final pinRect = _pinRect(bdr);
  82. canvas.drawArc(pinRect, math.pi / 2, -math.pi, true, pinPaint);
  83. RRect _chargeRRect(RRect bdr) {
  84. final percent = minCharge * ((charge / minCharge).round());
  85. final left = bdr.left + padding;
  86. final top = bdr.top + padding;
  87. final right = bdr.right - padding;
  88. final bottom = bdr.bottom - padding;
  89. final height = bottom - top;
  90. final width = right - left;
  91. final radius = Radius.circular(height * 0.15);
  92. final rect = Rect.fromLTWH(left, top, width * percent, height);
  93. return RRect.fromRectAndRadius(rect, radius);
  94. }
  95. // Battery charge progress
  96. final chargeRRect = _chargeRRect(bdr);
  97. canvas.drawRRect(chargeRRect, chargePaint);
  98. }
  99. @override
  100. bool shouldRepaint(covariant BatteryPainter oldDelegate) {
  101. return oldDelegate.charge != charge;
  102. }
  103. }