ı create canvas for users can draw.And ı am trying to do use in that canvas another screen. I add button for clear. But ı cant access the clear method,
when I looked at the previous opened questions on stackoverflow, I saw that it is used in InheritedWidget. at first I used an object from my canvas class, but I couldn’t see the method.
import 'dart:ui';
import 'package:flutter/material.dart';
import '../../../core/util/app_sizes.dart';
class ThetaCanvas extends StatefulWidget {
const ThetaCanvas({Key? key}) : super(key: key);
@override
State<ThetaCanvas> createState() => _ThetaCanvasState();
}
class _ThetaCanvasState extends State<ThetaCanvas> {
List<DrawingArea?> points = [];
late double opacity;
@override
void initState() {
super.initState();
opacity = 1.0;
}
clearThetaCanvas() {
setState(() {
points.clear();
});
}
@override
Widget build(BuildContext context) {
return ClipRect(
child: Container(
height: AppSizes.screenHeight / 4,
width: AppSizes.screenWidth,
decoration: BoxDecoration(color: Colors.white70, borderRadius: BorderRadius.circular(25)),
child: GestureDetector(
onPanDown: (details) {
setState(() {
points.add(DrawingArea(
point: details.localPosition,
areaPaint: Paint()
..color = Colors.black.withOpacity(opacity)
..strokeCap = StrokeCap.round
..isAntiAlias = true
..strokeWidth = 2.0,
));
});
},
onPanUpdate: (details) {
setState(() {
points.add(DrawingArea(
point: details.localPosition,
areaPaint: Paint()
..color = Colors.black.withOpacity(opacity)
..strokeCap = StrokeCap.round
..isAntiAlias = true
..strokeWidth = 2.0,
));
});
},
onPanEnd: (details) {
points.add(null);
},
child: CustomPaint(
size: Size.infinite,
painter: DrawingPainter(pointsList: points),
),
),
),
);
}
}
class DrawingPainter extends CustomPainter {
List<DrawingArea?> pointsList;
DrawingPainter({required this.pointsList});
@override
void paint(Canvas canvas, Size size) {
for (int i = 0; i < pointsList.length - 1; i++) {
if (pointsList[i] != null && pointsList[i + 1] != null) {
canvas.drawLine(
pointsList[i]!.point,
pointsList[i + 1]!.point,
pointsList[i]!.areaPaint,
);
} else if (pointsList[i] != null && pointsList[i + 1] == null) {
canvas.drawPoints(PointMode.points, [pointsList[i]!.point], pointsList[i]!.areaPaint);
}
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
class DrawingArea {
Offset point;
Paint areaPaint;
DrawingArea({required this.point, required this.areaPaint});
}
and here is my button
class NextAndClearButton extends StatelessWidget {
const NextAndClearButton({super.key});
@override
Widget build(BuildContext context) {
final ThetaCanvas thetaCanvas = ThetaCanvas();
return TextButton(
onPressed: () {
},
style: ButtonStyle(
splashFactory: InkRipple.splashFactory,
overlayColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) {
return Colors.white
.withOpacity(0.4);
}
return Colors.transparent;
},
),
),
child: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(25),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'NEXT',
style: Theme.of(context).textTheme.displaySmall?.copyWith(color: Colors.white),
),
const Icon(
Icons.chevron_right_outlined,
color: Colors.white,
size: 40,
),
],
),
),
);
}
}
when ı write the thetaCanvas.clearThetaCanvas its says
The method ‘clearThetaCanvas’ isn’t defined for the type ‘ThetaCanvas’.
Try correcting the name to the name of an existing method, or defining a method named ‘clearThetaCanvas’