import 'dart:math'; import 'package:flutter/material.dart'; class WaitingClock extends…
- User
import 'dart:math';
import 'package:flutter/material.dart';
class WaitingClock extends StatefulWidget { final Color outerRingColor; final Color innerRingColor; final Color handColor; final bool smoothAnimation; final double size; final double strokeWidth;
const WaitingClock({ Key? key, this.outerRingColor = const Color(0xFF8E44AD), // Пурпурный цвет как на изображении this.innerRingColor = Colors.white, this.handColor = const Color(0xFFF1C40F), // Желтый цвет как на изображении this.smoothAnimation = true, // По умолчанию плавная анимация this.size = 100.0, this.strokeWidth = 10.0, }) : super(key: key);
@override State createState() => _WaitingClockState(); }
class _WaitingClockState extends State with SingleTickerProviderStateMixin { late AnimationController _controller;
@override void initState() { super.initState(); _controller = AnimationController(vsync: this, duration: const Duration(seconds: 2))..repeat(); }
@override void dispose() { _controller.dispose(); super.dispose(); }
@override Widget build(BuildContext context) { return AnimatedBuilder( animation: _controller, builder: (context, child) { return CustomPaint( size: Size(widget.size, widget.size), painter: ClockPainter( progress: widget.smoothAnimation ? _controller.value : (_controller.value * 12).floor() / 12, // Для рывков делим на 12 шагов outerRingColor: widget.outerRingColor, innerRingColor: widget.innerRingColor, handColor: widget.handColor, strokeWidth: widget.strokeWidth, ), ); }, ); } }
class ClockPainter extends CustomPainter { final double progress; final Color outerRingColor; final Color innerRingColor; final Color handColor; final double strokeWidth;
ClockPainter({required this.progress, required this.outerRingColor, required this.innerRingColor, required this.handColor, required this.strokeWidth});
@override void paint(Canvas canvas, Size size) { final center = Offset(size.width / 2, size.height / 2); final radius = min(size.width, size.height) / 2;
// Внешнее кольцо (пурпурное) final outerRingPaint = Paint() ..color = outerRingColor ..style = PaintingStyle.stroke ..strokeWidth = strokeWidth; canvas.drawCircle(center, radius - strokeWidth / 2, outerRingPaint); // Draw the circular background final backgroundPaint = Paint() ..color = Color(0xFFDAD5D2) ..style = PaintingStyle.fill; canvas.drawCircle(center, radius, backgroundPaint); final xRate = 5; // Внутреннее кольцо (белое) final innerRingPaint = Paint() ..color = innerRingColor ..style = PaintingStyle.stroke ..strokeWidth = strokeWidth / 10; canvas.drawCircle(center, radius - strokeWidth / 2, innerRingPaint); // Рисуем метки минут (как на изображении полосы) final minuteMarkPaint = Paint() ..color = Colors.white ..style = PaintingStyle.stroke ..strokeWidth = 1; for (int i = 0; i < 60; i++) { final angle = (i * pi / 30); final markLength = i % 5 == 0 ? strokeWidth : strokeWidth / 2; final innerPoint = Offset(center.dx + (radius - strokeWidth * xRate) * cos(angle), center.dy + (radius - strokeWidth * xRate) * sin(angle)); final outerPoint = Offset(center.dx + (radius - strokeWidth * xRate + markLength) * cos(angle), center.dy + (radius - strokeWidth * xRate + markLength) * sin(angle)); canvas.drawLine(innerPoint, outerPoint, minuteMarkPaint); } // Стрелка final handAngle = 2 * pi * progress - pi / 2; // Начало с 12 часов final handPaint = Paint() ..color = handColor ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round ..strokeWidth = strokeWidth / 4; final handStart = center; final handEnd = Offset(center.dx + (radius - strokeWidth * 4.5) * cos(handAngle), center.dy + (radius - strokeWidth * 4.5) * sin(handAngle)); canvas.drawLine(handStart, handEnd, handPaint);}
@override bool shouldRepaint(covariant ClockPainter oldDelegate) { return oldDelegate.progress != progress || oldDelegate.outerRingColor != outerRingColor || oldDelegate.innerRingColor != innerRingColor || oldDelegate.handColor != handColor || oldDelegate.strokeWidth != strokeWidth; } }
how to add shadow to clock hand to make more 3d look
Assistant
To add a shadow to the clock hand to make it look more 3D, you can use the
drawShadowmethod provided by theCanvasclass in Flutter. This method allows you to draw a shadow for a given path, which in this case will be the path of the clock hand.Here's how you can modify your
ClockPainterclass to include a shadow for the clock hand:
Updated Code
class ClockPainter extends CustomPainter { final double progress; final Color outerRingColor; final Color innerRingColor; final Color handColor; final double strokeWidth; ClockPainter({ required this.progress, required this.outerRingColor, required this.innerRingColor, required this.handColor, required this.strokeWidth, }); @override void paint(Canvas canvas, Size size) { final center = Offset(size.width / 2, size.height / 2); final radius = min(size.width, size.height) / 2; // Внешнее кольцо (пурпурное) final outerRingPaint = Paint() ..color = outerRingColor ..style = PaintingStyle.stroke ..strokeWidth = strokeWidth; canvas.drawCircle(center, radius - strokeWidth / 2, outerRingPaint); // Draw the circular background final backgroundPaint = Paint() ..color = Color(0xFFDAD5D2) ..style = PaintingStyle.fill; canvas.drawCircle(center, radius, backgroundPaint); final xRate = 5; // Внутреннее кольцо (белое) final innerRingPaint = Paint() ..color = innerRingColor ..style = PaintingStyle.stroke ..strokeWidth = strokeWidth / 10; canvas.drawCircle(center, radius - strokeWidth / 2, innerRingPaint); // Рисуем метки минут (как на изображении полосы) final minuteMarkPaint = Paint() ..color = Colors.white ..style = PaintingStyle.stroke ..strokeWidth = 1; for (int i = 0; i < 60; i++) { final angle = (i * pi / 30); final markLength = i % 5 == 0 ? strokeWidth : strokeWidth / 2; final innerPoint = Offset(center.dx + (radius - strokeWidth * xRate) * cos(angle), center.dy + (radius - strokeWidth * xRate) * sin(angle)); final outerPoint = Offset(center.dx + (radius - strokeWidth * xRate + markLength) * cos(angle), center.dy + (radius - strokeWidth * xRate + markLength) * sin(angle)); canvas.drawLine(innerPoint, outerPoint, minuteMarkPaint); } // Стрелка final handAngle = 2 * pi * progress - pi / 2; // Начало с 12 часов final handPaint = Paint() ..color = handColor ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round ..strokeWidth = strokeWidth / 4; final handStart = center; final handEnd = Offset(center.dx + (radius - strokeWidth * 4.5) * cos(handAngle), center.dy + (radius - strokeWidth * 4.5) * sin(handAngle)); // Draw the shadow for the hand final shadowPaint = Paint() ..color = Colors.black.withOpacity(0.5) ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round ..strokeWidth = strokeWidth / 4; final shadowOffset = Offset(2, 2); // Offset for the shadow final shadowStart = Offset(center.dx + shadowOffset.dx, center.dy + shadowOffset.dy); final shadowEnd = Offset(shadowStart.dx + (radius - strokeWidth * 4.5) * cos(handAngle), shadowStart.dy + (radius - strokeWidth * 4.5) * sin(handAngle)); canvas.drawLine(shadowStart, shadowEnd, shadowPaint); // Draw the hand canvas.drawLine(handStart, handEnd, handPaint); } @override bool shouldRepaint(covariant ClockPainter oldDelegate) { return oldDelegate.progress != progress || oldDelegate.outerRingColor != outerRingColor || oldDelegate.innerRingColor != innerRingColor || oldDelegate.handColor != handColor || oldDelegate.strokeWidth != strokeWidth; } }
Explanation of Changes
-
Shadow Paint:
- Created a
shadowPaintobject with a semi-transparent black color to represent the shadow. - Set the
strokeCapandstrokeWidthto match the hand paint.
- Created a
-
Shadow Offset:
- Defined an
shadowOffsetto specify the direction and distance of the shadow from the hand. In this example, the shadow is offset by 2 pixels to the right and 2 pixels down.
- Defined an
-
Drawing the Shadow:
- Calculated the start and end points for the shadow line by offsetting the hand's start and end points by the
shadowOffset. - Drew the shadow line using
canvas.drawLinewith theshadowPaint.
- Calculated the start and end points for the shadow line by offsetting the hand's start and end points by the
-
Drawing the Hand:
- Drew the hand line after drawing the shadow to ensure the hand appears above the shadow.
Output
The clock hand will now have a shadow, giving it a more 3D appearance. You can adjust the
shadowOffsetand the opacity of theshadowPaintto fine-tune the look of the shadow.Let me know if you need further assistance!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.