@@ -2,6 +2,7 @@ import 'dart:async';
22
33import 'package:easy_localization/easy_localization.dart' ;
44import 'package:flutter/material.dart' ;
5+ import 'package:flutter_hooks/flutter_hooks.dart' ;
56import 'package:hooks_riverpod/hooks_riverpod.dart' ;
67import 'package:timetable/core/constants/days.dart' ;
78import 'package:timetable/core/constants/grid_properties.dart' ;
@@ -53,46 +54,31 @@ class DaysBar extends ConsumerWidget {
5354 itemCount: daysLength,
5455 shrinkWrap: true ,
5556 itemBuilder: (context, index) {
56- return SizedBox (
57- width: isGridView!
58- ? ((screenWidth - (timeColumnWidth - 1 )) / daysLength)
59- : (screenWidth / daysLength),
60- child: TextButton (
61- onPressed: () {
62- if (isGridView! ) return ;
63-
64- controller.animateToPage (
65- index,
66- duration: const Duration (milliseconds: 300 ),
67- curve: Curves .easeInOut,
68- );
69- },
70- style: ButtonStyle (
71- foregroundColor: WidgetStateProperty .all <Color >(
72- Theme .of (context).colorScheme.onSurface,
73- ),
74- backgroundColor: isGridView! && (currentDay == index)
75- ? WidgetStateProperty .all <Color >(
76- theme == ThemeOption .auto
77- ? systemBrightness == Brightness .dark
78- ? darkCurrentDayColorScheme
79- : lightCurrentDayColorScheme
80- : theme == ThemeOption .dark
81- ? darkCurrentDayColorScheme
82- : lightCurrentDayColorScheme,
83- )
84- : null ,
85- ),
86- child: Text (
87- overflow: TextOverflow .clip,
88- softWrap: false ,
89- singleLetterDays
90- ? days[index].tr ()[0 ]
91- : isPortrait
92- ? days[index].tr ().substring (0 , 3 )
93- : days[index].tr (),
94- ),
95- ),
57+ return buildDayButton (
58+ index: index,
59+ currentDay: currentDay,
60+ currentDayColor: theme == ThemeOption .auto
61+ ? systemBrightness == Brightness .dark
62+ ? darkCurrentDayColorScheme
63+ : lightCurrentDayColorScheme
64+ : theme == ThemeOption .dark
65+ ? darkCurrentDayColorScheme
66+ : lightCurrentDayColorScheme,
67+ singleLetterDays: singleLetterDays,
68+ isPortrait: isPortrait,
69+ isGridView: isGridView! ,
70+ daysLength: daysLength,
71+ screenWidth: screenWidth,
72+ colorScheme: Theme .of (context).colorScheme,
73+ onTap: () {
74+ if (isGridView! ) return ;
75+
76+ controller.animateToPage (
77+ index,
78+ duration: const Duration (milliseconds: 300 ),
79+ curve: Curves .easeInOut,
80+ );
81+ },
9682 );
9783 },
9884 ),
@@ -101,9 +87,44 @@ class DaysBar extends ConsumerWidget {
10187 ),
10288 );
10389 }
90+
91+ Widget buildDayButton ({
92+ required int index,
93+ required bool isGridView,
94+ required int currentDay,
95+ required double screenWidth,
96+ required int daysLength,
97+ required bool singleLetterDays,
98+ required bool isPortrait,
99+ required Color currentDayColor,
100+ required VoidCallback ? onTap,
101+ required ColorScheme colorScheme,
102+ }) {
103+ final isCurrentDay = isGridView && (currentDay == index);
104+
105+ final dayText = singleLetterDays
106+ ? days[index].tr ()[0 ]
107+ : isPortrait
108+ ? days[index].tr ().substring (0 , 3 )
109+ : days[index].tr ();
110+
111+ return SizedBox (
112+ width: isGridView
113+ ? ((screenWidth - (timeColumnWidth - 1 )) / daysLength)
114+ : (screenWidth / daysLength),
115+ child: TextButton (
116+ onPressed: onTap,
117+ style: TextButton .styleFrom (
118+ foregroundColor: colorScheme.onSurface,
119+ backgroundColor: isCurrentDay ? currentDayColor : null ,
120+ ),
121+ child: Text (dayText, overflow: TextOverflow .clip, softWrap: false ),
122+ ),
123+ );
124+ }
104125}
105126
106- class DayBarUpdater extends StatefulWidget {
127+ class DayBarUpdater extends HookConsumerWidget {
107128 final PageController controller;
108129 final bool isGridView;
109130
@@ -114,48 +135,31 @@ class DayBarUpdater extends StatefulWidget {
114135 });
115136
116137 @override
117- State < DayBarUpdater > createState () => _DayBarUpdaterState ();
118- }
138+ Widget build ( BuildContext context, WidgetRef ref) {
139+ final currentDay = useState ( DateTime . now ().weekday - 1 );
119140
120- class _DayBarUpdaterState extends State <DayBarUpdater > {
121- late Timer timer;
122- int currentDay = DateTime .now ().weekday - 1 ;
141+ useEffect (() {
142+ Timer ? timer;
123143
124- @override
125- void initState () {
126- super .initState ();
127- updateTimer ();
128- }
144+ void updateTimer () {
145+ final now = DateTime .now ();
146+ final nextMidnight = DateTime (now.year, now.month, now.day + 1 );
129147
130- @override
131- void dispose () {
132- timer. cancel ();
133- super . dispose ( );
134- }
148+ timer = Timer (nextMidnight. difference (now), () {
149+ currentDay.value = DateTime . now ().weekday - 1 ;
150+ updateTimer ();
151+ } );
152+ }
135153
136- void updateTimer () {
137- final nextMidnight = DateTime (
138- DateTime .now ().add (const Duration (days: 1 )).day,
139- 0 ,
140- 0 ,
141- 0 ,
142- );
143- timer = Timer (nextMidnight.difference (DateTime .now ()), updateDay);
144- }
154+ updateTimer ();
145155
146- void updateDay () {
147- setState (() {
148- currentDay = DateTime .now ().weekday - 1 ;
149- });
150- updateTimer ();
151- }
156+ return () => timer? .cancel ();
157+ }, []);
152158
153- @override
154- Widget build (BuildContext context) {
155159 return DaysBar (
156- controller: widget. controller,
157- isGridView: widget. isGridView,
158- currentDay: currentDay,
160+ controller: controller,
161+ isGridView: isGridView,
162+ currentDay: currentDay.value ,
159163 );
160164 }
161165}
0 commit comments