mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
web/github_dataviz: Migrate to null safety (#919)
This commit is contained in:
@@ -14,7 +14,9 @@ class LayeredChart extends StatefulWidget {
|
||||
final List<WeekLabel> milestones;
|
||||
final double animationValue;
|
||||
|
||||
LayeredChart(this.dataToPlot, this.milestones, this.animationValue);
|
||||
const LayeredChart(this.dataToPlot, this.milestones, this.animationValue,
|
||||
{Key? key})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
@@ -23,14 +25,14 @@ class LayeredChart extends StatefulWidget {
|
||||
}
|
||||
|
||||
class LayeredChartState extends State<LayeredChart> {
|
||||
List<Path> paths;
|
||||
List<Path> capPaths;
|
||||
List<double> maxValues;
|
||||
double theta;
|
||||
double graphHeight;
|
||||
List<TextPainter> labelPainter;
|
||||
List<TextPainter> milestonePainter;
|
||||
Size lastSize;
|
||||
late List<Path> paths;
|
||||
late List<Path> capPaths;
|
||||
late List<double> maxValues;
|
||||
late double theta;
|
||||
late double graphHeight;
|
||||
late List<TextPainter> labelPainter;
|
||||
late List<TextPainter> milestonePainter;
|
||||
Size? lastSize;
|
||||
|
||||
void buildPaths(
|
||||
Size size,
|
||||
@@ -89,7 +91,7 @@ class LayeredChartState extends State<LayeredChart> {
|
||||
j.toDouble(), 0, (numPoints - 1).toDouble(), 0, (n - 1).toDouble());
|
||||
curve.progressiveGet(cpv);
|
||||
curvePoints.add(MathUtils.map(
|
||||
max(0, cpv.value), 0, maxValues[i].toDouble(), 0, graphHeight));
|
||||
max(0, cpv.value!), 0, maxValues[i].toDouble(), 0, graphHeight));
|
||||
}
|
||||
paths.add(Path());
|
||||
capPaths.add(Path());
|
||||
@@ -136,7 +138,7 @@ class LayeredChartState extends State<LayeredChart> {
|
||||
labelPainter = <TextPainter>[];
|
||||
for (int i = 0; i < dataToPlot.length; i++) {
|
||||
TextSpan span = TextSpan(
|
||||
style: TextStyle(
|
||||
style: const TextStyle(
|
||||
color: Color.fromARGB(255, 255, 255, 255), fontSize: 12),
|
||||
text: dataToPlot[i].label.toUpperCase());
|
||||
TextPainter tp = TextPainter(
|
||||
@@ -149,7 +151,7 @@ class LayeredChartState extends State<LayeredChart> {
|
||||
milestonePainter = <TextPainter>[];
|
||||
for (int i = 0; i < milestones.length; i++) {
|
||||
TextSpan span = TextSpan(
|
||||
style: TextStyle(
|
||||
style: const TextStyle(
|
||||
color: Color.fromARGB(255, 255, 255, 255), fontSize: 10),
|
||||
text: milestones[i].label.toUpperCase());
|
||||
TextPainter tp = TextPainter(
|
||||
@@ -174,15 +176,15 @@ class LayeredChartState extends State<LayeredChart> {
|
||||
}
|
||||
|
||||
class ChartPainter extends CustomPainter {
|
||||
static List<Color> colors = [
|
||||
static List<Color?> colors = [
|
||||
Colors.red[900],
|
||||
Color(0xffc4721a),
|
||||
const Color(0xffc4721a),
|
||||
Colors.lime[900],
|
||||
Colors.green[900],
|
||||
Colors.blue[900],
|
||||
Colors.purple[900],
|
||||
];
|
||||
static List<Color> capColors = [
|
||||
static List<Color?> capColors = [
|
||||
Colors.red[500],
|
||||
Colors.amber[500],
|
||||
Colors.lime[500],
|
||||
@@ -196,17 +198,17 @@ class ChartPainter extends CustomPainter {
|
||||
|
||||
double margin;
|
||||
double graphGap;
|
||||
double capTheta;
|
||||
late double capTheta;
|
||||
double capSize;
|
||||
int numPoints;
|
||||
double amount = 1.0;
|
||||
|
||||
Paint pathPaint;
|
||||
Paint capPaint;
|
||||
Paint textPaint;
|
||||
Paint milestonePaint;
|
||||
Paint linePaint;
|
||||
Paint fillPaint;
|
||||
late Paint pathPaint;
|
||||
late Paint capPaint;
|
||||
late Paint textPaint;
|
||||
late Paint milestonePaint;
|
||||
late Paint linePaint;
|
||||
late Paint fillPaint;
|
||||
|
||||
LayeredChartState state;
|
||||
|
||||
@@ -220,13 +222,13 @@ class ChartPainter extends CustomPainter {
|
||||
this.capSize,
|
||||
this.numPoints,
|
||||
this.amount) {
|
||||
this.capTheta = pi * capDegrees / 180;
|
||||
capTheta = pi * capDegrees / 180;
|
||||
pathPaint = Paint();
|
||||
pathPaint.style = PaintingStyle.fill;
|
||||
capPaint = Paint();
|
||||
capPaint.style = PaintingStyle.fill;
|
||||
textPaint = Paint();
|
||||
textPaint.color = Color(0xFFFFFFFF);
|
||||
textPaint.color = const Color(0xFFFFFFFF);
|
||||
milestonePaint = Paint();
|
||||
milestonePaint.color = Constants.milestoneColor;
|
||||
milestonePaint.style = PaintingStyle.stroke;
|
||||
@@ -236,19 +238,19 @@ class ChartPainter extends CustomPainter {
|
||||
linePaint.strokeWidth = 0.5;
|
||||
fillPaint = Paint();
|
||||
fillPaint.style = PaintingStyle.fill;
|
||||
fillPaint.color = Color(0xFF000000);
|
||||
fillPaint.color = const Color(0xFF000000);
|
||||
}
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
if (dataToPlot.length == 0) {
|
||||
if (dataToPlot.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (state.lastSize == null ||
|
||||
size.width != state.lastSize.width ||
|
||||
size.height != state.lastSize.height) {
|
||||
print("Building paths, lastsize = ${state.lastSize}");
|
||||
size.width != state.lastSize!.width ||
|
||||
size.height != state.lastSize!.height) {
|
||||
print('Building paths, lastsize = ${state.lastSize}');
|
||||
state.buildPaths(size, dataToPlot, milestones, numPoints, graphGap,
|
||||
margin, capTheta, capSize);
|
||||
}
|
||||
@@ -266,7 +268,7 @@ class ChartPainter extends CustomPainter {
|
||||
{
|
||||
for (int i = 0; i < milestones.length; i++) {
|
||||
WeekLabel milestone = milestones[i];
|
||||
double p = (milestone.weekNum.toDouble() / numWeeks) + (1 - amount);
|
||||
double p = (milestone.weekNum!.toDouble() / numWeeks) + (1 - amount);
|
||||
if (p < 1) {
|
||||
double x1 = MathUtils.map(p, 0, 1, startX, endX);
|
||||
double y1 = MathUtils.map(p, 0, 1, startY, endY);
|
||||
@@ -282,7 +284,7 @@ class ChartPainter extends CustomPainter {
|
||||
canvas.translate(textX, textY);
|
||||
canvas.skew(tan(capTheta * 1.0), -tan(state.theta));
|
||||
canvas.translate(-tp.width / 2, 0);
|
||||
tp.paint(canvas, Offset(0, 0));
|
||||
tp.paint(canvas, const Offset(0, 0));
|
||||
canvas.restore();
|
||||
}
|
||||
}
|
||||
@@ -302,11 +304,11 @@ class ChartPainter extends CustomPainter {
|
||||
canvas.skew(0, -tan(state.theta));
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(-1, -1, tp.width + 2, tp.height + 2), fillPaint);
|
||||
tp.paint(canvas, Offset(0, 0));
|
||||
tp.paint(canvas, const Offset(0, 0));
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
linePaint.color = capColors[i];
|
||||
linePaint.color = capColors[i]!;
|
||||
canvas.drawLine(Offset(startX, startY), Offset(endX, endY), linePaint);
|
||||
|
||||
Path clipPath = Path();
|
||||
@@ -317,8 +319,8 @@ class ChartPainter extends CustomPainter {
|
||||
clipPath.close();
|
||||
canvas.clipPath(clipPath);
|
||||
|
||||
pathPaint.color = colors[i];
|
||||
capPaint.color = capColors[i];
|
||||
pathPaint.color = colors[i]!;
|
||||
capPaint.color = capColors[i]!;
|
||||
double offsetX = MathUtils.map(1 - amount, 0, 1, startX, endX);
|
||||
double offsetY = MathUtils.map(1 - amount, 0, 1, startY, endY);
|
||||
canvas.translate(offsetX - startX, offsetY - startY);
|
||||
|
||||
Reference in New Issue
Block a user