1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 14:28:51 +00:00

web/github_dataviz: Migrate to null safety (#919)

This commit is contained in:
Brett Morgan
2021-10-08 19:43:25 +11:00
committed by GitHub
parent 8932e60976
commit 83d3ea99f0
12 changed files with 183 additions and 144 deletions

View File

@@ -14,17 +14,19 @@ class Timeline extends StatefulWidget {
final double animationValue;
final List<WeekLabel> weekLabels;
final MouseDownCallback mouseDownCallback;
final MouseMoveCallback mouseMoveCallback;
final MouseUpCallback mouseUpCallback;
final MouseDownCallback? mouseDownCallback;
final MouseMoveCallback? mouseMoveCallback;
final MouseUpCallback? mouseUpCallback;
Timeline(
{@required this.numWeeks,
@required this.animationValue,
@required this.weekLabels,
const Timeline(
{required this.numWeeks,
required this.animationValue,
required this.weekLabels,
this.mouseDownCallback,
this.mouseMoveCallback,
this.mouseUpCallback});
this.mouseUpCallback,
Key? key})
: super(key: key);
@override
State<StatefulWidget> createState() {
@@ -39,17 +41,17 @@ class TimelineState extends State<Timeline> {
void initState() {
super.initState();
for (int year = 2015; year < 2020; year++) {
String yearLabel = "$year";
String yearLabel = '$year';
labelPainters[yearLabel] =
_makeTextPainter(Constants.timelineLineColor, yearLabel);
}
widget.weekLabels.forEach((WeekLabel weekLabel) {
for (var weekLabel in widget.weekLabels) {
labelPainters[weekLabel.label] =
_makeTextPainter(Constants.milestoneTimelineColor, weekLabel.label);
labelPainters[weekLabel.label + "_red"] =
labelPainters[weekLabel.label + '_red'] =
_makeTextPainter(Colors.redAccent, weekLabel.label);
});
}
}
@override
@@ -57,19 +59,22 @@ class TimelineState extends State<Timeline> {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onHorizontalDragDown: (DragDownDetails details) {
if (widget.mouseDownCallback != null) {
widget.mouseDownCallback(
final mouseDownCallback = widget.mouseDownCallback;
if (mouseDownCallback != null) {
mouseDownCallback(
_getClampedXFractionLocalCoords(context, details.globalPosition));
}
},
onHorizontalDragEnd: (DragEndDetails details) {
if (widget.mouseUpCallback != null) {
widget.mouseUpCallback();
final mouseUpCallback = widget.mouseUpCallback;
if (mouseUpCallback != null) {
mouseUpCallback();
}
},
onHorizontalDragUpdate: (DragUpdateDetails details) {
if (widget.mouseMoveCallback != null) {
widget.mouseMoveCallback(
final mouseMoveCallback = widget.mouseMoveCallback;
if (mouseMoveCallback != null) {
mouseMoveCallback(
_getClampedXFractionLocalCoords(context, details.globalPosition));
}
},
@@ -95,17 +100,17 @@ class TimelineState extends State<Timeline> {
double _getClampedXFractionLocalCoords(
BuildContext context, Offset globalOffset) {
final RenderBox box = context.findRenderObject();
final RenderBox box = context.findRenderObject() as RenderBox;
final Offset localOffset = box.globalToLocal(globalOffset);
return MathUtils.clamp(localOffset.dx / context.size.width, 0, 1);
return MathUtils.clamp(localOffset.dx / context.size!.width, 0, 1);
}
}
class TimelinePainter extends CustomPainter {
TimelineState state;
Paint mainLinePaint;
Paint milestoneLinePaint;
late Paint mainLinePaint;
late Paint milestoneLinePaint;
Color lineColor = Colors.white;
@@ -164,7 +169,7 @@ class TimelinePainter extends CustomPainter {
var mappedValue =
MathUtils.clampedMap(currTimeXDiff, 0, 0.025, 0, 1);
var lerpedColor = Color.lerp(Constants.milestoneTimelineColor,
Constants.timelineLineColor, mappedValue);
Constants.timelineLineColor, mappedValue)!;
mainLinePaint.color = lerpedColor;
} else {
mainLinePaint.color = Constants.timelineLineColor;
@@ -174,8 +179,8 @@ class TimelinePainter extends CustomPainter {
}
if (isYear) {
var yearLabel = "$yearNumber";
state.labelPainters[yearLabel]
var yearLabel = '$yearNumber';
state.labelPainters[yearLabel]!
.paint(canvas, Offset(currX, size.height - labelHeight));
yearNumber++;
}
@@ -185,17 +190,17 @@ class TimelinePainter extends CustomPainter {
{
for (int i = 0; i < weekLabels.length; i++) {
WeekLabel weekLabel = weekLabels[i];
double currX = (weekLabel.weekNum / numWeeks.toDouble()) * size.width;
double currX = (weekLabel.weekNum! / numWeeks.toDouble()) * size.width;
var timelineXDiff = (currTimeX - currX) / size.width;
double maxTimelineDiff = 0.08;
TextPainter textPainter = state.labelPainters[weekLabel.label];
TextPainter textPainter = state.labelPainters[weekLabel.label]!;
if (timelineXDiff > 0 &&
timelineXDiff < maxTimelineDiff &&
animationValue < 1) {
var mappedValue =
MathUtils.clampedMap(timelineXDiff, 0, maxTimelineDiff, 0, 1);
var lerpedColor = Color.lerp(
Colors.redAccent, Constants.milestoneTimelineColor, mappedValue);
Colors.redAccent, Constants.milestoneTimelineColor, mappedValue)!;
milestoneLinePaint.strokeWidth =
MathUtils.clampedMap(timelineXDiff, 0, maxTimelineDiff, 6, 1);
milestoneLinePaint.color = lerpedColor;