Skip to content

Commit 83d3ea9

Browse files
web/github_dataviz: Migrate to null safety (#919)
1 parent 8932e60 commit 83d3ea9

12 files changed

+185
-146
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
linter:
4+
rules:
5+
avoid_print: false
6+
prefer_single_quotes: true

web/github_dataviz/lib/catmull.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:github_dataviz/mathutils.dart';
22

33
class ControlPointAndValue {
4-
int point;
5-
double value;
4+
late int point;
5+
double? value;
66

77
ControlPointAndValue() {
88
value = 0;
@@ -37,9 +37,9 @@ class CatmullInterpolator implements Interpolator {
3737
}
3838

3939
ControlPointAndValue progressiveGet(ControlPointAndValue cpv) {
40-
double v = cpv.value;
40+
double? v = cpv.value;
4141
for (int i = cpv.point; i < controlPoints.length - 1; i++) {
42-
if (controlPoints[i].x >= v) {
42+
if (controlPoints[i].x >= v!) {
4343
double t = (v - controlPoints[i - 1].x) /
4444
(controlPoints[i].x - controlPoints[i - 1].x);
4545
double p0 = controlPoints[i - 2].y;

web/github_dataviz/lib/constants.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
22
import 'dart:ui';
33

44
class Constants {
5-
static final Color backgroundColor = const Color(0xFF000020);
6-
static final Color timelineLineColor = Color(0x60FFFFFF);
7-
static final Color milestoneColor = Color(0x40FFFFFF);
8-
static final Color milestoneTimelineColor = Colors.white;
5+
static const Color backgroundColor = Color(0xFF000020);
6+
static const Color timelineLineColor = Color(0x60FFFFFF);
7+
static const Color milestoneColor = Color(0x40FFFFFF);
8+
static const Color milestoneTimelineColor = Colors.white;
99
}

web/github_dataviz/lib/data/contribution_data.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ContributionData {
88

99
static ContributionData fromJson(Map<String, dynamic> jsonMap) {
1010
ContributionData data = ContributionData(
11-
jsonMap["w"], jsonMap["a"], jsonMap["d"], jsonMap["c"]);
11+
jsonMap['w'], jsonMap['a'], jsonMap['d'], jsonMap['c']);
1212
return data;
1313
}
1414
}

web/github_dataviz/lib/data/user.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class User {
66
User(this.id, this.username, this.avatarUrl);
77

88
static User fromJson(Map<String, dynamic> jsonMap) {
9-
User user = User(jsonMap["id"], jsonMap["login"], jsonMap["avatar_url"]);
9+
User user = User(jsonMap['id'], jsonMap['login'], jsonMap['avatar_url']);
1010
return user;
1111
}
1212
}

web/github_dataviz/lib/data/user_contribution.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class UserContribution {
88
UserContribution(this.user, this.contributions);
99

1010
static UserContribution fromJson(Map<String, dynamic> jsonMap) {
11-
List<ContributionData> contributionList = (jsonMap["weeks"] as List)
11+
List<ContributionData> contributionList = (jsonMap['weeks'] as List)
1212
.map((e) => ContributionData.fromJson(e))
1313
.toList();
1414
var userContribution =
15-
UserContribution(User.fromJson(jsonMap["author"]), contributionList);
15+
UserContribution(User.fromJson(jsonMap['author']), contributionList);
1616
return userContribution;
1717
}
1818
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import 'package:intl/intl.dart';
22

33
class WeekLabel {
4-
int weekNum;
4+
int? weekNum;
55
String label;
66

77
WeekLabel(this.weekNum, this.label);
88

9-
WeekLabel.forDate(DateTime date, String label) {
10-
this.label = label;
9+
WeekLabel.forDate(DateTime date, this.label) {
1110
int year = getYear(date);
1211
int weekOfYearNum = getWeekNumber(date);
13-
this.weekNum = 9 + ((year - 2015) * 52) + weekOfYearNum;
12+
weekNum = 9 + ((year - 2015) * 52) + weekOfYearNum;
1413
}
1514

1615
int getYear(DateTime date) {
17-
return int.parse(DateFormat("y").format(date));
16+
return int.parse(DateFormat('y').format(date));
1817
}
1918

2019
int getWeekNumber(DateTime date) {
21-
int dayOfYear = int.parse(DateFormat("D").format(date));
20+
int dayOfYear = int.parse(DateFormat('D').format(date));
2221
return ((dayOfYear - date.weekday + 10) / 7).floor();
2322
}
2423
}

web/github_dataviz/lib/layered_chart.dart

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class LayeredChart extends StatefulWidget {
1414
final List<WeekLabel> milestones;
1515
final double animationValue;
1616

17-
LayeredChart(this.dataToPlot, this.milestones, this.animationValue);
17+
const LayeredChart(this.dataToPlot, this.milestones, this.animationValue,
18+
{Key? key})
19+
: super(key: key);
1820

1921
@override
2022
State<StatefulWidget> createState() {
@@ -23,14 +25,14 @@ class LayeredChart extends StatefulWidget {
2325
}
2426

2527
class LayeredChartState extends State<LayeredChart> {
26-
List<Path> paths;
27-
List<Path> capPaths;
28-
List<double> maxValues;
29-
double theta;
30-
double graphHeight;
31-
List<TextPainter> labelPainter;
32-
List<TextPainter> milestonePainter;
33-
Size lastSize;
28+
late List<Path> paths;
29+
late List<Path> capPaths;
30+
late List<double> maxValues;
31+
late double theta;
32+
late double graphHeight;
33+
late List<TextPainter> labelPainter;
34+
late List<TextPainter> milestonePainter;
35+
Size? lastSize;
3436

3537
void buildPaths(
3638
Size size,
@@ -89,7 +91,7 @@ class LayeredChartState extends State<LayeredChart> {
8991
j.toDouble(), 0, (numPoints - 1).toDouble(), 0, (n - 1).toDouble());
9092
curve.progressiveGet(cpv);
9193
curvePoints.add(MathUtils.map(
92-
max(0, cpv.value), 0, maxValues[i].toDouble(), 0, graphHeight));
94+
max(0, cpv.value!), 0, maxValues[i].toDouble(), 0, graphHeight));
9395
}
9496
paths.add(Path());
9597
capPaths.add(Path());
@@ -136,7 +138,7 @@ class LayeredChartState extends State<LayeredChart> {
136138
labelPainter = <TextPainter>[];
137139
for (int i = 0; i < dataToPlot.length; i++) {
138140
TextSpan span = TextSpan(
139-
style: TextStyle(
141+
style: const TextStyle(
140142
color: Color.fromARGB(255, 255, 255, 255), fontSize: 12),
141143
text: dataToPlot[i].label.toUpperCase());
142144
TextPainter tp = TextPainter(
@@ -149,7 +151,7 @@ class LayeredChartState extends State<LayeredChart> {
149151
milestonePainter = <TextPainter>[];
150152
for (int i = 0; i < milestones.length; i++) {
151153
TextSpan span = TextSpan(
152-
style: TextStyle(
154+
style: const TextStyle(
153155
color: Color.fromARGB(255, 255, 255, 255), fontSize: 10),
154156
text: milestones[i].label.toUpperCase());
155157
TextPainter tp = TextPainter(
@@ -174,15 +176,15 @@ class LayeredChartState extends State<LayeredChart> {
174176
}
175177

176178
class ChartPainter extends CustomPainter {
177-
static List<Color> colors = [
179+
static List<Color?> colors = [
178180
Colors.red[900],
179-
Color(0xffc4721a),
181+
const Color(0xffc4721a),
180182
Colors.lime[900],
181183
Colors.green[900],
182184
Colors.blue[900],
183185
Colors.purple[900],
184186
];
185-
static List<Color> capColors = [
187+
static List<Color?> capColors = [
186188
Colors.red[500],
187189
Colors.amber[500],
188190
Colors.lime[500],
@@ -196,17 +198,17 @@ class ChartPainter extends CustomPainter {
196198

197199
double margin;
198200
double graphGap;
199-
double capTheta;
201+
late double capTheta;
200202
double capSize;
201203
int numPoints;
202204
double amount = 1.0;
203205

204-
Paint pathPaint;
205-
Paint capPaint;
206-
Paint textPaint;
207-
Paint milestonePaint;
208-
Paint linePaint;
209-
Paint fillPaint;
206+
late Paint pathPaint;
207+
late Paint capPaint;
208+
late Paint textPaint;
209+
late Paint milestonePaint;
210+
late Paint linePaint;
211+
late Paint fillPaint;
210212

211213
LayeredChartState state;
212214

@@ -220,13 +222,13 @@ class ChartPainter extends CustomPainter {
220222
this.capSize,
221223
this.numPoints,
222224
this.amount) {
223-
this.capTheta = pi * capDegrees / 180;
225+
capTheta = pi * capDegrees / 180;
224226
pathPaint = Paint();
225227
pathPaint.style = PaintingStyle.fill;
226228
capPaint = Paint();
227229
capPaint.style = PaintingStyle.fill;
228230
textPaint = Paint();
229-
textPaint.color = Color(0xFFFFFFFF);
231+
textPaint.color = const Color(0xFFFFFFFF);
230232
milestonePaint = Paint();
231233
milestonePaint.color = Constants.milestoneColor;
232234
milestonePaint.style = PaintingStyle.stroke;
@@ -236,19 +238,19 @@ class ChartPainter extends CustomPainter {
236238
linePaint.strokeWidth = 0.5;
237239
fillPaint = Paint();
238240
fillPaint.style = PaintingStyle.fill;
239-
fillPaint.color = Color(0xFF000000);
241+
fillPaint.color = const Color(0xFF000000);
240242
}
241243

242244
@override
243245
void paint(Canvas canvas, Size size) {
244-
if (dataToPlot.length == 0) {
246+
if (dataToPlot.isEmpty) {
245247
return;
246248
}
247249

248250
if (state.lastSize == null ||
249-
size.width != state.lastSize.width ||
250-
size.height != state.lastSize.height) {
251-
print("Building paths, lastsize = ${state.lastSize}");
251+
size.width != state.lastSize!.width ||
252+
size.height != state.lastSize!.height) {
253+
print('Building paths, lastsize = ${state.lastSize}');
252254
state.buildPaths(size, dataToPlot, milestones, numPoints, graphGap,
253255
margin, capTheta, capSize);
254256
}
@@ -266,7 +268,7 @@ class ChartPainter extends CustomPainter {
266268
{
267269
for (int i = 0; i < milestones.length; i++) {
268270
WeekLabel milestone = milestones[i];
269-
double p = (milestone.weekNum.toDouble() / numWeeks) + (1 - amount);
271+
double p = (milestone.weekNum!.toDouble() / numWeeks) + (1 - amount);
270272
if (p < 1) {
271273
double x1 = MathUtils.map(p, 0, 1, startX, endX);
272274
double y1 = MathUtils.map(p, 0, 1, startY, endY);
@@ -282,7 +284,7 @@ class ChartPainter extends CustomPainter {
282284
canvas.translate(textX, textY);
283285
canvas.skew(tan(capTheta * 1.0), -tan(state.theta));
284286
canvas.translate(-tp.width / 2, 0);
285-
tp.paint(canvas, Offset(0, 0));
287+
tp.paint(canvas, const Offset(0, 0));
286288
canvas.restore();
287289
}
288290
}
@@ -302,11 +304,11 @@ class ChartPainter extends CustomPainter {
302304
canvas.skew(0, -tan(state.theta));
303305
canvas.drawRect(
304306
Rect.fromLTWH(-1, -1, tp.width + 2, tp.height + 2), fillPaint);
305-
tp.paint(canvas, Offset(0, 0));
307+
tp.paint(canvas, const Offset(0, 0));
306308
canvas.restore();
307309
}
308310

309-
linePaint.color = capColors[i];
311+
linePaint.color = capColors[i]!;
310312
canvas.drawLine(Offset(startX, startY), Offset(endX, endY), linePaint);
311313

312314
Path clipPath = Path();
@@ -317,8 +319,8 @@ class ChartPainter extends CustomPainter {
317319
clipPath.close();
318320
canvas.clipPath(clipPath);
319321

320-
pathPaint.color = colors[i];
321-
capPaint.color = capColors[i];
322+
pathPaint.color = colors[i]!;
323+
capPaint.color = capColors[i]!;
322324
double offsetX = MathUtils.map(1 - amount, 0, 1, startX, endX);
323325
double offsetY = MathUtils.map(1 - amount, 0, 1, startY, endY);
324326
canvas.translate(offsetX - startX, offsetY - startY);

0 commit comments

Comments
 (0)