Learn
← Previous Next →

Hari 27: Mini Project — Todo App

60 min Last updated 09 Apr 2026

Todo App Lengkap

Kita buat Todo App dengan fitur: tambah, selesaikan, hapus, filter, dan statistik.

import "package:flutter/material.dart";

enum FilterTodo { semua, aktif, selesai }

class Todo {
  final int id;
  String judul;
  bool selesai;
  final DateTime dibuat;

  Todo(this.id, this.judul) : selesai = false, dibuat = DateTime.now();
}

class TodoProvider extends ChangeNotifier {
  List _todos = [];
  FilterTodo _filter = FilterTodo.semua;
  int _nextId = 1;

  FilterTodo get filter => _filter;
  int get total => _todos.length;
  int get selesaiCount => _todos.where((t) => t.selesai).length;
  int get aktifCount => _todos.where((t) => !t.selesai).length;

  List get filteredTodos {
    switch (_filter) {
      case FilterTodo.aktif: return _todos.where((t) => !t.selesai).toList();
      case FilterTodo.selesai: return _todos.where((t) => t.selesai).toList();
      default: return List.from(_todos);
    }
  }

  void tambah(String judul) {
    if (judul.trim().isEmpty) return;
    _todos.add(Todo(_nextId++, judul.trim()));
    notifyListeners();
  }

  void toggle(int id) {
    final todo = _todos.firstWhere((t) => t.id == id);
    todo.selesai = !todo.selesai;
    notifyListeners();
  }

  void hapus(int id) {
    _todos.removeWhere((t) => t.id == id);
    notifyListeners();
  }

  void setFilter(FilterTodo f) {
    _filter = f;
    notifyListeners();
  }
}

💡 Notice: firstWhere() mencari elemen pertama yang memenuhi kondisi. Akan throw StateError jika tidak ditemukan.

Assignment

Buat TodoApp lengkap dengan tambah, toggle, hapus, filter, dan statistik.

Expected output:

=== Todo (filter: semua) ===
[ ] Belajar Flutter (id: 1)
[ ] Buat project (id: 2)
[ ] Deploy ke Play Store (id: 3)
Total: 3, Selesai: 0
Setelah selesaikan id 1:
=== Todo (filter: semua) ===
[✓] Belajar Flutter (id: 1)
[ ] Buat project (id: 2)
[ ] Deploy ke Play Store (id: 3)
Total: 3, Selesai: 1
Filter aktif:
=== Todo (filter: aktif) ===
[ ] Buat project (id: 2)
[ ] Deploy ke Play Store (id: 3)
Total: 3, Selesai: 1
Setelah hapus id 2:
=== Todo (filter: semua) ===
[✓] Belajar Flutter (id: 1)
[ ] Deploy ke Play Store (id: 3)
Total: 2, Selesai: 1
Dart main.dart
Solution
Output