Skip to content

Instantly share code, notes, and snippets.

@asialgearoid
Created May 30, 2018 03:21
Show Gist options
  • Save asialgearoid/fe8f74f00c2f8d0aa10de6c0a3d059ed to your computer and use it in GitHub Desktop.
Save asialgearoid/fe8f74f00c2f8d0aa10de6c0a3d059ed to your computer and use it in GitHub Desktop.
Todo App Step 5 (Removing an Item)
// Much like _addTodoItem, this modifies the array of todo strings and
// notifies the app that the state has changed by using setState
void _removeTodoItem(int index) {
setState(() => _todoItems.removeAt(index));
}
// Show an alert dialog asking the user to confirm that the task is done
void _promptRemoveTodoItem(int index) {
showDialog(
context: context,
builder: (BuildContext context) {
return new AlertDialog(
title: new Text('Mark "${_todoItems[index]}" as done?'),
actions: <Widget>[
new FlatButton(
child: new Text('CANCEL'),
onPressed: () => Navigator.of(context).pop()
),
new FlatButton(
child: new Text('MARK AS DONE'),
onPressed: () {
_removeTodoItem(index);
Navigator.of(context).pop();
}
)
]
);
}
);
}
Widget _buildTodoList() {
return new ListView.builder(
itemBuilder: (context, index) {
if(index < _todoItems.length) {
return _buildTodoItem(_todoItems[index], index);
}
},
);
}
Widget _buildTodoItem(String todoText, int index) {
return new ListTile(
title: new Text(todoText),
onTap: () => _promptRemoveTodoItem(index)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment