Advertisement
joaopaulofcc

Untitled

Sep 2nd, 2020
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.89 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:
  46.               _pushAddTodoScreen, // pressing this button now opens the new screen
  47.           tooltip: 'Add task',
  48.           child: new Icon(Icons.add)),
  49.     );
  50.   }
  51.  
  52.   void _pushAddTodoScreen()
  53.   {
  54.     Navigator.of(context).push(new MaterialPageRoute(builder: (context)
  55.     {
  56.       return new Scaffold(
  57.           appBar: new AppBar(title: new Text('Add a new task')),
  58.           body: new TextField(
  59.             autofocus: true,
  60.             onSubmitted: (val)
  61.             {
  62.               _addTodoItem(val);
  63.               Navigator.pop(context); // Close the add todo screen
  64.             },
  65.             decoration: new InputDecoration(
  66.                 hintText: 'Enter something to do...',
  67.                 contentPadding: const EdgeInsets.all(16.0)),
  68.           ));
  69.     }));
  70.   }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement