There is a video about creating a drawing app by flutter (YouTube) which support to draw lines / points when user tapping on screen but I can't find a feature / way to erase what user had drawn like Android native (like here) especially for lines. I can't just overlaying some colour like white on them because I have background image under the gesture detector.
Can anybody give me some advised about it?
My Sample Project:
import 'dart:ui';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@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> {
List<Offset> points = <Offset>[];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: GestureDetector(
onPanUpdate: (DragUpdateDetails details) {
if (details.localPosition.dx < 0) {
return;
}
RenderBox oRenderBox = context.findRenderObject();
Offset oLocationPoints =
oRenderBox.localToGlobal(details.localPosition);
setState(() {
this.points = new List.from(this.points..add(oLocationPoints));
});
},
onPanEnd: (DragEndDetails details) => this.points.add(null),
child: CustomPaint(
painter: SignaturePainter(
points: this.points),
size: Size.infinite)),
),
);
}
}
class SignaturePainter extends CustomPainter {
List<Offset> points = <Offset>[];
SignaturePainter({this.points});
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.red
..strokeCap = StrokeCap.round
..strokeWidth = 3.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);
}
}
}
@override
bool shouldRepaint(SignaturePainter oldDelegate) {
return oldDelegate.points != points;
}
}
Thanks.
Use saveLayer() and restore() with BlendMode.clear.
From BlendMode.clear:
Example: