Last active
August 19, 2020 15:41
-
-
Save nateshmbhat/628355e7ed980c3502c5d04fa364bc39 to your computer and use it in GitHub Desktop.
touchable usage flutter demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:touchable/touchable.dart'; | |
class MyCoolPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.white, | |
//wrap CustomPaint with CanvasTouchDetector | |
child: CanvasTouchDetector( | |
builder: (context) => CustomPaint( | |
painter: MyPainter(context), | |
), | |
)); | |
} | |
} | |
class MyPainter extends CustomPainter { | |
final BuildContext context; | |
MyPainter(this.context); | |
@override | |
void paint(Canvas canvas, Size size) { | |
//Create and use TouchyCanvas to draw | |
TouchyCanvas touchyCanvas = TouchyCanvas(context, canvas); | |
var blueCircle = Offset(size.width / 2, size.height / 2 - 100); | |
var greenCircle = Offset(size.width / 2, size.height / 2 + 100); | |
touchyCanvas.drawCircle(blueCircle, 60, Paint()..color = Colors.blue, onTapDown: (_) { | |
print('You clicked BLUE circle'); | |
}); | |
touchyCanvas.drawCircle(greenCircle, 30, Paint()..color = Colors.green, onLongPressStart: (_) { | |
print('long pressed on GREEN circle'); | |
}); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment