Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() => runApp(new TodoApp());
- class TodoApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return new MaterialApp(title: 'Lista de Tarefas', home: new TodoList());
- }
- }
- class TodoList extends StatefulWidget {
- @override
- createState() => new TodoListState();
- }
- class TodoListState extends State<TodoList> {
- List<String> _todoItems = [];
- void _addTodoItem(String task) {
- if (task.length > 0) {
- setState(() => _todoItems.add(task));
- }
- }
- void _removeTodoItem(int index) {
- setState(() => _todoItems.removeAt(index));
- }
- void _promptRemoveTodoItem(int index) {
- showDialog(
- context: context,
- builder: (BuildContext context) {
- return new AlertDialog(
- title: new Text('Marcar "${_todoItems[index]}" como concluída?'),
- actions: <Widget>[
- new FlatButton(
- child: new Text('CANCELAR'),
- onPressed: () => Navigator.of(context).pop()),
- new FlatButton(
- child: new Text('MARCAR COMO CONCLUÍDA'),
- 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));
- }
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- appBar: new AppBar(
- title: new Text('Lista de tarefas'), backgroundColor: Colors.red),
- body: _buildTodoList(),
- floatingActionButton: new FloatingActionButton(
- onPressed: _pushAddTodoScreen,
- tooltip: 'Adiciona tarefa',
- child: new Icon(Icons.add)),
- );
- }
- void _pushAddTodoScreen() {
- Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
- return new Scaffold(
- appBar: new AppBar(
- title: new Text('Adicionar uma tarefa'),
- backgroundColor: Colors.red),
- body: new TextField(
- autofocus: true,
- onSubmitted: (val) {
- _addTodoItem(val);
- Navigator.pop(context);
- },
- decoration: new InputDecoration(
- hintText: 'Digite uma tarefa...',
- contentPadding: const EdgeInsets.all(16.0)),
- ));
- }));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement