Advertisement
joaopaulofcc

Untitled

Sep 2nd, 2020
1,692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.64 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: 'Todo List', 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('Mark "${_todoItems[index]}" as done?'),
  36.               actions: <Widget>[
  37.                 new FlatButton(
  38.                     child: new Text('CANCEL'),
  39.                     onPressed: () => Navigator.of(context).pop()),
  40.                 new FlatButton(
  41.                     child: new Text('MARK AS DONE'),
  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(title: new Text('Todo List')),
  69.       body: _buildTodoList(),
  70.       floatingActionButton: new FloatingActionButton(
  71.           onPressed: _pushAddTodoScreen,
  72.           tooltip: 'Add task',
  73.           child: new Icon(Icons.add)),
  74.     );
  75.   }
  76.  
  77.   void _pushAddTodoScreen() {
  78.     Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
  79.       return new Scaffold(
  80.           appBar: new AppBar(title: new Text('Add a new task')),
  81.           body: new TextField(
  82.             autofocus: true,
  83.             onSubmitted: (val) {
  84.               _addTodoItem(val);
  85.               Navigator.pop(context);
  86.             },
  87.             decoration: new InputDecoration(
  88.                 hintText: 'Enter something to do...',
  89.                 contentPadding: const EdgeInsets.all(16.0)),
  90.           ));
  91.     }));
  92.   }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement