my_position_point.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'package:flutter/material.dart';
  2. import 'package:trackoffical_app/screen.dart';
  3. import 'dart:ui' as ui;
  4. class MyPositionPoint extends StatelessWidget{
  5. const MyPositionPoint({
  6. super.key,
  7. required this.offset,
  8. required this.color,
  9. this.radius,
  10. this.direction
  11. });
  12. final Color color;
  13. final Offset offset;
  14. final double? radius;
  15. final double? direction;
  16. @override
  17. Widget build(BuildContext context) {
  18. final width = radius??6.62.wp;
  19. final height = width*3.3;
  20. final hasDirection = direction!= null;
  21. Widget child = CustomPaint(
  22. painter: _LayerMyPositionPointPainter(color, hasDirection, 255),
  23. size: Size(width, height),
  24. );
  25. if(hasDirection){
  26. child = Transform.rotate(
  27. angle: direction!,
  28. alignment: Alignment.center,
  29. child: child);
  30. }
  31. return Positioned(
  32. left: offset.dx-width/2,
  33. top: offset.dy-height/2,
  34. child: child);
  35. }
  36. }
  37. class _LayerMyPositionPointPainter extends CustomPainter {
  38. final Color color;
  39. final bool hasDirection;
  40. final int alpha;
  41. _LayerMyPositionPointPainter(
  42. this.color,
  43. this.hasDirection,
  44. this.alpha
  45. );
  46. @override
  47. void paint(Canvas canvas, Size size) {
  48. final paint = Paint();
  49. drawMyLocation(canvas, paint, size);
  50. }
  51. @override
  52. bool shouldRepaint(CustomPainter oldDelegate) => false;
  53. drawMyLocation(Canvas canvas, Paint paint, Size size) {
  54. final color = this.color.withAlpha(alpha);
  55. final color2 = this.color.withAlpha((alpha*0.8).toInt());
  56. final center = Offset(size.width/2, size.height/2);
  57. paint.color = color;
  58. var rect = Rect.fromLTRB(0 , 0, size.width, center.dy);
  59. paint.shader = ui.Gradient.linear(rect.topCenter, rect.bottomCenter, [
  60. const Color(0x00FFFFFF),
  61. color2
  62. ], [
  63. 0,
  64. 1,
  65. ]);
  66. final path = Path();
  67. path.moveTo(rect.topLeft.dx, rect.topLeft.dy);
  68. path.lineTo(rect.topRight.dx, rect.topRight.dy);
  69. path.lineTo(rect.bottomCenter.dx, rect.bottomCenter.dy);
  70. canvas.drawPath(path, paint);
  71. paint.shader = null;
  72. paint.color = Colors.white.withAlpha(alpha);
  73. canvas.drawCircle(center, center.dx* 0.8, paint);
  74. paint.color = color;
  75. canvas.drawCircle(center, center.dx * 0.5, paint);
  76. }
  77. }
  78. void main(){
  79. runPreview(Scaffold(
  80. backgroundColor: Colors.white,
  81. appBar: AppBar(title: const Text('data'),),
  82. body:const SizedBox(
  83. height: double.infinity,
  84. width: double.infinity,
  85. child: Stack(
  86. children: [
  87. MyPositionPoint(offset: Offset(200, 200), color: Colors.red, direction: 0,)
  88. ],
  89. ),
  90. )
  91. ));
  92. }