我正在尝试使用CustomPainter类从用户那里获得签名。 当我尝试在新页面上使用它时,它效果很好。问题是,它在AlertDialog上不起作用。
这是我的CustomPainter的代码。
class Signature extends StatefulWidget {
Signature({Key key}) : super(key: key);
@override
_SignatureState createState() => _SignatureState();
}
class _SignatureState extends State<Signature> {
List<Offset> _points = <Offset>[];
Future<ui.Image> get rendered{
ui.PictureRecorder recorder = ui.PictureRecorder();
Canvas canvas = Canvas(recorder);
SignaturePainter painter = SignaturePainter(points: _points);
var size = context.size;
painter.paint(canvas, size);
return recorder.endRecording().toImage(size.width.floor(), size.height.floor());
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Container(
child: GestureDetector(
onPanUpdate: (DragUpdateDetails details) {
setState(() {
RenderBox _object = context.findRenderObject();
Offset _locationPoints = _object.localToGlobal(details.globalPosition);
_points = new List.from(_points)..add(_locationPoints);
});
},
onPanEnd: (DragEndDetails details) {
setState(() {
_points.add(null);
});
},
child: CustomPaint(painter: SignaturePainter(points: _points),
size: Size.infinite),
),
)
);
}
void clearPoints(){
setState(() {
_points.clear();
});
}
}
class SignaturePainter extends CustomPainter {
List<Offset> points = <Offset>[];
SignaturePainter({this.points});
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.square
..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);
}
}
}
@override
bool shouldRepaint(SignaturePainter oldDelegate) {
return oldDelegate.points != points;
}
}
这是调用AlertDialog的支架主体。
body: new Center(
child: new Container(
width: screenWidth(context, ratio:0.589),
height: screenHeight(context, ratio: 0.9),
child: new AlertDialog(
contentPadding: EdgeInsets.only(left: screenWidth(context, ratio: 0.065), right: screenWidth(context, ratio: 0.065)),
content: new Column(
children: <Widget>[
new Padding(padding: EdgeInsets.only(top: screenHeight(context, ratio: 0.028)),),
new Container(
color: Colors.lightGreenAccent,
width: screenWidth(context, ratio: 0.4414),
height: screenHeight(context, ratio: 0.105),
child: Text("General condition part.1 gonna be here"),
),
new Padding(padding: EdgeInsets.only(top: screenHeight(context, ratio: 0.0156)),),
new Container(
color: Colors.lightGreenAccent,
width: screenWidth(context, ratio: 0.4414),
height: screenHeight(context, ratio: 0.105),
child: Text("General condition part.2 gonna be here"),
),
new Padding(padding: EdgeInsets.only(top: screenHeight(context, ratio: 0.0156)),),
new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text("Acknowledged and agreed on: ${DateTime.now().month}, ${DateTime.now().day}, ${DateTime.now().year}"),
new Padding(padding: EdgeInsets.only(top: screenHeight(context, ratio: 0.02))),
new Row(
children: <Widget>[
new Text("Name"),
new Padding(padding: EdgeInsets.only(left: screenWidth(context, ratio: 0.08)), child: Text(globals.name),),
new Padding(padding: EdgeInsets.only(left: screenWidth(context, ratio: 0.05)), child: Text("Date"),),
new Padding(padding: EdgeInsets.only(left: screenWidth(context, ratio: 0.08)), child: Text("${DateTime.now().month}, ${DateTime.now().day}, ${DateTime.now().year}"),),
],
),
new Container(
color: Colors.amberAccent,
height: screenHeight(context, ratio: 0.2),
child: new Signature(key: signatureKey),
)
],
),
new MaterialButton(
minWidth: screenWidth(context, ratio: 0.37),
height: screenHeight(context, ratio: 0.061),
color: Color.fromRGBO(66, 230, 123, 100),
shape: OutlineInputBorder(),
child: Text("Submit", style: TextStyle(color: Colors.white),),
onPressed: (){
setState(() {
signFlag = false;
});
)
],
),
),
),
)
我希望我可以在画布区域上绘制线条,但是当我尝试在该区域上绘制某些东西时,什么也不会发生。 我不能在对话框上使用CustomPainter吗?