Skip to content

Commit 33a10cb

Browse files
fix: make addtasktotaskc stateless
1 parent 5a7d7af commit 33a10cb

File tree

3 files changed

+41
-76
lines changed

3 files changed

+41
-76
lines changed

lib/app/modules/home/controllers/home_controller.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class HomeController extends GetxController {
5151
final Rx<SupportedLanguage> selectedLanguage = SupportedLanguage.english.obs;
5252
final ScrollController scrollController = ScrollController();
5353
final RxBool showbtn = false.obs;
54+
late TaskDatabase taskdb;
5455
var tasks = <Tasks>[].obs;
5556

5657
@override
@@ -69,6 +70,8 @@ class HomeController extends GetxController {
6970
if (Platform.isAndroid) {
7071
handleHomeWidgetClicked();
7172
}
73+
taskdb = TaskDatabase();
74+
taskdb.open();
7275
getUniqueProjects();
7376
_loadTaskChampion();
7477
fetchTasksFromDB();
@@ -483,6 +486,7 @@ class HomeController extends GetxController {
483486
// dialogue box
484487
final formKey = GlobalKey<FormState>();
485488
final namecontroller = TextEditingController();
489+
final projectcontroller = TextEditingController();
486490
var due = Rxn<DateTime>();
487491
RxString dueString = ''.obs;
488492
RxString priority = 'M'.obs;

lib/app/modules/home/views/add_task_to_taskc_bottom_sheet.dart

Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,15 @@
22
import 'package:flutter/material.dart';
33
import 'package:google_fonts/google_fonts.dart';
44
import 'package:intl/intl.dart';
5-
import 'package:shared_preferences/shared_preferences.dart';
65
import 'package:taskwarrior/api_service.dart';
6+
import 'package:taskwarrior/app/modules/home/controllers/home_controller.dart';
77
import 'package:taskwarrior/app/utils/constants/taskwarrior_colors.dart';
88
import 'package:taskwarrior/app/utils/constants/taskwarrior_fonts.dart';
99
import 'package:taskwarrior/app/utils/theme/app_settings.dart';
1010

11-
class AddTaskToTaskcBottomSheet extends StatefulWidget {
12-
const AddTaskToTaskcBottomSheet({super.key});
13-
14-
@override
15-
State<AddTaskToTaskcBottomSheet> createState() =>
16-
_AddTaskToTaskcBottomSheetState();
17-
}
18-
19-
class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
20-
final formKey = GlobalKey<FormState>();
21-
final namecontroller = TextEditingController();
22-
DateTime? due;
23-
String dueString = '';
24-
String priority = 'M';
25-
final projectController = TextEditingController();
26-
String project = '';
27-
bool inThePast = false;
28-
bool change24hr = false;
29-
late TaskDatabase taskdb;
30-
31-
Future<void> checkto24hr() async {
32-
final SharedPreferences prefs = await SharedPreferences.getInstance();
33-
setState(() {
34-
change24hr = prefs.getBool('24hourformate') ?? false;
35-
});
36-
}
37-
38-
@override
39-
void initState() {
40-
super.initState();
41-
checkto24hr();
42-
taskdb = TaskDatabase();
43-
taskdb.open();
44-
}
45-
46-
@override
47-
void dispose() {
48-
projectController.dispose();
49-
namecontroller.dispose();
50-
super.dispose();
51-
}
11+
class AddTaskToTaskcBottomSheet extends StatelessWidget {
12+
final HomeController homeController;
13+
const AddTaskToTaskcBottomSheet({super.key, required this.homeController});
5214

5315
@override
5416
Widget build(BuildContext context) {
@@ -78,7 +40,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
7840
),
7941
),
8042
content: Form(
81-
key: formKey,
43+
key: homeController.formKey,
8244
child: SizedBox(
8345
width: MediaQuery.of(context).size.width * 0.8,
8446
child: Column(
@@ -108,7 +70,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
10870

10971
Widget buildName() => TextFormField(
11072
autofocus: true,
111-
controller: namecontroller,
73+
controller: homeController.namecontroller,
11274
style: TextStyle(
11375
color: AppSettings.isDarkMode
11476
? TaskWarriorColors.white
@@ -129,7 +91,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
12991

13092
Widget buildProject() => TextFormField(
13193
autofocus: true,
132-
controller: projectController,
94+
controller: homeController.projectcontroller,
13395
style: TextStyle(
13496
color: AppSettings.isDarkMode
13597
? TaskWarriorColors.white
@@ -160,7 +122,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
160122
Expanded(
161123
child: GestureDetector(
162124
child: TextFormField(
163-
style: inThePast
125+
style: homeController.inThePast.value
164126
? TextStyle(color: TaskWarriorColors.red)
165127
: TextStyle(
166128
color: AppSettings.isDarkMode
@@ -169,11 +131,13 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
169131
),
170132
readOnly: true,
171133
controller: TextEditingController(
172-
text: (due != null) ? dueString : null,
134+
text: (homeController.due.value != null)
135+
? homeController.dueString.value
136+
: null,
173137
),
174138
decoration: InputDecoration(
175139
hintText: 'Select due date',
176-
hintStyle: inThePast
140+
hintStyle: homeController.inThePast.value
177141
? TextStyle(color: TaskWarriorColors.red)
178142
: TextStyle(
179143
color: AppSettings.isDarkMode
@@ -215,7 +179,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
215179
},
216180
fieldHintText: "Month/Date/Year",
217181
context: context,
218-
initialDate: due ?? DateTime.now(),
182+
initialDate: homeController.due.value ?? DateTime.now(),
219183
firstDate: DateTime.now(),
220184
lastDate: DateTime(2037, 12, 31),
221185
);
@@ -251,14 +215,15 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
251215
),
252216
child: MediaQuery(
253217
data: MediaQuery.of(context).copyWith(
254-
alwaysUse24HourFormat: change24hr,
218+
alwaysUse24HourFormat:
219+
homeController.change24hr.value,
255220
),
256221
child: child!),
257222
);
258223
},
259224
context: context,
260-
initialTime:
261-
TimeOfDay.fromDateTime(due ?? DateTime.now()),
225+
initialTime: TimeOfDay.fromDateTime(
226+
homeController.due.value ?? DateTime.now()),
262227
);
263228
if (time != null) {
264229
var dateTime = date.add(
@@ -267,12 +232,11 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
267232
minutes: time.minute,
268233
),
269234
);
270-
due = dateTime.toUtc();
271-
dueString = DateFormat("yyyy-MM-dd").format(dateTime);
235+
homeController.due.value = dateTime.toUtc();
236+
homeController.dueString.value =
237+
DateFormat("yyyy-MM-dd").format(dateTime);
272238
if (dateTime.isBefore(DateTime.now())) {
273-
setState(() {
274-
inThePast = true;
275-
});
239+
homeController.inThePast.value = true;
276240

277241
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
278242
content: Text(
@@ -289,9 +253,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
289253
.kLightSecondaryBackgroundColor,
290254
duration: const Duration(seconds: 2)));
291255
} else {
292-
setState(() {
293-
inThePast = false;
294-
});
256+
homeController.inThePast.value = false;
295257
}
296258
}
297259
}
@@ -322,7 +284,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
322284
dropdownColor: AppSettings.isDarkMode
323285
? TaskWarriorColors.kdialogBackGroundColor
324286
: TaskWarriorColors.kLightDialogBackGroundColor,
325-
value: priority,
287+
value: homeController.priority.value,
326288
elevation: 16,
327289
style: GoogleFonts.poppins(
328290
color: AppSettings.isDarkMode
@@ -336,9 +298,7 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
336298
: TaskWarriorColors.kLightDialogBackGroundColor,
337299
),
338300
onChanged: (String? newValue) {
339-
setState(() {
340-
priority = newValue!;
341-
});
301+
homeController.priority.value = newValue!;
342302
},
343303
items: <String>['H', 'M', 'L', 'None']
344304
.map<DropdownMenuItem<String>>((String value) {
@@ -376,26 +336,25 @@ class _AddTaskToTaskcBottomSheetState extends State<AddTaskToTaskcBottomSheet> {
376336
),
377337
),
378338
onPressed: () async {
379-
if (formKey.currentState!.validate()) {
339+
if (homeController.formKey.currentState!.validate()) {
380340
var task = Tasks(
381-
description: namecontroller.text,
341+
description: homeController.namecontroller.text,
382342
status: 'pending',
383-
priority: priority,
343+
priority: homeController.priority.value,
384344
entry: DateTime.now().toIso8601String(),
385345
id: 0,
386-
project: projectController.text,
346+
project: homeController.projectcontroller.text,
387347
uuid: '',
388348
urgency: 0,
389-
due: dueString,
349+
due: homeController.dueString.value,
390350
// dueString.toIso8601String(),
391351
end: '',
392352
modified: 'r');
393-
await taskdb.insertTask(task);
394-
namecontroller.text = '';
395-
due = null;
396-
priority = 'M';
397-
project = '';
398-
setState(() {});
353+
await homeController.taskdb.insertTask(task);
354+
homeController.namecontroller.text = '';
355+
homeController.due.value = null;
356+
homeController.priority.value = 'M';
357+
homeController.projectcontroller.text = '';
399358
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
400359
content: Text(
401360
'Task Added Successfully!',

lib/app/modules/home/views/home_page_floating_action_button.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class HomePageFloatingActionButton extends StatelessWidget {
3232
onPressed: () => (controller.taskchampion.value)
3333
? (showDialog(
3434
context: context,
35-
builder: (context) => const AddTaskToTaskcBottomSheet(),
35+
builder: (context) => AddTaskToTaskcBottomSheet(
36+
homeController: controller,
37+
),
3638
).then((value) {
3739
if (controller.isSyncNeeded.value && value != "cancel") {
3840
controller.isNeededtoSyncOnStart(context);

0 commit comments

Comments
 (0)