Hibok
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

85 wiersze
2.7 KiB

  1. import 'package:flutter/material.dart';
  2. const double _kMenuScreenPadding = 8.0;
  3. class TrianglePainter extends CustomPainter {
  4. Paint _paint;
  5. final Color color;
  6. final RelativeRect position;
  7. final Size size;
  8. final double radius;
  9. final bool isInverted;
  10. final double screenWidth;
  11. TrianglePainter(
  12. {@required this.color,
  13. @required this.position,
  14. @required this.size,
  15. this.radius = 20,
  16. this.isInverted = false,
  17. this.screenWidth}) {
  18. _paint = Paint()
  19. ..style = PaintingStyle.fill
  20. ..color = color
  21. ..strokeWidth = 10
  22. ..isAntiAlias = true;
  23. }
  24. @override
  25. void paint(Canvas canvas, Size size) {
  26. var path = Path();
  27. // 如果 menu 的长度 大于 child 的长度
  28. if (size.width > this.size.width) {
  29. // 靠右
  30. if (position.left + this.size.width / 2 > position.right) {
  31. if (screenWidth - (position.left + this.size.width) > size.width / 2 + _kMenuScreenPadding) {
  32. path.moveTo(size.width / 2, isInverted ? 0 : size.height);
  33. path.lineTo(size.width / 2 - radius / 2, isInverted ? size.height : 0);
  34. path.lineTo(size.width / 2 + radius / 2, isInverted ? size.height : 0);
  35. }else {
  36. path.moveTo(size.width - this.size.width + this.size.width / 2,
  37. isInverted ? 0 : size.height);
  38. path.lineTo(
  39. size.width - this.size.width + this.size.width / 2 - radius / 2,
  40. isInverted ? size.height : 0);
  41. path.lineTo(
  42. size.width - this.size.width + this.size.width / 2 + radius / 2,
  43. isInverted ? size.height : 0);
  44. }
  45. }else{
  46. // 靠左
  47. if(position.left > size.width / 2 + _kMenuScreenPadding){
  48. path.moveTo(size.width / 2, isInverted ? 0 : size.height);
  49. path.lineTo(size.width / 2 - radius / 2, isInverted ? size.height : 0);
  50. path.lineTo(size.width / 2 + radius / 2, isInverted ? size.height : 0);
  51. }else {
  52. path.moveTo(this.size.width / 2, isInverted ? 0 : size.height);
  53. path.lineTo(
  54. this.size.width / 2 - radius / 2, isInverted ? size.height : 0);
  55. path.lineTo(
  56. this.size.width / 2 + radius / 2, isInverted ? size.height : 0);
  57. }
  58. }
  59. } else {
  60. path.moveTo(size.width / 2, isInverted ? 0 : size.height);
  61. path.lineTo(
  62. size.width / 2 - radius / 2, isInverted ? size.height : 0);
  63. path.lineTo(
  64. size.width / 2 + radius / 2, isInverted ? size.height : 0);
  65. }
  66. path.close();
  67. canvas.drawPath(
  68. path,
  69. _paint,
  70. );
  71. }
  72. @override
  73. bool shouldRepaint(CustomPainter oldDelegate) {
  74. return true;
  75. }
  76. }