iOS Development

ios – Flutter-Ontap location isn’t within the path of picture generated by CustomPainter

Spread the love


I’m making an attempt to get the properties of CustomPainter parts when tapped. Nonetheless, the placement generated by onTapUp isn’t throughout the path created by CustomPainter Path.


import 'bundle:flutter/materials.dart';

void essential() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  Offset _tapPosition = Offset.zero;


  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      dwelling: Scaffold(
        appBar: AppBar(
          title: Textual content('Customized Paint Instance'),
        ),
        physique: GestureDetector(
          onTapDown: (TapDownDetails particulars) {
            // Deal with faucet right here
            print('Faucet location: ${particulars.localPosition}');
            print('Faucet location: ${particulars.globalPosition}');
            closing RenderBox renderBox = context.findRenderObject() as RenderBox;
            closing Offset localOffset = renderBox.localToGlobal(particulars.globalPosition);
            Offset customPaintPosition = renderBox.localToGlobal(Offset.zero);
            print(Offset.zero);

            if (isTapInsidePath(LinePainter.path_1, particulars.localPosition)) {
              print('Tapped inside path_1 at ${particulars.localPosition}');
              // You may add logic to get the colour on the faucet place if wanted.
            }
            // _handleClick(particulars.localPosition); 
          },
          little one: Container(
            ornament: BoxDecoration(
              border: Border.all(
                coloration: Colours.black,
                width: 10.0,
              ),
            ),
            little one: CustomPaintExample(),
          ),        ),
      ),
    );
  }
  bool isTapInsidePath(Path path, Offset tapPosition) {
    print (path.getBounds());
    print (path.computeMetrics());

    return path.incorporates(tapPosition);
  }
}

class CustomPaintExample extends StatelessWidget {
  @override
  Widget construct(BuildContext context) {
    return Middle(
      little one: CustomPaint(
        painter: LinePainter(),
        measurement: Measurement(300, 300),
      ),
    );
  }
}


class LinePainter extends CustomPainter {
   static closing Path path_1 = Path()
    ..moveTo(167.309,177.39)
    ..cubicTo(180.907,163.368,200.541,152.36499999999998,213.487,149.762)
    ..cubicTo(170.764,142.488,175.079,155.056,167.309,177.39)
    ..shut();


    
  // static closing Path path_1 = Path();
  @override
  void paint(Canvas canvas, Measurement measurement) {
    Paint paint = Paint()
      ..coloration = Colours.blue
      ..strokeCap = StrokeCap.spherical
      ..strokeWidth = 5.0;

    // Outline a set of strains to be drawn on the canvas
    Listing<Offset> factors = [
      Offset(50, 50),
      Offset(150, 100),
      Offset(100, 200),
      Offset(200, 250),
    ];

    // Draw strains on the canvas
    for (int i = 0; i < factors.size - 1; i++) {
      canvas.drawLine(factors[i], factors[i + 1], paint);
    }


   
   Paint paint_1_fill = Paint()..type=PaintingStyle.fill;
    paint_1_fill.coloration = Shade(0xff000000).withOpacity(1.0);
    canvas.drawPath(path_1,paint_1_fill);

  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
  
}

I’ve checked that the offset is 0. I’ve additionally checked the trail.bounds(), which didn’t appear proper. Beneath is the code.
Location when tapped: flutter: 256.3333282470703‘ and 527.0
Bounds from path_1: Rect.fromLTRB(167.3, 142.5, 213.5, 177.4)

I’ve used world location as properly.

Leave a Reply

Your email address will not be published. Required fields are marked *