Skip to content

Commit b4dd7cc

Browse files
committed
change daybarupdater from statefulwidget to hookconsumerwidget to fix some performance issues
1 parent 27a1e7c commit b4dd7cc

3 files changed

Lines changed: 81 additions & 79 deletions

File tree

lib/features/settings/providers/settings.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class SettingsNotifier extends StateNotifier<Settings> {
1111
loadSettings();
1212
}
1313

14-
// should be self-explanatory
1514
void updateDefaultSubjectDuration(Duration defaultSubjectDuration) {
1615
final newState = state.copyWith(
1716
defaultSubjectDuration: defaultSubjectDuration,

lib/features/subjects/widgets/color_picker.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
3-
import 'package:flutter_hooks/flutter_hooks.dart';
43

54
/// Custom color picker screen for the color configuration screen.
6-
class ColorPickerScreen extends HookWidget {
5+
class ColorPickerScreen extends StatelessWidget {
76
/// this is the color that will be changed by the color picker.
87
final ValueNotifier<Color> color;
98

lib/features/timetable/widgets/day_view/days_bar.dart

Lines changed: 80 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22

33
import 'package:easy_localization/easy_localization.dart';
44
import 'package:flutter/material.dart';
5+
import 'package:flutter_hooks/flutter_hooks.dart';
56
import 'package:hooks_riverpod/hooks_riverpod.dart';
67
import 'package:timetable/core/constants/days.dart';
78
import '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

Comments
 (0)