@@ -18,100 +18,110 @@ class ClockScreen extends StatelessWidget {
1818 ),
1919 ),
2020 body: SafeArea (
21- child: Center (
22- child: RePaint .inline <Paint >(
23- frameRate: 1 ,
24- setUp: (box) => Paint ()
25- ..color = Colors .black
26- ..style = PaintingStyle .stroke
27- ..strokeWidth = 2 ,
28- render: (box, paint, canvas) {
29- final size = box.size; // Get the size of the box
30- final now = DateTime .now (); // Get the current time
21+ child: Padding (
22+ padding: const EdgeInsets .all (16 ),
23+ child: Center (
24+ child: AspectRatio (
25+ aspectRatio: 1 ,
26+ child: RePaint .inline <Paint >(
27+ frameRate: 6 ,
28+ setUp: (box) => Paint ()..style = PaintingStyle .stroke,
29+ render: (box, paint, canvas) {
30+ final size = box.size; // Get the size of the box
31+ final now = DateTime .now (); // Get the current time
3132
32- final center = size.center (Offset .zero);
33- final radius = size.shortestSide / 3 ;
33+ final center = size.center (Offset .zero);
34+ final radius = size.shortestSide / 2 - 8 ;
3435
35- // Draw the clock face
36- canvas.drawCircle (center, radius, paint);
36+ // Draw the clock face
37+ canvas.drawCircle (
38+ center,
39+ radius,
40+ paint
41+ ..color = Colors .black
42+ ..strokeWidth = 2 ,
43+ );
44+
45+ final textPainter = TextPainter (
46+ textAlign: TextAlign .center,
47+ textDirection: TextDirection .ltr,
48+ );
3749
38- final textPainter = TextPainter (
39- textAlign: TextAlign .center,
40- textDirection: TextDirection .ltr,
41- );
50+ // Draw the clock numbers
51+ for (int i = 1 ; i <= 12 ; i++ ) {
52+ final angle =
53+ math.pi / 6 * (i - 3 ); // Positioning the numbers
54+ final numberPosition = Offset (
55+ center.dx + radius * 0.8 * math.cos (angle),
56+ center.dy + radius * 0.8 * math.sin (angle),
57+ );
4258
43- // Draw the clock numbers
44- for (int i = 1 ; i <= 12 ; i++ ) {
45- final angle =
46- math.pi / 6 * (i - 3 ); // Positioning the numbers
47- final numberPosition = Offset (
48- center.dx + radius * 0.8 * math.cos (angle),
49- center.dy + radius * 0.8 * math.sin (angle),
50- );
59+ textPainter
60+ ..text = TextSpan (
61+ text: '$i ' ,
62+ style: TextStyle (
63+ color: Colors .black,
64+ fontSize: radius * 0.15 ,
65+ ),
66+ )
67+ ..layout ()
68+ ..paint (
69+ canvas,
70+ numberPosition -
71+ Offset (textPainter.width / 2 ,
72+ textPainter.height / 2 ),
73+ );
74+ }
5175
52- textPainter
53- ..text = TextSpan (
54- text: '$i ' ,
55- style: TextStyle (
56- color: Colors .black,
57- fontSize: radius * 0.15 ,
76+ // Draw the hour hand
77+ final hourAngle =
78+ math.pi / 6 * (now.hour % 12 + now.minute / 60 ) -
79+ math.pi / 2 ;
80+ final hourHandLength = radius * 0.5 ;
81+ paint
82+ ..color = Colors .black
83+ ..strokeWidth = 4 ;
84+ canvas.drawLine (
85+ center,
86+ Offset (
87+ center.dx + hourHandLength * math.cos (hourAngle),
88+ center.dy + hourHandLength * math.sin (hourAngle),
5889 ),
59- )
60- ..layout ()
61- ..paint (
62- canvas,
63- numberPosition -
64- Offset (textPainter.width / 2 , textPainter.height / 2 ),
90+ paint,
6591 );
66- }
67-
68- // Draw the hour hand
69- final hourAngle =
70- math.pi / 6 * (now.hour % 12 + now.minute / 60 ) -
71- math.pi / 2 ;
72- final hourHandLength = radius * 0.5 ;
73- paint
74- ..color = Colors .black
75- ..strokeWidth = 4 ;
76- canvas.drawLine (
77- center,
78- Offset (
79- center.dx + hourHandLength * math.cos (hourAngle),
80- center.dy + hourHandLength * math.sin (hourAngle),
81- ),
82- paint,
83- );
8492
85- // Draw the minute hand
86- final minuteAngle = math.pi / 30 * now.minute - math.pi / 2 ;
87- final minuteHandLength = radius * 0.7 ;
88- paint
89- ..color = Colors .blue
90- ..strokeWidth = 3 ;
91- canvas.drawLine (
92- center,
93- Offset (
94- center.dx + minuteHandLength * math.cos (minuteAngle),
95- center.dy + minuteHandLength * math.sin (minuteAngle),
96- ),
97- paint,
98- );
93+ // Draw the minute hand
94+ final minuteAngle = math.pi / 30 * now.minute - math.pi / 2 ;
95+ final minuteHandLength = radius * 0.7 ;
96+ paint
97+ ..color = Colors .blue
98+ ..strokeWidth = 3 ;
99+ canvas.drawLine (
100+ center,
101+ Offset (
102+ center.dx + minuteHandLength * math.cos (minuteAngle),
103+ center.dy + minuteHandLength * math.sin (minuteAngle),
104+ ),
105+ paint,
106+ );
99107
100- // Draw the second hand
101- final secondAngle = math.pi / 30 * now.second - math.pi / 2 ;
102- final secondHandLength = radius * 0.9 ;
103- paint
104- ..color = Colors .red
105- ..strokeWidth = 2 ;
106- canvas.drawLine (
107- center,
108- Offset (
109- center.dx + secondHandLength * math.cos (secondAngle),
110- center.dy + secondHandLength * math.sin (secondAngle),
111- ),
112- paint,
113- );
114- },
108+ // Draw the second hand
109+ final secondAngle = math.pi / 30 * now.second - math.pi / 2 ;
110+ final secondHandLength = radius * 0.9 ;
111+ paint
112+ ..color = Colors .red
113+ ..strokeWidth = 2 ;
114+ canvas.drawLine (
115+ center,
116+ Offset (
117+ center.dx + secondHandLength * math.cos (secondAngle),
118+ center.dy + secondHandLength * math.sin (secondAngle),
119+ ),
120+ paint,
121+ );
122+ },
123+ ),
124+ ),
115125 ),
116126 ),
117127 ),
0 commit comments