flutter 五点一点三:简简单单小画板

class C extends StatefulWidget {
  @override
  State createState() => CState();
}



class CState extends State {

  List points = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MouseRegion(
        child: GestureDetector(
          onPanDown: (details){
            print("onPanDown>>${details}");
          },
          onPanStart: (details){
            print("onPanStart>>${details}");
          },
          onPanUpdate: (details){
            print("onPanUpdate>>${details}");

            print("x:${details.globalPosition.dx}  y:${details.globalPosition.dy}");
            setState(() {
              points.add(Offset(details.globalPosition.dx, details.globalPosition.dy));
            });
          },
          onPanEnd: (details){
            print("onPanEnd>>${details}");
          },
          onPanCancel: (){
            print("onPanCancel>>");
          },
          child:CustomPaint(
            painter: MyPainter(points),
            size: Size.infinite,
          )
        ),

      )
    );
  }
}


class MyPainter extends CustomPainter{
  final List points;

  MyPainter(this.points);

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

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = Colors.yellow
      ..strokeWidth = 5;

    canvas.drawPoints(
        PointMode.points,
        points,
        paint
    );
  }
  
}

flutter 五点一点三:简简单单小画板_第1张图片

你可能感兴趣的:(flutter)