Skip to content

Instantly share code, notes, and snippets.

@asialgearoid
Created May 30, 2018 03:20
Show Gist options
  • Save asialgearoid/ad09922b8c8332204056e6ac8acb9444 to your computer and use it in GitHub Desktop.
Save asialgearoid/ad09922b8c8332204056e6ac8acb9444 to your computer and use it in GitHub Desktop.
Todo App Step 4 (User Interaction)
// Instead of autogenerating a todo item, _addTodoItem now accepts a string
void _addTodoItem(String task) {
// Only add the task if the user actually entered something
if(task.length > 0) {
setState(() => _todoItems.add(task));
}
}
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Todo List')
),
body: _buildTodoList(),
floatingActionButton: new FloatingActionButton(
onPressed: _pushAddTodoScreen, // pressing this button now opens the new screen
tooltip: 'Add task',
child: new Icon(Icons.add)
),
);
}
void _pushAddTodoScreen() {
// Push this page onto the stack
Navigator.of(context).push(
// MaterialPageRoute will automatically animate the screen entry, as well
// as adding a back button to close it
new MaterialPageRoute(
builder: (context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Add a new task')
),
body: new TextField(
autofocus: true,
onSubmitted: (val) {
_addTodoItem(val);
Navigator.pop(context); // Close the add todo screen
},
decoration: new InputDecoration(
hintText: 'Enter something to do...',
contentPadding: const EdgeInsets.all(16.0)
),
)
);
}
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment