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