Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import MaterialApp and other widgets which we can use to quickly create a material app
- import 'package:flutter/material.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- void main() => runApp(new TodoApp());
- class TodoApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return new MaterialApp(
- title: 'Todo List',
- home: new TodoList(),
- theme: ThemeData(
- brightness: Brightness.light,
- primaryColor: Colors.red,
- ),
- darkTheme: ThemeData(
- brightness: Brightness.dark,
- ),
- );
- }
- }
- class TodoList extends StatefulWidget {
- @override
- createState() => new TodoListState();
- }
- class TodoListState extends State<TodoList> {
- List<String> _todoItems = [];
- // Instead of autogenerating a todo item, _addTodoItem now accepts a string
- void _addTodoItem(String task) {
- // Only add the task if the user actually entered something
- if (task.length > 0) {
- setState(() => _todoItems.add(task));
- }
- }
- // Build the whole list of todo items
- Widget _buildTodoList() {
- return new ListView.builder(
- itemBuilder: (context, index) {
- // itemBuilder will be automatically be called as many times as it takes for the
- // list to fill up its available space, which is most likely more than the
- // number of todo items we have. So, we need to check the index is OK.
- if (index < _todoItems.length) {
- return _buildTodoItem(_todoItems[index], index);
- }
- },
- );
- }
- // Build a single todo item
- Widget _buildTodoItem(String todoText, int index) {
- return new ListTile(
- title: new Text(todoText),
- onTap: () => _promptRemoveTodoItem(index),
- );
- }
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- appBar: new AppBar(title: new Text('Todo List')),
- body: _buildTodoList(),
- floatingActionButton: new FloatingActionButton(
- backgroundColor: Theme.of(context).primaryColor,
- onPressed: _pushAddTodoScreen,
- // pressing this button now opens the new screen
- tooltip: "Add task",
- child: new IconTheme(
- data: new IconThemeData(color: Colors.black87),
- child: new Icon(Icons.add),
- ),
- ),
- );
- }
- void _pushAddTodoScreen() {
- // Push this page onto the stack
- Navigator.of(context).push(
- // MaterialPageRoute will automatically animate the screen entry, as well
- // as adding a back button to close it
- new MaterialPageRoute(builder: (context) {
- return new Scaffold(
- appBar: new AppBar(title: new Text('Add a new task')),
- body: new TextField(
- autofocus: true,
- onSubmitted: (val) {
- _addTodoItem(val);
- Navigator.pop(context); // Close the add todo screen
- },
- decoration: new InputDecoration(
- hintText: 'Enter something to do...',
- contentPadding: const EdgeInsets.all(16.0)),
- ));
- }));
- }
- // Much like _addTodoItem, this modifies the array of todo strings and
- // notifies the app that the state has changed by using setState
- void _removeTodoItem(int index) {
- setState(() => _todoItems.removeAt(index));
- }
- // Show an alert dialog asking the user to confirm that the task is done
- void _promptRemoveTodoItem(int index) {
- showDialog(
- context: context,
- builder: (BuildContext context) {
- return new AlertDialog(
- title: new Text('Mark "${_todoItems[index]}" as done?'),
- actions: <Widget>[
- new FlatButton(
- child: new Text('CANCEL'),
- onPressed: () => Navigator.of(context).pop()),
- new FlatButton(
- child: new Text('MARK AS DONE'),
- onPressed: () {
- _removeTodoItem(index);
- Navigator.of(context).pop();
- })
- ]);
- });
- }
- @override
- void initState() {
- getSP();
- super.initState();
- }
- @override
- void deactivate() {
- setSP();
- super.deactivate();
- }
- void getSP() async {
- var prefs = await SharedPreferences.getInstance();
- _todoItems = prefs.getStringList("key") ?? _todoItems;
- }
- void setSP() async {
- var prefs = await SharedPreferences.getInstance();
- prefs.setStringList("key", _todoItems);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement