Intensive if chain refactoring (#145194)
This pull request refactors if-statements into switch expressions, as part of the effort to solve issue #144903. Making changes beyond just swapping syntax is more difficult (and also more difficult to review, I apologize), but much more satisfying too.
This commit is contained in:
@@ -97,7 +97,7 @@ class PaintTest extends CustomPainter {
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final double height = size.height;
|
||||
final double halfHeight = size.height / 2;
|
||||
double x = 0;
|
||||
const double strokeSize = .5;
|
||||
const double zoomFactor = .5;
|
||||
@@ -125,19 +125,12 @@ class PaintTest extends CustomPainter {
|
||||
final Float32List offsets = Float32List(consolidate ? waveData.length * 4 : 4);
|
||||
int used = 0;
|
||||
for (index = 0; index < waveData.length; index++) {
|
||||
Paint curPaint;
|
||||
Offset p1;
|
||||
if (waveData[index].isNegative) {
|
||||
curPaint = paintPos;
|
||||
p1 = Offset(x, height * 1 / 2 - waveData[index] / 32768 * (height / 2));
|
||||
} else if (waveData[index] == 0) {
|
||||
curPaint = paintZero;
|
||||
p1 = Offset(x, height * 1 / 2 + 1);
|
||||
} else {
|
||||
curPaint = (waveData[index] == 0) ? paintZero : paintNeg;
|
||||
p1 = Offset(x, height * 1 / 2 - waveData[index] / 32767 * (height / 2));
|
||||
}
|
||||
final Offset p0 = Offset(x, height * 1 / 2);
|
||||
final (Paint curPaint, Offset p1) = switch (waveData[index]) {
|
||||
< 0 => (paintPos, Offset(x, halfHeight * (1 - waveData[index] / 32768))),
|
||||
> 0 => (paintNeg, Offset(x, halfHeight * (1 - waveData[index] / 32767))),
|
||||
_ => (paintZero, Offset(x, halfHeight + 1)),
|
||||
};
|
||||
final Offset p0 = Offset(x, halfHeight);
|
||||
if (consolidate) {
|
||||
if (listPaint != null && listPaint != curPaint) {
|
||||
canvas.drawRawPoints(PointMode.lines, offsets.sublist(0, used), listPaint);
|
||||
@@ -179,7 +172,7 @@ class PaintSomeTest extends CustomPainter {
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final double height = size.height;
|
||||
final double halfHeight = size.height / 2;
|
||||
double x = 0;
|
||||
const double strokeSize = .5;
|
||||
const double zoomFactor = .5;
|
||||
@@ -203,19 +196,12 @@ class PaintSomeTest extends CustomPainter {
|
||||
..style = PaintingStyle.stroke;
|
||||
|
||||
for (int index = from; index <= to; index++) {
|
||||
Paint curPaint;
|
||||
Offset p1;
|
||||
if (waveData[index].isNegative) {
|
||||
curPaint = paintPos;
|
||||
p1 = Offset(x, height * 1 / 2 - waveData[index] / 32768 * (height / 2));
|
||||
} else if (waveData[index] == 0) {
|
||||
curPaint = paintZero;
|
||||
p1 = Offset(x, height * 1 / 2 + 1);
|
||||
} else {
|
||||
curPaint = (waveData[index] == 0) ? paintZero : paintNeg;
|
||||
p1 = Offset(x, height * 1 / 2 - waveData[index] / 32767 * (height / 2));
|
||||
}
|
||||
final Offset p0 = Offset(x, height * 1 / 2);
|
||||
final (Paint curPaint, Offset p1) = switch (waveData[index]) {
|
||||
< 0 => (paintPos, Offset(x, halfHeight * (1 - waveData[index] / 32768))),
|
||||
> 0 => (paintNeg, Offset(x, halfHeight * (1 - waveData[index] / 32767))),
|
||||
_ => (paintZero, Offset(x, halfHeight + 1)),
|
||||
};
|
||||
final Offset p0 = Offset(x, halfHeight);
|
||||
canvas.drawLine(p0, p1, curPaint);
|
||||
x += zoomFactor;
|
||||
}
|
||||
|
||||
@@ -474,58 +474,13 @@ const Color lerpToColor = Colors.red;
|
||||
const BorderSide lerpToBorder = BorderSide(width: 5, color: lerpToColor);
|
||||
|
||||
ShapeBorder? lerpBorder(StarBorder border, LerpTarget target, double t, {bool to = true}) {
|
||||
switch (target) {
|
||||
case LerpTarget.circle:
|
||||
if (to) {
|
||||
return border.lerpTo(const CircleBorder(side: lerpToBorder, eccentricity: 0.5), t);
|
||||
} else {
|
||||
return border.lerpFrom(const CircleBorder(side: lerpToBorder, eccentricity: 0.5), t);
|
||||
}
|
||||
case LerpTarget.roundedRect:
|
||||
if (to) {
|
||||
return border.lerpTo(
|
||||
const RoundedRectangleBorder(
|
||||
side: lerpToBorder,
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(10),
|
||||
),
|
||||
),
|
||||
t,
|
||||
);
|
||||
} else {
|
||||
return border.lerpFrom(
|
||||
const RoundedRectangleBorder(
|
||||
side: lerpToBorder,
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(10),
|
||||
),
|
||||
),
|
||||
t,
|
||||
);
|
||||
}
|
||||
case LerpTarget.rect:
|
||||
if (to) {
|
||||
return border.lerpTo(const RoundedRectangleBorder(side: lerpToBorder), t);
|
||||
} else {
|
||||
return border.lerpFrom(const RoundedRectangleBorder(side: lerpToBorder), t);
|
||||
}
|
||||
case LerpTarget.stadium:
|
||||
if (to) {
|
||||
return border.lerpTo(const StadiumBorder(side: lerpToBorder), t);
|
||||
} else {
|
||||
return border.lerpFrom(const StadiumBorder(side: lerpToBorder), t);
|
||||
}
|
||||
case LerpTarget.polygon:
|
||||
if (to) {
|
||||
return border.lerpTo(const StarBorder.polygon(side: lerpToBorder, sides: 4), t);
|
||||
} else {
|
||||
return border.lerpFrom(const StarBorder.polygon(side: lerpToBorder, sides: 4), t);
|
||||
}
|
||||
case LerpTarget.star:
|
||||
if (to) {
|
||||
return border.lerpTo(const StarBorder(side: lerpToBorder, innerRadiusRatio: .5), t);
|
||||
} else {
|
||||
return border.lerpFrom(const StarBorder(side: lerpToBorder, innerRadiusRatio: .5), t);
|
||||
}
|
||||
}
|
||||
final OutlinedBorder targetBorder = switch (target) {
|
||||
LerpTarget.circle => const CircleBorder(side: lerpToBorder, eccentricity: 0.5),
|
||||
LerpTarget.rect => const RoundedRectangleBorder(side: lerpToBorder),
|
||||
LerpTarget.stadium => const StadiumBorder(side: lerpToBorder),
|
||||
LerpTarget.polygon => const StarBorder.polygon(side: lerpToBorder, sides: 4),
|
||||
LerpTarget.star => const StarBorder(side: lerpToBorder, innerRadiusRatio: 0.5),
|
||||
LerpTarget.roundedRect => RoundedRectangleBorder(side: lerpToBorder, borderRadius: BorderRadius.circular(10)),
|
||||
};
|
||||
return to ? border.lerpTo(targetBorder, t) : border.lerpFrom(targetBorder, t);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user