import 'package:flutter/material.dart'; import 'package:todo/database/PathProvider.dart'; import '../database/Sqlite.dart'; import 'ToDoItem.dart'; class ToDoProvider with ChangeNotifier { final List _toDoList = []; final safeHelper = Sqlite(); List get toDoList => _toDoList; ToDoProvider() { _loadFromPrefs(); } Future addToDo(ToDoItem item) async { await safeHelper.insertToDoItem(item); _toDoList.add(item); notifyListeners(); } void updateStatus(int id, String status) { ToDoItem item = _toDoList.firstWhere((todo) => todo.id == id); item.status = status; safeHelper.updateToDoItem(item); notifyListeners(); } void deleteItem(int id) { _toDoList.remove(_toDoList.where((toDoID) => toDoID.id == id).first); safeHelper.deleteToDoItem(id); notifyListeners(); } Future _loadFromPrefs() async { List items = await safeHelper.getToDoItems(); for (var item in items) { _toDoList.add(item); } notifyListeners(); } void sortByName() { _toDoList.sort((a, b) => a.name.compareTo(b.name)); notifyListeners(); } void sortByDueDate() { _toDoList.sort((a, b) => a.dueDate.compareTo(b.dueDate)); notifyListeners(); } void sortByStatus() { _toDoList.sort((a, b) => a.status.compareTo(b.status)); notifyListeners(); } }