Created
April 22, 2020 20:59
-
-
Save Bruising6802/eb77de37a94458f0f4375dd56c053591 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'todo_class.dart'; | |
class TodoScreen extends StatefulWidget { | |
@override | |
_TodoScreenState createState() => _TodoScreenState(); | |
} | |
class _TodoScreenState extends State<TodoScreen> { | |
List<ToDo> tasksTodo; | |
final GlobalKey<AnimatedListState> _listKey = GlobalKey(); | |
final ToDoDatabase database = ToDoDatabase(); | |
void showTextfield() { | |
String titleInput; | |
showModalBottomSheet( | |
context: context, | |
builder: (context) => Padding( | |
padding: MediaQuery.of(context).viewInsets, | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: TextField( | |
autofocus: true, | |
decoration: InputDecoration( | |
suffixIcon: IconButton( | |
icon: Icon(Icons.send), | |
onPressed: () { | |
Navigator.pop(context); | |
_insertSingleItem('$titleInput'); | |
})), | |
onChanged: (String value) { | |
setState(() { | |
titleInput = value; | |
}); | |
}, | |
)), | |
)); | |
} | |
void _insertSingleItem(String title) { | |
int insertIndex = 0; | |
tasksTodo.insert( | |
insertIndex, | |
ToDo( | |
title: "$title", | |
description: "", | |
id: DateTime.now().millisecondsSinceEpoch, | |
complete: false.toString())); | |
_listKey.currentState.insertItem(insertIndex); | |
tasksTodo.sort((a, b) => b.id.compareTo(a.id)); | |
database.deleteAll(); | |
for (ToDo data in tasksTodo) { | |
database.addTodo(data); | |
} | |
} | |
void _removeSingleItems(int index) { | |
int removeIndex = index; | |
AnimatedListRemovedItemBuilder builder = (context, animation) { | |
return item(animation, removeIndex); | |
}; | |
_listKey.currentState.removeItem(removeIndex, builder); | |
tasksTodo.removeAt(removeIndex); | |
} | |
void fetchTodos() async { | |
var allTasks = await database.fetchAll(); | |
setState(() { | |
tasksTodo = allTasks; | |
}); | |
} | |
@override | |
void initState() { | |
database.initialize(); | |
fetchTodos(); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
database.dispose(); | |
super.dispose(); | |
} | |
Widget item(Animation animation, int index) { | |
return FadeTransition( | |
child: ListTile( | |
title: Text("${tasksTodo[index].title}"), | |
onTap: () {}, | |
leading: IconButton( | |
icon: Icon(Icons.delete), | |
onPressed: () { | |
_removeSingleItems(index); | |
}), | |
trailing: Checkbox( | |
value: tasksTodo[index].complete == true.toString(), | |
onChanged: (bool value) { | |
setState(() { | |
tasksTodo[index].complete = value.toString(); | |
}); | |
if (tasksTodo[index].complete == true.toString()) { | |
print('lol'); | |
} | |
})), | |
opacity: animation, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Tasks"), | |
), | |
body: FutureBuilder( | |
future: database.fetchAll(), | |
builder: (context, snapshot) { | |
return AnimatedSwitcher( | |
duration: Duration(milliseconds: 250), | |
child: snapshot.hasData | |
? AnimatedList( | |
key: _listKey, | |
initialItemCount: tasksTodo.length, | |
itemBuilder: (context, index, animation) { | |
return item(animation, index); | |
}) | |
: Center(child: CircularProgressIndicator()), | |
); | |
}), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), onPressed: showTextfield), | |
); | |
} | |
} | |
// ToDo Class | |
class ToDo { | |
int id; | |
String title; | |
String description; | |
String complete; | |
ToDo({this.title, this.description, this.id, this.complete}); | |
Map<String, dynamic> toMapp() { | |
return { | |
'title': title, | |
'description': description, | |
'id': id, | |
'complete': complete | |
}; | |
} | |
ToDo.fromMapp(Map<String, dynamic> map) | |
: title = map['title'], | |
description = map['description'], | |
id = map['id'], | |
complete = map['complete']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment