7

i want to create a signature area like Here with dart in a mobile app!

I tried to use the CustomPaint class ... But it doesn't work.

Can anyone help me?

4
  • 7
    We discourage posts that simply state a problem out of context, and expect the community to solve it. Assuming you tried to solve it yourself and got stuck, it may be helpful if you wrote your thoughts and what you could not figure out. Add an minimal reproducible example of your code, and describe what problems you're having. "But it doesn't work" isn't very helpful.
    – Cerbrus
    Oct 11, 2017 at 8:28
  • 1
    Your program doesn't work? Oct 11, 2017 at 12:03
  • Can the delete voter explain their vote? The answer posted here seems rather helpful....
    – user000001
    Oct 11, 2017 at 19:35
  • 3
    The Meta effect, @user000001. I agree that deletion is completely unwarranted, given the high quality answer. If it gets deleted anyway, please flag it. Oct 12, 2017 at 9:20

1 Answer 1

60

You can create a signature area using GestureDetector to record touches and CustomPaint to draw on the screen. Here are a few tips:

video

import 'package:flutter/material.dart';

class SignaturePainter extends CustomPainter {
  SignaturePainter(this.points);

  final List<Offset> points;

  void paint(Canvas canvas, Size size) {
    Paint paint = new Paint()
      ..color = Colors.black
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 5.0;
    for (int i = 0; i < points.length - 1; i++) {
      if (points[i] != null && points[i + 1] != null)
        canvas.drawLine(points[i], points[i + 1], paint);
    }
  }

  bool shouldRepaint(SignaturePainter other) => other.points != points;
}

class Signature extends StatefulWidget {
  SignatureState createState() => new SignatureState();
}

class SignatureState extends State<Signature> {
  List<Offset> _points = <Offset>[];

  Widget build(BuildContext context) {
    return new Stack(
      children: [
        GestureDetector(
          onPanUpdate: (DragUpdateDetails details) {
            RenderBox referenceBox = context.findRenderObject();
            Offset localPosition =
                referenceBox.globalToLocal(details.globalPosition);

            setState(() {
              _points = new List.from(_points)..add(localPosition);
            });
          },
          onPanEnd: (DragEndDetails details) => _points.add(null),
        ),
        CustomPaint(painter: SignaturePainter(_points), size: Size.infinite),
      ],
    );
  }
}

class DemoApp extends StatelessWidget {
  Widget build(BuildContext context) => new Scaffold(body: new Signature());
}

void main() => runApp(new MaterialApp(home: new DemoApp()));
10
  • This code does not seem to work anymore with the beta channel of flutter. I tried on emulator and on a real device (Pixel 2) but nothing draws on the screen.
    – xrd
    Mar 10, 2018 at 6:58
  • 3
    @xrd i changed structure a bit, please check the gist here: gist.github.com/kitttn/408aa1264b82ae8ce1c0098a270e4f52 Mar 14, 2018 at 18:26
  • @xrd please note you should enable Dart 2 support in your IDE / analyzer. Or just manually add new in children constructor invocations. Mar 14, 2018 at 18:27
  • @NikitaKraev thank you. How can I make it more performant? If I keep drawing I loose the 60 fps and gets into like 5 fps and becomes very slow. Is there a way to make it more performant. Im creating more like a drawing app. Thank you.
    – user983327
    Apr 15, 2018 at 19:05
  • 1
    I had to switch GestureDetector and CustomPaint, otherwiser the onPanUpdate was not triggered
    – Gpack
    Aug 20, 2019 at 7:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.