touchable 0.1.0 touchable: ^0.1.0 copied to clipboard
Draw Shapes on your canvas and add full interactive capability and gesture callbacks to each object.
touchable 👆🏻 #
Flutter library to add various gesture callbacks to each Shape you draw on your canvas in your CustomPainter
Why Use Touchable ? #
-
The CustomPainter lets you only draw shapes on the canvas. But most would want to let user interact with the drawings.
-
With touchable , you get what the normal canvas always missed : touchability 😉
-
With this , its possible to add all kinds of gesture callbacks to each drawing and thus interaction capability to each Shape you draw on the canvas.
-
Animating individual shapes becomes so much easier than ever before.
-
Automatically handles the painting style of your drawing. So , when your paint is
filled ▮
it registers touch on the entire shape else when itsstroke ▯
, it looks for gesture only on the borders. -
Takes the painting stroke width also into account. So if your shapes are painted thick , we still got it covered ✓
-
Supports and handles clipping and different clipping modes. So, You can have any kind of complex clipping and drawing combinations while getting full interactive capability.
-
Simple and Easy API. Just wrap your
CustomPaint
withCanvasTouchDetector
and use theTouchCanvas
in your painter.
Usage : #
- Just Wrap your
CustomPaint
widget withCanvasTouchDetector
. It takes abuilder
function as argument that expects yourCustomPaint
widget as shown below.
CanvasTouchDetector(
builder: (context) =>
CustomPaint(
painter: MyPainter(context)
)
)
- Inside your
CustomPainter
class'spaint
method , create and use theTouchyCanvas
object (using thecontext
obtained from the CanvasTouchDetector andcanvas
) to draw any shape with different gesture callbacks.
var myCanvas = TouchyCanvas(context,canvas);
myCanvas.drawRect( rect , Paint() , onTapDown: (tapDetail){
//Do stuff here. Probably change your state and animate
});
MyPainter example : #
class MyPainter extends CustomPainter {
final BuildContext context ;
MyPainter(this.context); // context from CanvasTouchDetector
@override
void paint(Canvas canvas, Size size) {
var myCanvas = TouchyCanvas(context,canvas);
myCanvas.drawCircle(Offset(10, 10), 60, Paint()..color=Colors.orange ,
onTapDown: (_) {
print("orange Circle touched");
});
myCanvas.drawLine(
Offset(0, 0),
Offset(size.width - 100, size.height - 100),
Paint()
..color = Colors.black
..strokeWidth = 50,
onPanUpdate: (detail) {
print('Black line Swiped'); //do cooler things here. Probably change app state or animate
});
}
}