Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to set default color #158

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/enums/taskoptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum TaskOption {
launchMeditationScreen,
toggleTipVisibility,
exportToCSV,
defaultColor,
}
7 changes: 7 additions & 0 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:taskly/screens/tasklist_screen.dart';
import 'package:taskly/models/task.dart';
import 'package:taskly/storage/task_storage.dart';
import 'package:taskly/service/random_tip_service.dart';
import 'package:taskly/widgets/default_color.dart';
import 'package:taskly/widgets/theme_mode_switch.dart';
import 'package:taskly/widgets/tip_of_day_card.dart';
import 'dart:io';
Expand Down Expand Up @@ -189,6 +190,8 @@ class _HomeScreenState extends State<HomeScreen> {
showtip = !showtip;
} else if (option == TaskOption.exportToCSV) {
exportToCSV(tasks);
} else if (option == TaskOption.defaultColor){
showColorPickerDialog(context);
}
});
void _editTask(int index) async {
Expand Down Expand Up @@ -347,6 +350,10 @@ class _HomeScreenState extends State<HomeScreen> {
value: TaskOption.launchMeditationScreen,
child: Text("Meditate"),
),
const PopupMenuItem(
value: TaskOption.defaultColor,
child: Text("Set Default Task Color"),
),
];
},
),
Expand Down
14 changes: 10 additions & 4 deletions lib/screens/taskform_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:taskly/models/task.dart';
import 'package:taskly/service/speech_service.dart';
import 'package:taskly/constants.dart';
import 'package:taskly/storage/task_storage.dart';
import 'package:taskly/utils/date_utils.dart';
import 'package:taskly/widgets/repeat_select_card.dart';

Expand All @@ -29,8 +30,9 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
var hasDeadline = false;
DateTime? deadline;
Task? selectedDependency;
Color selectedColor = Colors.blue;
Color? selectedColor;
int? repeatInterval;
Color defaultColor = Colors.blue;
late List<Task> _availableTasks;


Expand All @@ -50,7 +52,6 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
_availableTasks.removeWhere((task) => task.id == widget.task!.id);
}
hasDeadline = widget.task?.hasDeadline ?? false;
selectedColor = widget.task?.color ?? Colors.blue;
_titleController.addListener(_onTitleChanged);
_focusNode.addListener(() {
if (!_focusNode.hasFocus) {
Expand All @@ -61,6 +62,7 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
widget.task?.recurringDays == 0 ? null : widget.task?.recurringDays;

if (hasDeadline) deadline = widget.task?.deadline;
setSelectedColour();
}

@override
Expand All @@ -71,6 +73,10 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
_focusNode.dispose();
super.dispose();
}
void setSelectedColour() async{
defaultColor = await DefaultTaskColorStorage.loadDefaultColor();
selectedColor = widget.task?.color ?? defaultColor;
}

void _onTitleChanged() {
final input = _titleController.text.toLowerCase();
Expand Down Expand Up @@ -148,7 +154,7 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
title: const Text('Pick Task Color'),
content: SingleChildScrollView(
child: ColorPicker(
pickerColor: selectedColor,
pickerColor: selectedColor?? defaultColor,
onColorChanged: (color) {
setState(() {
selectedColor = color;
Expand Down Expand Up @@ -351,7 +357,7 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
description: _descController.text,
deadline: hasDeadline ? deadline : null,
recurringDays: repeatInterval,
color: selectedColor,
color: selectedColor ?? defaultColor,
);
Fluttertoast.showToast(
msg: editing
Expand Down
15 changes: 15 additions & 0 deletions lib/storage/task_storage.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:taskly/models/task.dart';

Expand All @@ -22,3 +23,17 @@ class TaskStorage {
.toList();
}
}

class DefaultTaskColorStorage {
static const String _defaultColorKey = 'defaultColor';

static Future<void> saveDefaultColor(int color) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(_defaultColorKey, color);
}

static Future<Color> loadDefaultColor() async {
final prefs = await SharedPreferences.getInstance();
return Color(prefs.getInt(_defaultColorKey)?? Colors.blue as int);
}
}
181 changes: 181 additions & 0 deletions lib/widgets/default_color.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:taskly/storage/task_storage.dart';

class DefaultColorPickerDialog extends StatefulWidget {
const DefaultColorPickerDialog({super.key});

@override
_DefaultColorPickerDialogState createState() => _DefaultColorPickerDialogState();
}

class _DefaultColorPickerDialogState extends State<DefaultColorPickerDialog> {
Color selectedColor = Colors.blue;

@override
void initState() {
super.initState();
DefaultTaskColorStorage.loadDefaultColor().then((color) {
setState(() {
selectedColor = color;
});
});
}

void _showColorPicker() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text(
'Pick Task Color',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ColorPicker(
pickerColor: selectedColor,
onColorChanged: (color) {
setState(() {
selectedColor = color;
});
},
pickerAreaHeightPercent: 0.8,
enableAlpha: false,
displayThumbColor: true,
portraitOnly: true,
labelTypes: const [],
),
const SizedBox(height: 20),
Container(
width: 60,
height: 60,
decoration: BoxDecoration(
color: selectedColor,
shape: BoxShape.circle,
border: Border.all(color: Colors.grey.shade300),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.1),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
),
],
),
),
actions: [
ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
style: ElevatedButton.styleFrom(
backgroundColor: selectedColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text(
'Select',
style: TextStyle(fontSize: 16),
),
),
],
),
);
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text(
'Choose Default Task Color',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
color: selectedColor,
shape: BoxShape.circle,
border: Border.all(color: Colors.grey.shade300),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.1),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: _showColorPicker,
icon: const Icon(Icons.color_lens),
label: const Text('Pick Color'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
style: TextButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
),
child: const Text(
'Cancel',
style: TextStyle(fontSize: 16),
),
),
ElevatedButton(
onPressed: () {
DefaultTaskColorStorage.saveDefaultColor(selectedColor.value);
Navigator.of(context).pop();
},
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text(
'Save',
style: TextStyle(fontSize: 16),
),
),
],
);
}
}

// Usage Example
void showColorPickerDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) => const DefaultColorPickerDialog(),
);
}