From f4f45e66be436e958293adecbd3143ee5ae7e7a6 Mon Sep 17 00:00:00 2001 From: suu3 Date: Thu, 4 Jul 2024 04:58:43 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=9C=84=EC=A0=AF=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/screens/home.dart | 61 ++------------------------------------ lib/widgets/task_list.dart | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 59 deletions(-) create mode 100644 lib/widgets/task_list.dart diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 5671a4a..77f4625 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod_demo/providers/task_provider.dart'; import 'package:flutter_riverpod_demo/widgets/add_task_bottomsheet.dart'; import 'package:flutter_riverpod_demo/widgets/empty_task_list.dart'; +import 'package:flutter_riverpod_demo/widgets/task_list.dart'; class MyHome extends ConsumerWidget { const MyHome({super.key}); @@ -28,70 +29,12 @@ class MyHome extends ConsumerWidget { return Scaffold( appBar: AppBar( - // leading: IconButton( - // icon: const Icon(Icons.menu, color: Colors.white), - // onPressed: () {}, - // ), title: const Text('할 일 목록', style: TextStyle(color: Colors.white)), centerTitle: true, ), body: Container( padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 10), - child: taskList.isNotEmpty - ? ListView.builder( - itemCount: taskList.length, - itemBuilder: (context, index) { - final task = taskList[index]; - return Padding( - padding: - const EdgeInsets.symmetric(vertical: 3, horizontal: 16), - child: Card( - child: ListTile( - leading: IconButton( - icon: Icon( - task.isCompleted - ? Icons.check_circle - : Icons.radio_button_unchecked, - color: task.isCompleted ? Colors.green : null, - ), - onPressed: () { - ref - .read(taskListProvider.notifier) - .toggleTaskCompletion(index); - }, - ), - title: Text( - task.title, - style: TextStyle( - decoration: task.isCompleted - ? TextDecoration.lineThrough - : null, - color: task.isCompleted ? Colors.grey : null, - ), - ), - subtitle: Text( - '${task.description}\n${task.date} 까지', - style: TextStyle( - color: task.isCompleted - ? Colors.grey.withOpacity(0.5) - : null, - ), - ), - trailing: IconButton( - icon: const Icon(Icons.delete), - color: Colors.red, - onPressed: () { - ref - .read(taskListProvider.notifier) - .removeTask(index); - }, - ), - ), - ), - ); - }, - ) - : const EmptyTaskList(), + child: taskList.isNotEmpty ? const TaskList() : const EmptyTaskList(), ), floatingActionButton: FloatingActionButton( onPressed: () { diff --git a/lib/widgets/task_list.dart b/lib/widgets/task_list.dart new file mode 100644 index 0000000..6fbe7d9 --- /dev/null +++ b/lib/widgets/task_list.dart @@ -0,0 +1,60 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_riverpod_demo/providers/task_provider.dart'; + +class TaskList extends ConsumerWidget { + const TaskList({super.key}); + + @override + Widget build(BuildContext context, WidgetRef ref) { + final taskList = ref.watch(taskListProvider); + + return ListView.builder( + itemCount: taskList.length, + itemBuilder: (context, index) { + final task = taskList[index]; + return Padding( + padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 16), + child: Card( + child: ListTile( + leading: IconButton( + icon: Icon( + task.isCompleted + ? Icons.check_circle + : Icons.radio_button_unchecked, + color: task.isCompleted ? Colors.green : null, + ), + onPressed: () { + ref + .read(taskListProvider.notifier) + .toggleTaskCompletion(index); + }, + ), + title: Text( + task.title, + style: TextStyle( + decoration: + task.isCompleted ? TextDecoration.lineThrough : null, + color: task.isCompleted ? Colors.grey : null, + ), + ), + subtitle: Text( + '${task.description}\n${task.date} 까지', + style: TextStyle( + color: task.isCompleted ? Colors.grey.withOpacity(0.5) : null, + ), + ), + trailing: IconButton( + icon: const Icon(Icons.delete), + color: Colors.red, + onPressed: () { + ref.read(taskListProvider.notifier).removeTask(index); + }, + ), + ), + ), + ); + }, + ); + } +}