flutter 当我隐藏和打开我用CustomPaint绘制的形状时,它会在屏幕左侧绘制一条线,如何防止这种情况?

zd287kbt  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我使用偏移量和坐标。我使用点将它们合并组合并让它们绘制一个形状,当我隐藏这个绘制的形状并再次打开它时,一条线从屏幕左上角出现在我的形状上。如何防止这种情况?
beforeafter的数据库
眼睛按钮功能

InkWell(onTap: () {
  setState(() {
    if (isEyeSelected) {
      selectedEyeIndexes.remove(index);
      showHiddenItems(index);
    } else {
      selectedEyeIndexes.add(index);
      for (int i = 0; i < studentLabelCoordinate[index].length; i++) {
         studentLabelCoordinate[index][i] = Offset.zero;
      }
    }
  });
},

字符串
功能

void showHiddenItems(int index) {
                                        if (imageTypeState?.toLowerCase() ==
                                                'bitewing' ||
                                            imageTypeState?.toLowerCase() ==
                                                'periapical') {
                                          for (var j = 0;
                                              j <
                                                  studentAnswerModel!
                                                      .illnessCoordsColors![
                                                          index]
                                                      .coords!
                                                      .length;
                                              j++) {
                                            studentLabelCoordinate[index].add(Offset(
                                                (studentAnswerModel!
                                                            .illnessCoordsColors![
                                                                index]
                                                            .coords![j][0] *
                                                        width!) *
                                                    widthrate *
                                                    _scale,
                                                (studentAnswerModel!
                                                                .illnessCoordsColors![
                                                                    index]
                                                                .coords![j][1] *
                                                            height!) *
                                                        _scale *
                                                        heightrate -
                                                    (deviceTopPadding!)));
                                          }
                                        } else {
                                          for (var j = 0;
                                              j <
                                                  studentAnswerModel!
                                                      .illnessCoordsColors![
                                                          index]
                                                      .coords!
                                                      .length;
                                              j++) {
                                            studentLabelCoordinate[index].add(Offset(
                                                (studentAnswerModel!
                                                            .illnessCoordsColors![
                                                                index]
                                                            .coords![j][0] *
                                                        width!) *
                                                    _scale,
                                                (studentAnswerModel!
                                                            .illnessCoordsColors![
                                                                index]
                                                            .coords![j][1] *
                                                        height! *
                                                        _scale) -
                                                    (deviceTopPadding! +
                                                        deviceBottomPadding! +
                                                        5.h)));
                                          }
                                        }
                                      }


画家

class PathPainterStudent extends CustomPainter {
  PathPainterStudent({
    required this.coordinats,
    required this.labelColor,
  });

  List<List<Offset>> coordinats;
  List<String> labelColor;

  @override
  void paint(Canvas canvas, Size size) {
    List<Path> paths = [];

    for (var i = 0; i < coordinats.length; i++) {
      Paint paintq = Paint()
        ..color = Color(int.parse(labelColor[i].replaceAll('#', '0xFF')))
        ..style = PaintingStyle.stroke
        ..strokeWidth = 2.0;

      Paint paintq2 = Paint()
        ..color = Color(int.parse(labelColor[i].replaceAll('#', '0xFF')))
            .withOpacity(0.2)
        ..style = PaintingStyle.fill
        ..strokeWidth = 2.0;

      Path path = Path();
      paths.add(path);

      paths[i].addPolygon(coordinats[i], true);
      paths[i].close();
      canvas.drawPath(paths[i], paintq);

      canvas.drawPath(paths[i], paintq2);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}


定制绘画

CustomPaint(
                        painter: PathPainterStudent(
                            coordinats: studentLabelCoordinate,
                            labelColor: studentLabelColor),
                        size: MediaQuery.of(context).size,
                      ),


我把它从列表中删除,然后再把它添加到列表中。我试着在画家中玩,但没有发生。当你从外部给予一个偏移列表时,它总是把它们连接在一起。

kgsdhlau

kgsdhlau1#

默认情况下,Path(0, 0)开始。
在调用Canvas.drawPath()之前,使用Path.moveTo(x, y)path移动到起始点。

Path path = Path();
double dx = coordinates[0].dx;
double dy = coordinates[0].dy;

path.moveTo(dx, dy);

// start drawing path

字符串
希望有帮助!

相关问题