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

Commit

Permalink
refactor: filter 상수
Browse files Browse the repository at this point in the history
  • Loading branch information
suu3 committed Jul 3, 2024
1 parent fc97f0d commit fa1b4ad
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
12 changes: 12 additions & 0 deletions lib/constants/todo_filter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Filter {
final int index;
final String label;

const Filter(this.index, this.label);
}

const Filter filterAll = Filter(0, '전체 할 일');
const Filter filterIncomplete = Filter(1, '미완료된 할 일');
const Filter filterComplete = Filter(2, '완료된 할 일');

const List<Filter> filters = [filterAll, filterIncomplete, filterComplete];
55 changes: 26 additions & 29 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_riverpod_demo/constants/todo_filter.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';
Expand All @@ -13,15 +14,17 @@ class MyHome extends ConsumerStatefulWidget {
}

class _MyHomeState extends ConsumerState<MyHome> {
int _selectedFilterIndex = 0;
int _selectedFilterIndex = filterAll.index;

List<Task> _filterTasks(List<Task> tasks) {
if (_selectedFilterIndex == 1) {
return tasks.where((task) => !task.isCompleted).toList();
} else if (_selectedFilterIndex == 2) {
return tasks.where((task) => task.isCompleted).toList();
switch (_selectedFilterIndex) {
case 1:
return tasks.where((task) => !task.isCompleted).toList();
case 2:
return tasks.where((task) => task.isCompleted).toList();
default:
return tasks;
}
return tasks;
}

void _setFilter(int index) {
Expand All @@ -31,14 +34,7 @@ class _MyHomeState extends ConsumerState<MyHome> {
}

String _filterLabel() {
switch (_selectedFilterIndex) {
case 1:
return '미완료된 할 일';
case 2:
return '완료된 할 일';
default:
return '전체 할 일';
}
return filters[_selectedFilterIndex].label;
}

@override
Expand Down Expand Up @@ -71,29 +67,30 @@ class _MyHomeState extends ConsumerState<MyHome> {
: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 16),
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
child: Center(
child: ToggleButtons(
isSelected: [
_selectedFilterIndex == 0,
_selectedFilterIndex == 1,
_selectedFilterIndex == 2
],
constraints: const BoxConstraints(
minHeight: 40.0, // 여기서 버튼의 최소 높이를 설정합니다.
minWidth: 80.0, // 버튼의 최소 너비를 설정할 수도 있습니다.
),
isSelected: filters
.map((filter) => filter.index == _selectedFilterIndex)
.toList(),
onPressed: (int index) {
_setFilter(index);
},
borderRadius: BorderRadius.circular(8),
selectedBorderColor: theme.primaryColor,
fillColor: theme.primaryColor.withOpacity(0.2),
constraints: const BoxConstraints(
minHeight: 40.0, // 여기서 버튼의 최소 높이를 설정합니다.
minWidth: 80.0, // 버튼의 최소 너비를 설정할 수도 있습니다.
),
children: const [
Text('전체'),
Text('미완료'),
Text('완료'),
],
children: filters
.map((filter) => Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16),
child: Text(filter.label),
))
.toList(),
),
),
),
Expand Down

0 comments on commit fa1b4ad

Please sign in to comment.