Advertisement
joaopaulofcc

Untitled

Sep 9th, 2020
1,369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.78 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() => runApp(new TodoApp());
  4.  
  5. class TodoApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return new MaterialApp(title: 'Lista de Tarefas', home: new TodoList());
  9.   }
  10. }
  11.  
  12. class TodoList extends StatefulWidget {
  13.   @override
  14.   createState() => new TodoListState();
  15. }
  16.  
  17. class TodoListState extends State<TodoList> {
  18.   List<String> _todoItems = [];
  19.  
  20.   void _addTodoItem(String task) {
  21.     if (task.length > 0) {
  22.       setState(() => _todoItems.add(task));
  23.     }
  24.   }
  25.  
  26.   void _removeTodoItem(int index) {
  27.     setState(() => _todoItems.removeAt(index));
  28.   }
  29.  
  30.   void _promptRemoveTodoItem(int index) {
  31.     showDialog(
  32.         context: context,
  33.         builder: (BuildContext context) {
  34.           return new AlertDialog(
  35.               title: new Text('Marcar "${_todoItems[index]}" como concluída?'),
  36.               actions: <Widget>[
  37.                 new FlatButton(
  38.                     child: new Text('CANCELAR'),
  39.                     onPressed: () => Navigator.of(context).pop()),
  40.                 new FlatButton(
  41.                     child: new Text('MARCAR COMO CONCLUÍDA'),
  42.                     onPressed: () {
  43.                       _removeTodoItem(index);
  44.                       Navigator.of(context).pop();
  45.                     })
  46.               ]);
  47.         });
  48.   }
  49.  
  50.   Widget _buildTodoList() {
  51.     return new ListView.builder(
  52.       itemBuilder: (context, index) {
  53.         if (index < _todoItems.length) {
  54.           return _buildTodoItem(_todoItems[index], index);
  55.         }
  56.       },
  57.     );
  58.   }
  59.  
  60.   Widget _buildTodoItem(String todoText, int index) {
  61.     return new ListTile(
  62.         title: new Text(todoText), onTap: () => _promptRemoveTodoItem(index));
  63.   }
  64.  
  65.   @override
  66.   Widget build(BuildContext context) {
  67.     return new Scaffold(
  68.       appBar: new AppBar(
  69.           title: new Text('Lista de tarefas'), backgroundColor: Colors.red),
  70.       body: _buildTodoList(),
  71.       floatingActionButton: new FloatingActionButton(
  72.           onPressed: _pushAddTodoScreen,
  73.           tooltip: 'Adiciona tarefa',
  74.           child: new Icon(Icons.add)),
  75.     );
  76.   }
  77.  
  78.   void _pushAddTodoScreen() {
  79.     Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
  80.       return new Scaffold(
  81.           appBar: new AppBar(
  82.               title: new Text('Adicionar uma tarefa'),
  83.               backgroundColor: Colors.red),
  84.           body: new TextField(
  85.             autofocus: true,
  86.             onSubmitted: (val) {
  87.               _addTodoItem(val);
  88.               Navigator.pop(context);
  89.             },
  90.             decoration: new InputDecoration(
  91.                 hintText: 'Digite uma tarefa...',
  92.                 contentPadding: const EdgeInsets.all(16.0)),
  93.           ));
  94.     }));
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement