搜索插件、代码仓与 APP 分类

touchable 封面
插件详情v1.0.2

touchable

Flutter 库,可为 CustomPainter 中绘制的每个形状添加各种手势回调

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.4万
喜欢数
232
Pub 分数
130

安装插件

flutter pub add touchable

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 库,可在 CustomPainter 内绘制的单个形状上启用手势检测。它允许开发者将各种触摸回调(如 onTap、onLongPress 和 onPan)附加到画布上的特定形状,从而增强绘图、绘画和签名应用的交互性。非常适合创建用户可以点击或操作不同元素的交互式画布。

适合场景

  • 交互式绘图应用
  • 基于画布的签名工具
  • 手势响应式图表
  • 具有形状级别交互功能的自定义画笔
  • 教育绘图界面

主要特点

  • 形状级手势检测
  • 支持 onTap、onLongPress、onPan
  • 与 CustomPainter 的轻松集成
  • 适用于所有 Flutter 平台
  • 轻量且专注的 API

适合对象

  • 开发交互式画布功能的开发者
  • 需要触摸反馈的绘制形状的应用程序
  • 具有复杂手势逻辑的自定义绘图UI

使用建议

  • 仅限 CustomPainter 内的形状
  • 默认情况下不处理重叠的形状
  • 在复杂布局中需要仔细管理命中测试

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Flutter library to bring your CustomPainter 🎨 to Life ✨⚡️

touchable library gives you the ability to add various gestures and animations to each Shape you draw on your canvas in the CustomPainter

           

Index :

Why Use Touchable ?

  • The CustomPainter lets you only draw shapes on the canvas. But most would want to let user interact with the drawings.

  • Add all kinds of gesture callbacks to each drawing to give interaction capability to the shapes you draw on the canvas.

  • Animating individual shapes has never been this easy.

  • Auto Handles the painting style (filled ▮ , stroke ▯) and detects touch accordingly.

  • Handles Painting stroke width. So if your shapes are painted thick , we still got it covered ✓

  • Supports clipping and different clipping modes for the drawings.

  • Supports HitTestBehavior for each shape.

  • Simple and Easy API. Just wrap your CustomPaint with CanvasTouchDetector and use the TouchyCanvas in your painter.

With touchable , you get what the normal canvas always missed : touchability 😉

Installation

Add the touchable package as dependency in your pubspec.yaml

dependencies:
  touchable:

Usage

  • Just Wrap your CustomPaint widget with CanvasTouchDetector. It takes a builder function as argument that expects your CustomPaint widget as shown below.
CanvasTouchDetector(
    builder: (context) => 
        CustomPaint(
            painter: MyPainter(context)
        )
)
  • Inside your CustomPainter class's paint method , create and use the TouchyCanvas object (using the context obtained from the CanvasTouchDetector and canvas) 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: (tapdetail) {
         print("orange Circle touched");
       },
        onPanDown:(tapdetail){
          print("orange circle swiped");
        } 
    );

    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
    });
  }
}
Read the article on Medium : Bring Your CustomPainter to Life using Touchable


How Touchable Works

When you draw shapes on the canvas (TouchyCanvas) , it keeps track of the dimensions of each shape you draw and their painting style , stroke , order , clippings etc.

When user performs any gesture on the screen , based on the location of the gesture , the appropriate shape is selected from the lot taking clipping regions , paint , hitTest behaviour etc into account in an optimized way. Callbacks of the corresponding shapes (one or more depending on the hitTest behavior) are executed.

Road Map

  • [x] Basic Shape Detection
    • [x] Line
    • [x] Rectangle (Rect)
    • [x] Circle
    • [x] Oval or Ellipse
    • [x] Arc
      • [x] segment
      • [x] sector
    • [x] Rounded Rectangle (RRect)
    • [x] Custom Path [only supports opaque hittest]
    • [x] Points (PointMode.points , PointMode.lines , PointMode.polygon)
    • [ ] Vertices
      • [ ] Traingle
      • [ ] Traingle Strip
      • [ ] Traingle Fan
  • [x] Support for proper edge detection based on the Paint object properties :
    • [x] Paint style
    • [x] Stroke Width
    • [ ] Stroke Cap
      • [x] StrokeCap to draw Points
      • [ ] StrokeCap.round for lines with huge width.
  • [x] Support Clipping and clipping modes
    • [x] ClipRect
      • [x] intersect mode [Touch detection enabled only inside the clipped region]
      • [x] difference mode [Touch detection enabled only outside the clipped region]
    • [x] ClipRRect
    • [x] ClipPath
  • [x] Support for HitTestBehavior
  • [ ] Make the touch detection handling to run in a seperate isolate.
  • [ ] Support for translation , rotation , scaling and skewing transformations that needs some vector math

Links

所属分类

源码仓库

使用此插件的项目