paint method
Called whenever the object needs to paint. The given Canvas has its
coordinate space configured such that the origin is at the top left of the
box. The area of the box is the size of the size
argument.
Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.
Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.
To paint text on a Canvas, use a TextPainter.
To paint an image on a Canvas:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.
-
In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.
Implementation
@override
Future<void> paint(Canvas canvas, Size size) async {
final rect = Rect.fromLTWH(0, 0, size.width, size.height);
/* double centerX = size.width / 2;
double centerY = size.height / 2;*/
final radius = min(size.width / 2, size.height / 2) -
20; // 20 is Manage circle outerline
final center = Offset(size.width / 2, size.height / 2);
menstruationDayCountNew = menstruationDayCount;
follicularDayCountNew = follicularDayCount + menstruationDayCount;
ovulationDayCountNew = follicularDayCountNew + ovulationDayCount;
drawTopAndTextPhase(
canvas,
rect,
center,
radius,
0,
menstruationDayCountNew,
menstruationColor,
menstruationBackgroundColor,
menstruationName,
menstruationTextColor);
drawTopAndTextPhase(
canvas,
rect,
center,
radius,
menstruationDayCount,
follicularDayCountNew,
follicularPhaseColor,
follicularBackgroundColor,
follicularPhaseName,
follicularTextColor);
drawTopAndTextPhase(
canvas,
rect,
center,
radius,
follicularDayCountNew,
ovulationDayCountNew,
ovulationColor,
ovulationBackgroundColor,
ovulationName,
ovulationTextColor);
drawTopAndTextPhase(
canvas,
rect,
center,
radius,
ovulationDayCountNew,
totalCycleDays,
lutealPhaseColor,
lutealPhaseBackgroundColor,
lutealPhaseName,
lutealPhaseTextColor);
/// Show outside phase text
if (phaseTextBoundaries != PhaseTextBoundaries.none) {
if (phaseTextBoundaries == PhaseTextBoundaries.both ||
phaseTextBoundaries == PhaseTextBoundaries.outside) {
drawOutSide(canvas, radius, size, menstruationName, 0,
menstruationDayCountNew, menstruationTextColor);
drawOutSide(canvas, radius, size, follicularPhaseName,
menstruationDayCount, follicularDayCountNew, follicularTextColor);
drawOutSide(canvas, radius, size, ovulationName, follicularDayCountNew,
ovulationDayCountNew, ovulationTextColor);
drawOutSide(
canvas,
radius,
size,
lutealPhaseName,
ovulationDayCountNew,
totalCycleDays,
lutealPhaseTextColor,
);
}
}
if (viewType == MenstrualCycleViewType.circleImage ||
viewType == MenstrualCycleViewType.circleText) {
/// draw border of circle background on center
final Paint circleBorder = Paint()
..color = centralCircleBorderColor
..style = PaintingStyle.fill;
canvas.drawCircle(
center, centralCircleSize + centralCircleBorderSize, circleBorder);
/// draw Circle background on center
final Paint backgroundPaint = Paint()
..color = centralCircleBackgroundColor
..style = PaintingStyle.fill;
canvas.drawCircle(center, centralCircleSize, backgroundPaint);
}
if (viewType == MenstrualCycleViewType.text ||
viewType == MenstrualCycleViewType.circleText) {
final centralTextPainter = TextPainter(
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
/// show title text
TextSpan titleLabel = TextSpan(
text: title,
style: TextStyle(
color: titleTextColor,
fontSize: titleTextSize,
fontWeight: titleFontWeight,
),
);
centralTextPainter.text = titleLabel;
centralTextPainter.layout();
final labelOffset = Offset(
center.dx - centralTextPainter.width / 2,
center.dx -
centralTextPainter.height / 2 -
(spaceBtnTitleMessage / 2));
centralTextPainter.paint(canvas, labelOffset);
/// Show message text
TextSpan messageLabel = TextSpan(
text: message,
style: TextStyle(
color: messageTextColor,
fontSize: messageTextSize,
fontWeight: messageFontWeight,
),
);
centralTextPainter.text = messageLabel;
centralTextPainter.layout();
final labelOffset2 = Offset(
center.dx - centralTextPainter.width / 2,
center.dx -
centralTextPainter.height / 2 +
(spaceBtnTitleMessage / 2));
centralTextPainter.paint(canvas, labelOffset2);
}
if (viewType == MenstrualCycleViewType.image ||
viewType == MenstrualCycleViewType.circleImage) {
/// Draw the image at the center
if (imageAssets != null) {
final paint = Paint()..style = PaintingStyle.stroke;
final imageSize = Size(imgSize, imgSize);
final imageOffset = Offset(
center.dx - imageSize.width / 2,
center.dy - imageSize.height / 2,
);
canvas.drawImageRect(
imageAssets!,
Rect.fromLTWH(0, 0, imageAssets!.width.toDouble(),
imageAssets!.height.toDouble()),
imageOffset & imageSize,
paint,
);
}
}
/// For loop for display a Days
for (int day = 1; day <= totalCycleDays; day++) {
final startAngle = (2 * pi / totalCycleDays) * (day - 1) - pi / 2;
final endAngle = (2 * pi / totalCycleDays) * day - pi / 2;
final middleAngle = (startAngle + endAngle) / 2;
// For / Arrow btn days
/*Paint paint = Paint()
..color = Colors.black
..strokeWidth = 2;
final newRadius = min(size.width / 2, size.height / 2) ;
double angle = (2 * pi / totalCycleDays) * day;
double startX = centerX + newRadius * cos(angle);
double startY = centerY + newRadius * sin(angle);
double endX = centerX + (newRadius - arcStrokeWidth) * cos(angle); // Shorter end inside
double endY = centerY + (newRadius - arcStrokeWidth) * sin(angle); // Shorter end inside
canvas.drawLine(Offset(startX, startY), Offset(endX, endY), paint);*/
/// Calculate Day position
final textX = center.dx + radius * cos(middleAngle);
final textY = center.dy + radius * sin(middleAngle);
final textPainter = TextPainter(
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
Color circleBorderColor = getSelectedBorderColor(day);
Color dayTextColor = getSelectedDayTextColor(day);
/// Draw "Day" label
TextSpan dayLabel = TextSpan(
text: dayTitle,
style: TextStyle(
color: dayTextColor,
fontSize: dayTitleFontSize,
fontWeight: FontWeight.normal,
),
);
textPainter.text = dayLabel;
textPainter.layout();
final labelOffset = Offset(
textX - textPainter.width / 2, textY - textPainter.height / 3.5 - 10);
/// 10 Mange space btn Day and 'Day' label
if (isShowDayTitle) {
textPainter.paint(canvas, labelOffset);
}
if (theme == MenstrualCycleTheme.circle) {
final Paint borderPaint = Paint()
..color = Colors.white
..style = PaintingStyle.stroke
..strokeWidth = 2;
canvas.drawCircle(Offset(textX, textY), circleDaySize, borderPaint);
/// set background color of selected day
final Paint highlightCirclePaint = Paint()
..color = circleBorderColor
..style = PaintingStyle.fill
..strokeWidth = 1;
canvas.drawCircle(
Offset(textX, textY), circleDaySize, highlightCirclePaint);
if (isShowDayTitle) {
textPainter.paint(canvas, labelOffset);
}
}
/// highlight current days
if (day == selectedDay) {
/// draw outer boarder for selected day
final Paint borderPaint = Paint()
..color = circleBorderColor
..style = PaintingStyle.stroke
..strokeWidth = 2;
canvas.drawCircle(
Offset(textX, textY), selectedDayCircleSize, borderPaint);
/// set background color of selected day
final Paint highlightCirclePaint = Paint()
..color = selectedDayBackgroundColor
..style = PaintingStyle.fill
..strokeWidth = 2;
canvas.drawCircle(Offset(textX, textY), selectedDayCircleSize - 1,
highlightCirclePaint);
if (isShowDayTitle) {
textPainter.paint(canvas, labelOffset);
}
}
double topPos = 2;
if (isShowDayTitle) {
topPos = 2.5;
}
/// Display day(number)
TextSpan dayText = TextSpan(
text: day.toString(),
style: TextStyle(
color: dayTextColor,
fontSize: (day == selectedDay) ? selectedDayFontSize : dayFontSize,
fontWeight: dayFontWeight,
),
);
textPainter.text = dayText;
textPainter.layout();
final textOffset = Offset(
textX - textPainter.width / 2, textY - textPainter.height / topPos);
textPainter.paint(
canvas,
textOffset,
);
}
}