signature Flutter Plugin

repository·master·Indexed 42 days ago

https://github.com/4q-s-r-o/signature

A performance-optimized Flutter plugin for capturing handwritten signatures. It provides a Signature widget and a SignatureController to manage pen styles, canvas boundaries, and state. Key features include exporting signatures to PNG, SVG, or raw XML, as well as built-in undo/redo functionality and the ability to restore signatures using saved point data.

Tokens
2.6K
Snippets
10
Records
13
Agent score
51%

What's inside signature

  1. Customize the iOS launch screen assets

    master

    You can customize the app's launch screen by replacing the existing image files in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Alternatively, you can manage these assets using Xcode:

    1. Open the iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, select Runner/Assets.xcassets.
    3. Locate the LaunchImage asset set and drag and drop your desired images into it.
    open ios/Runner.xcworkspace
  2. Migration guide for Signature widget layout (v5.0.0+)

    master

    In version 5.0.0 and later, the Signature widget no longer automatically wraps itself in an Expanded widget when dimensions are not specified. This change prevents Incorrect use of ParentDataWidget errors.

    How to handle layout changes:

    • If you provide width and height: No changes are required.
    • If you are using the widget inside a Row, Column, or Flex without dimensions: You must now manually wrap the Signature widget in an Expanded widget.
    • If you are using the widget without dimensions outside of Flex widgets: No changes are required.
  3. Understand the Point and PointType abstractions

    master

    A signature is composed of a list of Point objects. Each Point represents a specific interaction on the canvas.

    • Point: Contains the offset (2D coordinates), the pressure applied (if supported), and the type of movement.
    • PointType: An enum defining how the point was created:
      • PointType.tap: A single touch at a specific location (used for start/end of strokes).
      • PointType.move: A finger moving across the display.

    You can access the raw list of points via controller.points or controller.value.

  4. How Signature handles undo and redo

    master

    The SignatureController manages an internal undo/redo system based on discrete actions (strokes).

    1. Capturing State: Every time a user finishes a stroke (onPointerUp or onPointerCancel), the controller calls pushCurrentStateToUndoStack(). This saves the current list of points into _latestActions.
    2. Undo: Calling undo() removes the last action from _latestActions and moves it to _revertedActions. The canvas is then updated to the state before that action.
    3. Redo: Calling redo() takes the last action from _revertedActions, moves it back to _latestActions, and applies it to the canvas.
    4. Clearing: Calling clear() wipes all points and resets both the undo and redo stacks.
  5. Use the Signature widget

    master

    The Signature widget provides a canvas for users to draw signatures. It requires a SignatureController to manage the drawing state and interaction. You can optionally specify dimensions (width, height), a backgroundColor, and a placeholder widget to show when the canvas is empty.

    By default, the widget expands to fill its parent container unless width or height are provided.

    ```dart
    final SignatureController controller = SignatureController(
      penColor: Colors.black,
      penStrokeWidth: 3.0,
    );
    
    // In your widget tree:
    Signature(
      controller: controller,
      backgroundColor: Colors.grey,
      placeholder: Text('Please sign here'),
    )```
  6. Restore a previous signature using points

    master

    You can save a signature's state by exporting its points and later re-initializing a new SignatureController using those points. This allows you to load previously saved signatures.

    // 1. Export points from an existing controller
    var exportedPoints = _controller.points;
    
    // 2. Initialize a new controller with those points to restore the state
    final SignatureController _restoredController = SignatureController(points: exportedPoints);
  7. Display the Signature widget

    master

    The Signature widget is the UI component that renders the canvas. It requires a SignatureController and can be configured with specific dimensions and a background color.

    var _signatureCanvas = Signature(
      controller: _controller,
      width: 300,
      height: 300,
      backgroundColor: Colors.lightBlueAccent,
    );
  8. Use SignatureController to manage signature state

    master

    The SignatureController is the primary way to interact with the signature canvas. It manages signature points, stroke width, pen color, and export settings.

    Key capabilities:

    • Configuration: Set penStrokeWidth, penColor, and exportBackgroundColor during initialization.
    • State Checking: Use isNotEmpty or isEmpty to check if the user has drawn anything.
    • Canvas Management: Use clear() to wipe the canvas and dispose() to free resources.
    • Data Export: Use toPngBytes() to export the signature as a PNG image, or access points to get the 2D points representing the drawing.
    import 'package:signature/signature.dart';
    
    // Initialize the controller
    final SignatureController _controller = SignatureController(
        penStrokeWidth: 5,
        penColor: Colors.red,
        exportBackgroundColor: Colors.blue,
    );
    
    // Check if signature exists
    bool hasSignature = _controller.isNotEmpty;
    
    // Clear the canvas
    _controller.clear();
    
    // Export as PNG
    Uint8List? pngBytes = await _controller.toPngBytes();
    
    // Get raw points for saving/restoring
    var exportedPoints = _controller.points;
    
    // Cleanup
    @override
    void dispose() {
      _controller.dispose();
      super.dispose();
    }
  9. Configure SignatureController pen styles

    master

    The SignatureController allows you to customize the visual appearance of the signature lines using standard Flutter Paint properties:

    • penColor: The color of the signature line.
    • penStrokeWidth: The thickness of the line.
    • strokeCap: The shape of the line ends (e.g., StrokeCap.butt, StrokeCap.round).
    • strokeJoin: The shape of the line joins (e.g., StrokeJoin.miter, StrokeJoin.round).
    final controller = SignatureController(
      penColor: Colors.red,
      penStrokeWidth: 5.0,
      strokeCap: StrokeCap.round,
      strokeJoin: StrokeJoin.round,
    );
  10. Manage signature state with SignatureController

    master

    The SignatureController is the primary interface for interacting with the signature canvas. It extends ValueNotifier<List<Point>>, allowing you to listen to changes in the signature data.

    Key capabilities include:

    • Drawing Configuration: Set penColor, penStrokeWidth, strokeCap, and strokeJoin.
    • Undo/Redo: Use undo() and redo() to revert or re-apply recent strokes. Note that undo() works by popping from an internal undo stack created via pushCurrentStateToUndoStack() (which is called automatically on onPointerUp or onPointerCancel).
    • Callbacks: Listen to drawing lifecycle events via onDrawStart, onDrawMove, and onDrawEnd.
    • Control: Use the disabled property to prevent user input on the canvas.
    final controller = SignatureController(
      onDrawStart: () => print('Started drawing'),
      onDrawEnd: () => print('Finished drawing'),
    );
    
    // To clear the canvas:
    controller.clear();
    
    // To undo the last action:
    if (controller.canUndo) {
      controller.undo();
    }
  11. Export signature to SVG

    master

    The controller provides two ways to export the signature as SVG:

    1. toRawSVG({int? width, int? height, double minDistanceBetweenPoints = 3}): Returns a String? containing the raw <svg> XML. This is useful for server-side processing or custom rendering.
    2. toSVG({int? width, int? height, double minDistanceBetweenPoints = 3}): Returns an SvgPicture? (from the flutter_svg package) ready to be displayed in a Flutter widget tree.

    The minDistanceBetweenPoints parameter helps reduce the SVG file size by optimizing the number of points included in the path.

    // Displaying the signature as an SVG widget
    SvgPicture? svgWidget = controller.toSVG(width: 300, height: 100);
    
    if (svgWidget != null) {
      return svgWidget;
    }