Jon*_*ona 2 mobile signature dart flutter
我想创建一个签名区域像这里在移动应用中有镖!
我试图使用CustomPaint类......但它不起作用.
谁能帮我?
Col*_*son 44
您可以使用GestureDetector记录触摸创建签名区域并CustomPaint在屏幕上绘图.以下是一些提示:
RenderBox.globalToLocal的转换DragUpdateDetails提供了由GestureDetector.onPanUpdate成相对坐标GestureDetector.onPanEnd手势处理程序记录笔画之间的中断.List不会自动触发重绘,因为CustomPainter构造函数参数是相同的.您可以通过在List每次提供新点时创建新的触发器来触发重绘.Canvas.drawLine在签名的每个记录点之间绘制一条圆形线.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()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7875 次 |
| 最近记录: |