2017-09-15 99 views
-10

我想用移動應用程序中的飛鏢創建像Here這樣的簽名區域!在飛鏢(撲翼)中爲移動應用程序創建`簽名區域'

我試圖使用CustomPaint類...但它不起作用。

任何人都可以幫助我嗎?

+5

我們不鼓勵的帖子,簡單地陳述問題斷章取義,並期望社會來解決它。假設你試圖自己解決它並陷入困境,那麼如果你寫下了你的想法和你無法想象的東西,這可能會有所幫助。添加你的代碼的[mcve],並描述你有什麼問題。 _「但它不起作用」_不是很有幫助。 – Cerbrus

+1

你的程序[不起作用](http://importblogkit.com/2015/07/does-not-work/)? –

+0

刪除選民可以解釋他們的投票嗎?在這裏發佈的答案似乎相當有幫助.... – user000001

回答

14

您可以使用GestureDetector創建一個簽名區域來記錄觸摸,並使用CustomPaint在屏幕上繪製。這裏有幾個技巧:

video

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 GestureDetector(
     onPanUpdate: (DragUpdateDetails details) { 
     setState(() { 
      RenderBox referenceBox = context.findRenderObject(); 
      Offset localPosition = 
      referenceBox.globalToLocal(details.globalPosition); 
      _points = new List.from(_points)..add(localPosition); 
     }); 
     }, 
     onPanEnd: (DragEndDetails details) => _points.add(null), 
     child: new CustomPaint(painter: new SignaturePainter(_points)), 
    ); 
    } 
} 
class DemoApp extends StatelessWidget { 
    Widget build(BuildContext context) => new Scaffold(body: new Signature()); 
} 
void main() => runApp(new MaterialApp(home: new DemoApp())); 
+0

此代碼似乎不再適用於顫動的測試版。我嘗試過仿真器和真實設備(Pixel 2),但沒有畫在屏幕上。 – xrd