import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/model/report/report_marking_detail.dart';

class CustomRect extends StatefulWidget {
  ExamOriginPapers examOriginItem;

  CustomRect(this.examOriginItem, {Key? key}) : super(key: key);

  @override
  _CustomRectState createState() => _CustomRectState();
}

class _CustomRectState extends State<CustomRect> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.examOriginItem.width,
      height: widget.examOriginItem.height,
      decoration: BoxDecoration(
          image: DecorationImage(
        image: NetworkImage(widget.examOriginItem.imageUrl),
        fit: BoxFit.cover,
      )),
      child: CustomPaint(
        size:
            Size(widget.examOriginItem.width, widget.examOriginItem.height),
        painter: MyPainter(widget.examOriginItem.question),
        child: Stack(
          children: List.generate(widget.examOriginItem.question.length, (index) {
            var item = widget.examOriginItem.question[index];
            return Positioned(
                left: item.x + item.width/2,
                top: item.y + item.height/2 - 25.r,
                child: Row(
                  children: [
                    Text(item.score.toInt().toString(),style: TextStyle(fontSize: 30.sp,color: Colors.red),),
                  ],
                ));
          }),
        ),
      ),
    );
  }
}

///[定义画笔]
Paint _paint = Paint()
  ..color = Colors.red //画笔颜色
  ..strokeCap = StrokeCap.round //画笔笔触类型
  ..isAntiAlias = true //是否启动抗锯齿
  ..style = PaintingStyle.stroke //绘画风格，默认为填充
  ..strokeWidth = 3.0; //画笔的宽度

class MyPainter extends CustomPainter {
  final List<Question> questions;

  MyPainter(this.questions);


  @override
  void paint(Canvas canvas, Size size) {
    questions.forEach((item) {
      Rect rect = Rect.fromLTWH(item.x,item.y
          ,item.width, item.height);
      //根据上面的矩形,构建一个圆角矩形
      RRect rrect = RRect.fromRectAndRadius(rect, Radius.circular(0.0));

      canvas.drawRRect(rrect, _paint);
    });

    /* Rect rect1 = Rect.fromCenter(center: Offset(140.0, 491.0), width: 91.0,height: 35.0);
    //根据上面的矩形,构建一个圆角矩形
    RRect rrect1 = RRect.fromRectAndRadius(rect1, Radius.circular(0.0));
    canvas.drawRRect(rrect1, _paint);*/
    /* Rect rect2 = Rect.fromLTWH( 209.0, 549.0, 449.0,210.0);
     //根据上面的矩形,构建一个圆角矩形
     RRect rrect2 = RRect.fromRectAndRadius(rect2, Radius.circular(0.0));
     canvas.drawRRect(rrect2, _paint);*/
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {

    return true;
  }
}
