I have this code and I don't know how can I save the signature image it in the device any one know how can I do it? Then I need to retrive this picture again
import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_signature_pad/flutter_signature_pad.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ByteData _img = ByteData(0);
@override
Widget build(BuildContext context) {
var sign = Signature(
color: Colors.red,
strokeWidth: 5.0,
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(child: sign),
LimitedBox(
maxHeight: 200.0,
child: Image.memory(_img.buffer.asUint8List())),
Padding(
padding: EdgeInsets.all(25.0),
child:
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
MaterialButton(
color: Colors.green,
onPressed: () async {
//retrieve image data, do whatever you want with it (send to server, save locally...)
var data = await sign
.getData()
.toByteData(format: ui.ImageByteFormat.png);
setState(() {
_img = data;
});
debugPrint("onPressed " +
base64.encode(data.buffer.asUint8List()));
},
child: Text("Save")),
MaterialButton(
color: Colors.grey,
onPressed: () {
sign.clear();
debugPrint("cleared");
},
child: Text("Clear")),
]),
)
],
),
) // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Solution 1: boformer
The Signature
widget in the flutter example simply paints a path on a canvas:
void paint(Canvas canvas, Size size) {
var paint = 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);
}
}
You need to find a way to
- create your own canvas
- draw the path on the canvas
- render the canvas
- export the rendering as png
Here are the concrete steps:
- Create a
ui.PictureRecorder
. - Create a
Canvas
using the recorder and aSize
. - Now draw the path on the canvas.
- End the recording with
recorder.endRecording()
- Get the
ui.Image
object withpicture.toImage
- Get the image bytes in png format with
image.toByteData(format: ui.ImageByteFormat.png)
- Save it to a file, stream it to a server...
The steps are described in my answer here (although the use case is different): Flutter how to save(re-encode) text overlay on video
Solution 2: Aravind Vemula
pngBytes = image.toByteData(format: ui.ImageByteFormat.png)
now, you have to convert pngBytes
to List<int>
bytes.
then using File('$path/filename.png').writeAsBytesSync(pngBytes.buffer.asInt8List())
will save your canvas as png on storage
Here is an example
var pngBytes = await image.toByteData(format: ui.ImageByteFormat.png);
// requesting external storage permission
if(!(await checkPermission())) await requestPermission();
// Use plugin [path_provider] to export image to storage
Directory directory = await getExternalStorageDirectory();
String path = directory.path;
print(path);
// create directory on external storage
await Directory('$path/$directoryName').create(recursive: true);
// write to storage as a filename.png
File('$path/$directoryName/filename.png')
.writeAsBytesSync(pngBytes.buffer.asInt8List());
for complete example visit https://github.com/vemarav/signature