To-do List: Tick It Done
Part of the Projects section of Coddy's Flutter journey. Lesson 3 of 36.
A task needs to remember whether it is done. Give the class a second field that starts as false and can change, so it is not final:
class Task {
Task(this.title);
final String title;
bool done = false;
}Each row gets a Checkbox as its leading: widget. Its value is the task's done, and its onChanged sets done inside setState.
A done task should look done. TextDecoration.lineThrough draws a line through the text, and a grey color makes it step back. When the task is not done, the style is null and the text keeps the theme's look:
style: task.done
? const TextStyle(decoration: TextDecoration.lineThrough, color: Colors.grey)
: null,Challenge
MediumTick tasks done:
- add
bool done = false;toTask - give each
ListTilealeading:Checkboxthat shows and changes the task'sdone - when a task is done, cross its title out and make it
Colors.grey
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
),
home: const TodoScreen(),
);
}
}
class Task {
Task(this.title);
final String title;
}
class TodoScreen extends StatefulWidget {
const TodoScreen({super.key});
@override
State<TodoScreen> createState() => _TodoScreenState();
}
class _TodoScreenState extends State<TodoScreen> {
final List<Task> _tasks = [Task('Buy milk'), Task('Walk the dog'), Task('Read a book')];
final _controller = TextEditingController();
void _addTask() {
setState(() {
_tasks.add(Task(_controller.text));
});
_controller.clear();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('My Tasks')),
body: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
child: Row(
children: [
Expanded(
child: TextField(
controller: _controller,
decoration: const InputDecoration(
labelText: 'New task',
border: OutlineInputBorder(),
),
),
),
const SizedBox(width: 8),
IconButton.filled(
onPressed: _addTask,
icon: const Icon(Icons.add),
),
],
),
),
Expanded(
child: ListView.builder(
itemCount: _tasks.length,
itemBuilder: (context, index) {
final task = _tasks[index];
return ListTile(
title: Text(task.title),
);
},
),
),
],
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
Practice on your own: Online Flutter compiler