Advertisement
joaopaulofcc

Untitled

Sep 2nd, 2020
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.81 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.   Widget _buildTodoList() {
  27.     return new ListView.builder(
  28.       itemBuilder: (context, index) {
  29.         if (index < _todoItems.length) {
  30.           return _buildTodoItem(_todoItems[index]);
  31.         }
  32.       },
  33.     );
  34.   }
  35.  
  36.   Widget _buildTodoItem(String todoText) {
  37.     return new ListTile(title: new Text(todoText));
  38.   }
  39.  
  40.   Widget build(BuildContext context) {
  41.     return new Scaffold(
  42.       appBar: new AppBar(title: new Text('Todo List')),
  43.       body: _buildTodoList(),
  44.       floatingActionButton: new FloatingActionButton(
  45.           onPressed: _pushAddTodoScreen,
  46.           tooltip: 'Add task',
  47.           child: new Icon(Icons.add)),
  48.     );
  49.   }
  50.  
  51.   void _pushAddTodoScreen() {
  52.     Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
  53.       return new Scaffold(
  54.           appBar: new AppBar(title: new Text('Add a new task')),
  55.           body: new TextField(
  56.             autofocus: true,
  57.             onSubmitted: (val) {
  58.               _addTodoItem(val);
  59.               Navigator.pop(context); // Close the add todo screen
  60.             },
  61.             decoration: new InputDecoration(
  62.                 hintText: 'Enter something to do...',
  63.                 contentPadding: const EdgeInsets.all(16.0)),
  64.           ));
  65.     }));
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement