Skip to content
This repository has been archived by the owner on Dec 7, 2024. It is now read-only.

Commit

Permalink
feat: 날짜기준 sort
Browse files Browse the repository at this point in the history
  • Loading branch information
suu3 committed Jul 3, 2024
1 parent fa1b4ad commit 1f4651b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/providers/task_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final taskListProvider =
class Task {
final String title;
final String description;
final String date;
final DateTime date;
bool isCompleted;

Task({
Expand All @@ -22,7 +22,7 @@ class Task {
class TaskListNotifier extends StateNotifier<List<Task>> {
TaskListNotifier() : super([]);

void addTask(String title, String description, String date) {
void addTask(String title, String description, DateTime date) {
final newTask = Task(title: title, description: description, date: date);
state = [...state, newTask];
}
Expand Down
21 changes: 13 additions & 8 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ class _MyHomeState extends ConsumerState<MyHome> {
int _selectedFilterIndex = filterAll.index;

List<Task> _filterTasks(List<Task> tasks) {
List<Task> filteredTasks;
switch (_selectedFilterIndex) {
case 1:
return tasks.where((task) => !task.isCompleted).toList();
filteredTasks = tasks.where((task) => !task.isCompleted).toList();
break;
case 2:
return tasks.where((task) => task.isCompleted).toList();
filteredTasks = tasks.where((task) => task.isCompleted).toList();
break;
default:
return tasks;
filteredTasks = tasks;
break;
}
filteredTasks.sort((a, b) => a.date.compareTo(b.date));
return filteredTasks;
}

void _setFilter(int index) {
Expand All @@ -42,10 +48,10 @@ class _MyHomeState extends ConsumerState<MyHome> {
final theme = Theme.of(context);
final taskList = ref.watch(taskListProvider);
final filteredTasks = _filterTasks(taskList);
final Map<String, String> taskData = {
final Map<String, dynamic> taskData = {
'title': '',
'description': '',
'date': ''
'date': DateTime.now(),
};

void addTask() {
Expand All @@ -67,8 +73,7 @@ class _MyHomeState extends ConsumerState<MyHome> {
: Column(
children: [
Padding(
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
padding: const EdgeInsets.only(top: 16, left: 16, right: 16),
child: Center(
child: ToggleButtons(
constraints: const BoxConstraints(
Expand Down Expand Up @@ -97,7 +102,7 @@ class _MyHomeState extends ConsumerState<MyHome> {
Expanded(
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 20, horizontal: 10),
vertical: 16, horizontal: 10),
child: filteredTasks.isNotEmpty
? TaskList(taskList: filteredTasks)
: Center(
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/add_task_bottomsheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class AddTaskBottomSheet extends StatefulWidget {
final Map<String, String> taskData;
final Map<String, dynamic> taskData;
final VoidCallback onPressed;

const AddTaskBottomSheet({
Expand Down Expand Up @@ -73,7 +73,7 @@ class _AddTaskBottomSheetState extends State<AddTaskBottomSheet> {
if (picked != null) {
setState(() {
_dateController.text = DateFormat.yMMMd().format(picked);
widget.taskData['date'] = DateFormat.yMMMd().format(picked);
widget.taskData['date'] = picked;
});
_updateButtonState();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/widgets/task_list.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_riverpod_demo/providers/task_provider.dart';
import 'package:intl/intl.dart';

class TaskList extends ConsumerWidget {
final List<Task> taskList;
Expand Down Expand Up @@ -39,7 +40,7 @@ class TaskList extends ConsumerWidget {
),
),
subtitle: Text(
'${task.description}\n${task.date} 까지',
'${task.description}\n${DateFormat.yMMMd().format(task.date)} 까지',
style: TextStyle(
color: task.isCompleted ? Colors.grey.withOpacity(0.5) : null,
),
Expand Down

0 comments on commit 1f4651b

Please sign in to comment.