I am creating a waste management app where user can take image then this Image is shown in a screen where user can draw rectangles around trash. I am using Stack with Custom Paints for doing this. But I am not able to get position of rectangles because this image is then to be sent to a ML model for further evolution. I am using GetX
Here is my code snippet:
body: SizedBox(
width: double.maxFinite,
child: GetBuilder(builder: (DetectTrashController controller) {
return Stack(
children: [
GestureDetector(
onPanStart: (details) {
controller.startDrawing(details.localPosition);
},
onPanUpdate: (details) {
controller.updateDrawing(details.localPosition);
},
onPanEnd: (details) {
controller.endDrawing();
},
child: Stack(
children: [
Image.file(
fit: BoxFit.cover,
height: double.maxFinite,
width: double.maxFinite,
File(controller.imagePath!),
),
SizedBox(
height: controller.image!.height.toDouble(),
width: controller.image!.width.toDouble(),
child: CustomPaint(
key: controller.widgetKey,
painter: RectanglePainter(
controller.rectangles,
controller.currentRect,
controller.image!,
),
size: Size.infinite,
),
),
Padding(
padding: const EdgeInsets.all(8),
child: Align(
alignment: Alignment.topLeft,
child: InkWell(
onTap: () {
controller.toggleDrawing();
},
child: Container(
height: 31.v,
width: 161.h,
color: whiteColor,
child: Obx(
() => Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
Checkbox(
checkColor: whiteColor,
fillColor: MaterialStatePropertyAll(
controller.drawingEnabled.value
? Colors.black
: null),
shape: controller.drawingEnabled.value
? const CircleBorder()
: null,
value: controller.drawingEnabled.value,
onChanged: (value) {
controller.toggleDrawing();
},
),
Text(controller.drawingEnabled.value
? "Cancel Selection"
: "Draw Selection"),
],
),
)),
),
),
),
],
),
),
Padding(
padding: EdgeInsets.only(bottom: 10.v),
child: Align(
alignment: Alignment.bottomCenter,
child: CustomElevetedButton(
onPressed: () {},
backgroundColor: primaryColor,
child: "Update",
size: Size(189.h, 40.v),
),
),
)
],
);
}),
),
CustomPaint Class
class RectanglePainter extends CustomPainter {
final List<Rectangle> rectangles;
final Rect? currentRect;
RectanglePainter(this.rectangles, this.currentRect,);
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.red
..strokeWidth = 2
..style = PaintingStyle.stroke;
// Draw existing rectangles
for (var rectangle in rectangles) {
canvas.drawRect(rectangle.rect, paint);
}
// Draw the current rectangle being dragged
if (currentRect != null) {
canvas.drawRect(currentRect!, paint);
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true; // Repaint every time to reflect changes
}
}
OutPut
Output
New contributor
Muhmmad Nawaz Shah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.