Перевернутый индикатор уровня заряда батареи в классе Flutter CustomPainter
У меня есть этот код для отображения уровня заряда батареи, но он рисует внутренний цветной индикатор сверху вниз. Он должен рисовать снизу вверх, как вы видите на всех телефонах Android. Любые мысли о том, что я делаю неправильно?
class BatteryLevelPainter extends CustomPainter {
final int _batteryLevel;
final BatteryState _batteryState;
BatteryLevelPainter(this._batteryLevel, this._batteryState);
@override
void paint(Canvas canvas, Size size) {
Paint getPaint({Color color = Colors.black, PaintingStyle style = PaintingStyle.stroke}) {
return Paint()
..color = color
..strokeWidth = 1.0
..style = style;
}
final RRect batteryOutline = RRect.fromLTRBR(0.0, 0.0, size.width, size.height, const Radius.circular(2.0));
// Battery body
canvas.drawRRect(
batteryOutline,
getPaint(),
);
// Battery nub
canvas.drawRect(
const Rect.fromLTWH(4.0, -3.0, 4.0, 3.0),
getPaint(style: PaintingStyle.fill),
);
// Fill rect
canvas.clipRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height * (_batteryLevel / 100)));
Color indicatorColor;
if (_batteryLevel < 15) {
indicatorColor = Colors.red;
} else if (_batteryLevel < 30) {
indicatorColor = Colors.orange;
} else {
indicatorColor = Colors.green;
}
canvas.drawRRect(
RRect.fromLTRBR(0.5, 0.5, size.width - 0.5, size.height - 0.5, const Radius.circular(2.0)),
getPaint(style: PaintingStyle.fill, color: indicatorColor),
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
final BatteryLevelPainter old = oldDelegate as BatteryLevelPainter;
return old._batteryLevel != _batteryLevel || old._batteryState != _batteryState;
}
}
1 ответ
ИспользоватьRect.fromLTRB()
вместоRect.fromLTWH()
. Или вы можете использоватьRect.fromCircle()
или жеRect.fromCenter()
или жеRect.fromPoints()
. Прямоугольный класс
Добавьте эту строку:
canvas.translate(0.0, (size.height - size.height * (_batteryLevel / 100)));
Полный код:
class BatteryLevelPainter extends CustomPainter {
final int _batteryLevel;
final BatteryState _batteryState;
BatteryLevelPainter(this._batteryLevel, this._batteryState);
@override
void paint(Canvas canvas, Size size) {
Paint getPaint({Color color = Colors.black, PaintingStyle style = PaintingStyle.stroke}) {
return Paint()
..color = color
..strokeWidth = 1.0
..style = style;
}
final RRect batteryOutline = RRect.fromLTRBR(0.0, 0.0, size.width, size.height, const Radius.circular(2.0));
// Battery body
canvas.drawRRect(
batteryOutline,
getPaint(),
);
canvas.translate(0.0, (size.height - size.height * (_batteryLevel / 100))); // add this line
// Battery nub
canvas.drawRect(
const Rect.fromLTWH(4.0, -3.0, 4.0, 3.0),
getPaint(style: PaintingStyle.fill),
);
// Fill rect
canvas.clipRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height * (_batteryLevel / 100)));
Color indicatorColor;
if (_batteryLevel < 15) {
indicatorColor = Colors.red;
} else if (_batteryLevel < 30) {
indicatorColor = Colors.orange;
} else {
indicatorColor = Colors.green;
}
canvas.drawRRect(
RRect.fromLTRBR(0.5, 0.5, size.width - 0.5, size.height - 0.5, const Radius.circular(2.0)),
getPaint(style: PaintingStyle.fill, color: indicatorColor),
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
final BatteryLevelPainter old = oldDelegate as BatteryLevelPainter;
return old._batteryLevel != _batteryLevel || old._batteryState != _batteryState;
}
}