由于我将我的项目迁移到 2.8 版并具有空安全性,因此我遇到了这个错误,我不知道如何解决它;下面我放视频。
我的红色容器尺寸为 1700 x 1200,我将相同的测量值发送到 CustomPainter,但似乎在绘图时,它需要我手机的测量值,因为当我缩放它时(这就是我使用交互式缩放它的原因),它仍然有相同的测量值(我不知道它是什么)。
DrawnShape? currentShape; //My object
return InteractiveViewer(
/// * START
onInteractionStart: (details) {
currentShape = DrawnShape(
pointList: [],
paint: Paint()
..strokeCap = StrokeCap.round
..isAntiAlias = true
..color = Colors.black
..strokeWidth = 3.0
..style = PaintingStyle.stroke,
type: 1,
id: Uuid(),
);
},
/// * UPDATE
onInteractionUpdate: (details) {
currentShape.pointList.add(details.localFocalPoint);
// Some Bloc that returns a List<DrawnShape> in a DrawUpdate state
// and then go to the Class MyCanvas
BlocProvider.of<DrawBloc>(context).add(AddShapeEvent(
currentShape,
));
},
/// * END
onInteractionEnd: (details) {
currentShape = null;
},
constrained: false,
boundaryMargin: EdgeInsets.all(1200 * 0.2);,
maxScale: 5,
minScale: 0.1,
// * STACK
child: Stack(
children: [
SizedBox(
height: 1700,
width: 1200,
child: Container(
decoration: BoxDecoration(
color: Colors.red,
border: new Border.all(
color: Colors.grey, width: 1.5, style: BorderStyle.solid),
),
child: Text(""),
),
);
// * CANVAS TO DRAW ON
IgnorePointer(
ignoring: true,
child: MyCanvas(
height: 1700,
width: 1200,
),
),
],
),
);
class MyCanvas extends StatelessWidget {
final double height;
final double width;
const MyCanvas({
Key? key,
this.height,
this.width,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final canvasSize = Size(width, height);
return ClipRect(
// * CONSUMER
child: BlocConsumer<DrawBloc, DrawBlocState>(
listener: (context, state) {},
builder: (context, state) {
// * DRAWING UPDATED
if (state is DrawUpdate) {
// * CUSTOM PAINT
return CustomPaint(
isComplex: true,
size: canvasSize,
// * DRAWING PAINTER
painter: DrawingPainter(
state.lineList,
),
} else {
return Container();
}
},
),
);
}
}
//===========================================================================
// * DRAWING PAINTER (canvas to paint on)
//
class DrawingPainter extends CustomPainter {
DrawingPainter(
this.shapeList,
);
final List<DrawnShape> shapeList;
@override
void paint(Canvas canvas, Size size) {
canvas.saveLayer(Rect.fromLTWH(0, 0, size.width, size.height), Paint());
for (int i = 0; i < shapeList.length; i++) {
final currentShape = shapeList[i];
switch (currentShape.type) {
// case ActionType.RECTANGLE:
// this._drawRectangle(canvas, currentShape);
// break;
// case ActionType.CIRCLE:
// this._drawCircle(canvas, currentShape);
// break;
// case ActionType.CLOUD:
// this._drawCloud(canvas, currentShape);
// break;
default:
this._drawFromPointList(canvas, currentShape); // THE IMPORTANT
break;
}
}
canvas.restore();
}
@override
bool shouldRepaint(DrawingPainter oldDelegate) => true;
//===========================================================================
// POINT LIST
//
void _drawFromPointList(Canvas canvas, DrawnShape currentShape) {
final Path path = Path();
final pointList = currentShape.pointList;
/// starts at first point
path.moveTo(
pointList[0].dx,
pointList[0].dy,
);
/// goes through every point of the line, (starts at 1)
for (int f = 1; f < pointList.length; f++)
path.lineTo(pointList[f].dx, pointList[f].dy);
/// draw the line
canvas.drawPath(path, currentShape.paint);
}
}
似乎线条在正确的位置上画得很好,但是我想画我手指所在的位置。