You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4KB

  1. import 'package:flutter/material.dart';
  2. class RoundedRectIndicator extends Decoration {
  3. final BoxPainter _painter;
  4. RoundedRectIndicator({
  5. @required Color color,
  6. @required double radius,
  7. double padding = 0.0,
  8. double weight = 3.0,
  9. }) : _painter = _RectPainter(color, radius, padding, weight);
  10. @override
  11. BoxPainter createBoxPainter([onChanged]) {
  12. return _painter;
  13. }
  14. }
  15. class _RectPainter extends BoxPainter {
  16. final Paint _paint;
  17. final double radius;
  18. final double padding;
  19. final double weight;
  20. final indicatorPaddingBottom = 4.0;
  21. _RectPainter(Color color, this.radius, this.padding, this.weight)
  22. : _paint = Paint()
  23. ..color = color
  24. ..isAntiAlias = true;
  25. @override
  26. void paint(Canvas canvas, Offset offset, ImageConfiguration cfg) {
  27. var width = cfg.size.width;
  28. var height = cfg.size.height;
  29. var left = 0.0;
  30. var top = height - indicatorPaddingBottom;
  31. var right = width;
  32. var bottom = height - weight - indicatorPaddingBottom;
  33. //calculate offset
  34. left = left + offset.dx + padding;
  35. right = right + offset.dx - padding;
  36. var rect = RRect.fromLTRBAndCorners(left, top, right, bottom,
  37. topLeft: Radius.circular(radius),
  38. bottomLeft: Radius.circular(radius),
  39. bottomRight: Radius.circular(radius),
  40. topRight: Radius.circular(radius));
  41. canvas.drawRRect(rect, _paint);
  42. }
  43. }