Open ali94a opened 3 years ago
Did you find the solution to this problem?
The issue occurred because of Null safety in Flutter. It ensures variables cannot contain null values unless explicitly allowed. It helps prevent common runtime errors caused by null references, also known as null pointer exceptions.
To solve this problem i added Offset.zero instead of null.
onPanEnd: (details) {
setState(() {
drawingPoints.add(DrawingPoint(
Offset.zero,
Paint(),
));
});
},
In the loop now check the value of drawingPoints[i].offset
@override
void paint(Canvas canvas, Size size) {
for (int i = 0; i < drawingPoints.length - 1; i++) {
if (drawingPoints[i].offset != Offset.zero &&
drawingPoints[i + 1].offset != Offset.zero) {
canvas.drawLine(drawingPoints[i].offset, drawingPoints[i + 1].offset,
drawingPoints[i].paint);
}
}
}
This should fix the issue
import 'dart:ui';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: DrawingBoard(), ); } }
class DrawingBoard extends StatefulWidget { @override _DrawingBoardState createState() => _DrawingBoardState(); }
class _DrawingBoardState extends State {
Color selectedColor = Colors.black;
double strokeWidth = 5;
List drawingPoints = [];
List colors = [
Colors.pink,
Colors.red,
Colors.black,
Colors.yellow,
Colors.amberAccent,
Colors.purple,
Colors.green,
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
GestureDetector(
onPanStart: (details) {
setState(() {
drawingPoints.add(
DrawingPoint(
details.localPosition,
Paint()
..color = selectedColor
..isAntiAlias = true
..strokeWidth = strokeWidth
..strokeCap = StrokeCap.round,
),
);
});
},
onPanUpdate: (details) {
setState(() {
drawingPoints.add(
DrawingPoint(
details.localPosition,
Paint()
..color = selectedColor
..isAntiAlias = true
..strokeWidth = strokeWidth
..strokeCap = StrokeCap.round,
),
);
});
},
onPanEnd: (details) {
setState(() {
drawingPoints.add(null);//////////////////////////////here the issue (on null)
});
},
child: CustomPaint(
painter: _DrawingPainter(drawingPoints),
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
),
),
),
Positioned(
top: 40,
right: 30,
child: Row(
children: [
Slider(
min: 0,
max: 40,
value: strokeWidth,
onChanged: (val) => setState(() => strokeWidth = val),
),
ElevatedButton.icon(
onPressed: () => setState(() => drawingPoints = []),
icon: Icon(Icons.clear),
label: Text("Clear Board"),
)
],
),
),
],
),
bottomNavigationBar: BottomAppBar(
child: Container(
color: Colors.grey[200],
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: List.generate(
colors.length,
(index) => _buildColorChose(colors[index]),
),
),
),
),
);
}
Widget _buildColorChose(Color color) { bool isSelected = selectedColor == color; return GestureDetector( onTap: () => setState(() => selectedColor = color), child: Container( height: isSelected ? 47 : 40, width: isSelected ? 47 : 40, decoration: BoxDecoration( color: color, shape: BoxShape.circle, border: isSelected ? Border.all(color: Colors.white, width: 3) : null, ), ), ); } }
class _DrawingPainter extends CustomPainter { final List drawingPoints;
_DrawingPainter(this.drawingPoints);
List offsetsList = [];
@override void paint(Canvas canvas, Size size) { for (int i = 0; i < drawingPoints.length; i++) { if (drawingPoints[i] != null && drawingPoints[i + 1] != null) { canvas.drawLine(drawingPoints[i].offset, drawingPoints[i + 1].offset, drawingPoints[i].paint); } else if (drawingPoints[i] != null && drawingPoints[i + 1] == null) { offsetsList.clear(); offsetsList.add(drawingPoints[i].offset);
}
@override bool shouldRepaint(covariant CustomPainter oldDelegate) => true; }
class DrawingPoint { Offset offset; Paint paint;
DrawingPoint(this.offset, this.paint); }