Created
June 14, 2022 09:27
-
-
Save FlutterWiz/9d00cb57d524c373aa98b466651c1f6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
import 'package:todo_list_riverpod/application/todo/todo_event.dart'; | |
import 'package:todo_list_riverpod/application/todo/todo_state.dart'; | |
import 'package:todo_list_riverpod/domain/todo_model.dart'; | |
import 'package:uuid/uuid.dart'; | |
class TodoNotifier extends StateNotifier<TodoState> { | |
TodoNotifier() : super(TodoState.empty()); | |
final uuid = const Uuid(); | |
mapEventsToStates(TodoEvent event) async { | |
return event.map( | |
todoTitleChanged: (todoTitleChangedEvent) { | |
state = state.copyWith( | |
todo: TodoModel( | |
id: state.todo.id, | |
title: todoTitleChangedEvent.text.trimLeft(), | |
isTodoCompleted: false, | |
), | |
); | |
}, | |
todoStatusChanged: (todoStatusChangedEvent) { | |
final selectedTodo = state.todoList.where((element) => element.id == todoStatusChangedEvent.todoId).single; | |
final todolist = [...state.todoList]; | |
todolist[todolist.indexWhere((element) => element.id == selectedTodo.id)] = | |
TodoModel(id: selectedTodo.id, title: selectedTodo.title, isTodoCompleted: !selectedTodo.isTodoCompleted); | |
state = state.copyWith(todoList: todolist); | |
}, | |
addTodo: (addTodoEvent) { | |
final List<TodoModel> todoList = [...state.todoList]; | |
todoList.add( | |
TodoModel( | |
id: uuid.v1(), | |
title: state.todo.title, | |
isTodoCompleted: false, | |
), | |
); | |
state = state.copyWith(todoList: todoList); | |
}, | |
removeTodo: (removeTodoEvent) { | |
final List<TodoModel> todoList = [...state.todoList]; | |
final todoId = removeTodoEvent.todoId; | |
todoList.removeWhere((element) => element.id == todoId); | |
state = state.copyWith(todoList: todoList); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment