Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.53 KB | None | 0 0
  1. // Import MaterialApp and other widgets which we can use to quickly create a material app
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5.  
  6. void main() => runApp(new TodoApp());
  7.  
  8. class TodoApp extends StatelessWidget {
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return new MaterialApp(
  12.       title: 'Todo List',
  13.       home: new TodoList(),
  14.       theme: ThemeData(
  15.         brightness: Brightness.light,
  16.         primaryColor: Colors.red,
  17.       ),
  18.       darkTheme: ThemeData(
  19.         brightness: Brightness.dark,
  20.       ),
  21.     );
  22.   }
  23. }
  24.  
  25. class TodoList extends StatefulWidget {
  26.   @override
  27.   createState() => new TodoListState();
  28. }
  29.  
  30. class TodoListState extends State<TodoList> {
  31.   List<String> _todoItems = [];
  32.  
  33.   // Instead of autogenerating a todo item, _addTodoItem now accepts a string
  34.   void _addTodoItem(String task) {
  35.     // Only add the task if the user actually entered something
  36.     if (task.length > 0) {
  37.       setState(() => _todoItems.add(task));
  38.     }
  39.   }
  40.  
  41.   // Build the whole list of todo items
  42.   Widget _buildTodoList() {
  43.     return new ListView.builder(
  44.       itemBuilder: (context, index) {
  45.         // itemBuilder will be automatically be called as many times as it takes for the
  46.         // list to fill up its available space, which is most likely more than the
  47.         // number of todo items we have. So, we need to check the index is OK.
  48.         if (index < _todoItems.length) {
  49.           return _buildTodoItem(_todoItems[index], index);
  50.         }
  51.       },
  52.     );
  53.   }
  54.  
  55.   // Build a single todo item
  56.   Widget _buildTodoItem(String todoText, int index) {
  57.     return new ListTile(
  58.       title: new Text(todoText),
  59.       onTap: () => _promptRemoveTodoItem(index),
  60.     );
  61.   }
  62.  
  63.   @override
  64.   Widget build(BuildContext context) {
  65.     return new Scaffold(
  66.       appBar: new AppBar(title: new Text('Todo List')),
  67.       body: _buildTodoList(),
  68.       floatingActionButton: new FloatingActionButton(
  69.         backgroundColor: Theme.of(context).primaryColor,
  70.         onPressed: _pushAddTodoScreen,
  71.         // pressing this button now opens the new screen
  72.         tooltip: "Add task",
  73.         child: new IconTheme(
  74.           data: new IconThemeData(color: Colors.black87),
  75.           child: new Icon(Icons.add),
  76.         ),
  77.       ),
  78.     );
  79.   }
  80.  
  81.   void _pushAddTodoScreen() {
  82.     // Push this page onto the stack
  83.     Navigator.of(context).push(
  84.         // MaterialPageRoute will automatically animate the screen entry, as well
  85.         // as adding a back button to close it
  86.         new MaterialPageRoute(builder: (context) {
  87.       return new Scaffold(
  88.           appBar: new AppBar(title: new Text('Add a new task')),
  89.           body: new TextField(
  90.             autofocus: true,
  91.             onSubmitted: (val) {
  92.               _addTodoItem(val);
  93.               Navigator.pop(context); // Close the add todo screen
  94.             },
  95.             decoration: new InputDecoration(
  96.                 hintText: 'Enter something to do...',
  97.                 contentPadding: const EdgeInsets.all(16.0)),
  98.           ));
  99.     }));
  100.   }
  101.  
  102.   // Much like _addTodoItem, this modifies the array of todo strings and
  103.   // notifies the app that the state has changed by using setState
  104.   void _removeTodoItem(int index) {
  105.     setState(() => _todoItems.removeAt(index));
  106.   }
  107.  
  108.   // Show an alert dialog asking the user to confirm that the task is done
  109.   void _promptRemoveTodoItem(int index) {
  110.     showDialog(
  111.         context: context,
  112.         builder: (BuildContext context) {
  113.           return new AlertDialog(
  114.               title: new Text('Mark "${_todoItems[index]}" as done?'),
  115.               actions: <Widget>[
  116.                 new FlatButton(
  117.                     child: new Text('CANCEL'),
  118.                     onPressed: () => Navigator.of(context).pop()),
  119.                 new FlatButton(
  120.                     child: new Text('MARK AS DONE'),
  121.                     onPressed: () {
  122.                       _removeTodoItem(index);
  123.                       Navigator.of(context).pop();
  124.                     })
  125.               ]);
  126.         });
  127.   }
  128.  
  129.   @override
  130.   void initState() {
  131.     getSP();
  132.     super.initState();
  133.   }
  134.  
  135.   @override
  136.   void deactivate() {
  137.     setSP();
  138.     super.deactivate();
  139.   }
  140.  
  141.   void getSP() async {
  142.     var prefs = await SharedPreferences.getInstance();
  143.     _todoItems = prefs.getStringList("key") ?? _todoItems;
  144.   }
  145.  
  146.   void setSP() async {
  147.     var prefs = await SharedPreferences.getInstance();
  148.     prefs.setStringList("key", _todoItems);
  149.   }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement