Links

Building Custom Signature Widget with Custom Code

FlutterFlow offers a variety of built-in widgets, but what if you need a widget with functionalities beyond the standard set? That's where FlutterFlow's custom code features come into play. Take the Signature widget as an example: it's useful, but maybe you want to add some extra bells and whistles. This guide walks you through building your own enhanced Signature widget from scratch.
You can either clone the complete project from this link or continue reading for a detailed breakdown.
What we'll cover:
  • Crafting a Signature widget from the ground up using FlutterFlow's Custom Widget feature.
  • The Signature widget needs a SignatureController that will also be used to perform other related actions such as clearing signature, undo etc. We will cover how to create a single instance of the SignatureController so all our custom actions can access it.
  • Creating Custom Actions for tasks like clearing the signature, undoing actions, redoing actions, and converting the signature to Base64 format.

Custom Widget: Signature Widget

Go to the Custom Code tab, create a new Custom Widget, and call it SignatureWidget
Arguments: No additional arguments.
Pubspec Dependencies: Since FlutterFlow already uses signature package from pub.dev, we can use the same package to implement our signature functionality. This also means, the import import 'package:signature/signature.dart'; should already be available.
Body: For the code body, we actually took the example code from the package's example directory. You can directly check this file from Github. We removed some unnecessary code and this is the final code
⬇️
1
// Automatic FlutterFlow imports
2
import '/flutter_flow/flutter_flow_theme.dart';
3
import '/flutter_flow/flutter_flow_util.dart';
4
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
5
import '/custom_code/actions/index.dart'; // Imports custom actions
6
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
7
import 'package:flutter/material.dart';
8
// Begin custom widget code
9
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
10
11
import 'dart:convert';
12
import 'package:signature/signature.dart';
13
import '/custom_code/actions/init_signature_controller.dart';
14
15
class SignatureWidget extends StatefulWidget {
16
const SignatureWidget({
17
Key? key,
18
this.width,
19
this.height,
20
}) : super(key: key);
21
22
final double? width;
23
final double? height;
24
25
@override
26
_SignatureWidgetState createState() => _SignatureWidgetState();
27
}
28
29
class _SignatureWidgetState extends State<SignatureWidget> {
30
late SignatureController _signatureController;
31
32
@override
33
void initState() {
34
super.initState();
35
_signatureController = // TODO
36
_signatureController.addListener(() => print('Value changed'));
37
}
38
39
@override
40
void dispose() {
41
_signatureController.dispose();
42
super.dispose();
43
}
44
45
@override
46
Widget build(BuildContext context) {
47
return ClipRect(
48
child: Signature(
49
controller: _signatureController,
50
backgroundColor: FlutterFlowTheme.of(context).secondaryBackground,
51
height: 120,
52
),
53
);
54
}
55
}
At line: 30, we are declaring a SignatureController that will be used in the Signature Widget at line 49. This controller is crucial: it governs the Signature component, enabling functionalities like clearing the canvas or undoing previous strokes.
At line 35, you'll see a 'TODO' comment. This is where you'll want to create the SignatureController object. Typically, the code for doing so would look something like this:
signatureController = SignatureController(
penStrokeWidth: 5,
penColor: Colors.red,
exportPenColor: Colors.red,
exportBackgroundColor: Colors.white,
);
The SignatureController should be declared only once, as it sets the properties for our Signature widget. If we place it inside this Custom Action for the SignatureWidget, we won't be able to access it from other parts of the application.
So let's fix this!

Custom Action: initSignatureController

Let's create a new action called initSignatureController and initialize our SignatureController here. We will be creating a Singleton* class here which means that only one instance of the controller will be available in the whole app and no duplicate controllers will be created.
Arguments: No additional arguments.
Pubspec Dependencies: Not required, signature package is natively available.
The Singleton class for SignatureController creation will look like this:
1
class SignatureControllerSingleton {
2
static final SignatureControllerSingleton _singleton =
3
SignatureControllerSingleton._internal();
4
5
factory SignatureControllerSingleton() {
6
return _singleton;
7
}
8
9
SignatureControllerSingleton._internal();
10
11
SignatureController? signatureController;
12
13
init() {
14
signatureController = SignatureController(
15
penStrokeWidth: 5,
16
penColor: Colors.red,
17
exportPenColor: Colors.red,
18
exportBackgroundColor: Colors.white,
19
);
20
}
21
}
This will be placed below the initSignatureController function (after the closing parenthesis)
To personalize your Signature widget, you can adjust settings like penStrokeWidth and exportPenColor. If you're interested in making further changes, the official documentation on pub.dev provides a complete list of modifiable properties.
This init() method will be called from our main initSignatureController function.
Future initSignatureController() async {
SignatureControllerSingleton().init();
}
The whole code should look like this:
1
// Automatic FlutterFlow imports
2
import '/flutter_flow/flutter_flow_theme.dart';
3
import '/flutter_flow/flutter_flow_util.dart';
4
import '/custom_code/actions/index.dart'; // Imports other custom actions
5
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
6
import 'package:flutter/material.dart';
7
// Begin custom action code
8
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
9
10
import 'package:signature/signature.dart';
11
12
Future initSignatureController() async {
13
SignatureControllerSingleton().init();
14
}
15
16
class SignatureControllerSingleton {
17
static final SignatureControllerSingleton _singleton =
18
SignatureControllerSingleton._internal();
19
20
factory SignatureControllerSingleton() {
21
return _singleton;
22
}
23
24
SignatureControllerSingleton._internal();
25
26
SignatureController? signatureController;
27
28
init() {
29
signatureController = SignatureController(
30
penStrokeWidth: 5,
31
penColor: Colors.red,
32
exportPenColor: Colors.red,
33
exportBackgroundColor: Colors.white,
34
);
35
}
36
}
This custom action needs to be invoked directly from the main.dart file. We do this to ensure all initializations such as our custom Signature Controller, take place right when the application starts up.
Navigate to "Custom Files" and open up main.dart. On the right-hand side, you'll find a section called "Final Actions." Go ahead and click on the "+" icon there. From the options that appear, select "initSignatureController." Doing so will automatically update the code in main.dart for you.
Now you can update your SignatureWidget and update at line 35 where the TODO comment was created:
_signatureController = SignatureControllerSingleton().signatureController!;
Now, using this approach, you can easily tap into the same controller from other custom actions to carry out a variety of tasks.
Make sure to Compile Code at this point to remove all compilation errors

Custom Action: clearSignature

Arguments: No additional arguments.
Pubspec Dependencies: Not required.
To avoid compilation errors related to the Singleton class we've created, make sure to include a specific import in your files where you're accessing the SignatureControllerSingleton class. (So any file that requires the SignatureController singleton instance) This will ensure that the class is recognized and accessible.
import '/custom_code/actions/init_signature_controller.dart';
The custom action body is pretty simple now and the complete code looks like this:
// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import '/custom_code/actions/init_signature_controller.dart';
Future clearSignature() async {
SignatureControllerSingleton().signatureController!.clear();
}
With this custom action in place, you're now free to control the Signature widget from anywhere in your app using the Action Flow Editor.
Want to extend the functionality for 'undo' and 'redo'? Just create two more custom actions and adjust their code accordingly.
undoSignature:
Future undoSignature() async {
SignatureControllerSingleton().signatureController!.undo();
}
redoSignature:
Future redoSignature() async {
SignatureControllerSingleton().signatureController!.redo();
}

Custom Action: convertToBase64

Often, you'll want to save this signature in a database or display it on another screen as an image. To do so, converting it to Base64 format will be key.
Here's how the code for that function would look:
Return Value: String (Nullable)
Arguments: No additional arguments.
Pubspec Dependencies: Not required.
1
// Automatic FlutterFlow imports
2
import '/flutter_flow/flutter_flow_theme.dart';
3
import '/flutter_flow/flutter_flow_util.dart';
4
import '/custom_code/actions/index.dart'; // Imports other custom actions
5
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
6
import 'package:flutter/material.dart';
7
// Begin custom action code
8
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
9
10
import '/custom_code/actions/init_signature_controller.dart';
11
import 'dart:convert';
12
import 'dart:typed_data';
13
14
Future<String?> convertToBase64() async {
15
final Uint8List? uint8list =
16
await SignatureControllerSingleton().signatureController!.toPngBytes();
17
String? imageEncoded = uint8list != null ? base64.encode(uint8list) : null;
18
return imageEncoded;
19
}
And that's a wrap! Feel free to use this same logic not only for other custom widgets that need controllers, but also whenever you want to control your widget via different custom actions.
As mentioned earlier, feel free to explore this public project to see everything in action for yourself.

Developer Terms Singleton* It's a special kind of class in programming where only one instance (or copy) of the class can exist at any time.