nateshmbhat / touchable

The only flutter library to add gestures and animations to custom shapes you draw on your canvas.
https://pub.dev/packages/touchable
Mozilla Public License 2.0
233 stars 80 forks source link

solution to make text receive touchs detector #28

Open Marrawi1994 opened 3 years ago

Marrawi1994 commented 3 years ago

Hello ... first thanks you for developing awesome library I noticed that drawParagraph function didn't have any touch detector so I created my workaround solution : 1- draw text via textPainter class, like this :

  double x = 100; // Offset -> x
  double y = 100; // Offset -> y

  final textSpan = TextSpan(
      text: 'Hello world',
      style: TextStyle(color: Colors.black, fontSize: 20),
    );

    final textPainter = TextPainter(
      text: textSpan, textDirection: TextDirection.ltr
    );

    textPainter.layout(minWidth: 0, maxWidth: double.infinity);
    textPainter.paint(canvas, Offset(x, y));
    // canvas is the original canvas ( not TouchyCanvas )

2- create new transparent rect with same size as text like this:

  TouchyCanvas myCanvas = TouchyCanvas(context, canvas);
  Paint rectPaint = Paint()..color = Colors.transparent;
  Rect rect = Offset(x, y) & Size(textPainter.width, textPainter.height);
  myCanvas.drawRect(rect, rectPaint);

now you could do anything like translating 👍 just don't forget to translate text and rect

waiting for your big update !!