Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 25, 2020 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/aecd442c876eedd56d4ba0319e919adc to your computer and use it in GitHub Desktop.
Save velotiotech/aecd442c876eedd56d4ba0319e919adc to your computer and use it in GitHub Desktop.
/// State is composed all the variables declared in the State implementation of a Stateful widget
class TodoListState extends State<TodoList> {
final List<Todo> todos = List<Todo>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Todo'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: todos.length > 0
? ListView.builder(
itemCount: todos.length,
itemBuilder: _buildRow,
)
: Text('There is nothing here yet. Start by adding some Todos'),
),
);
}
/// build a single row of the list
Widget _buildRow(context, index) => Row(
children: <Widget>[
Checkbox(
value: todos[index].completed,
onChanged: (value) => _changeTodo(index, value)),
Text(todos[index].label,
style: TextStyle(
decoration: todos[index].completed
? TextDecoration.lineThrough
: null))
],
);
/// toggle the completed state of a todo item
_changeTodo(int index, bool value) =>
setState(() => todos[index].completed = value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment