131 lines
3.2 KiB
Dart
131 lines
3.2 KiB
Dart
|
import 'package:flutter/material.dart';
|
||
|
import 'package:hive_flutter/hive_flutter.dart';
|
||
|
import '../data/database.dart';
|
||
|
import '../util/dialog_box.dart';
|
||
|
import '../util/todo_tile.dart';
|
||
|
|
||
|
class HomePage extends StatefulWidget {
|
||
|
const HomePage({super.key});
|
||
|
|
||
|
@override
|
||
|
State<HomePage> createState() => _HomePageState();
|
||
|
}
|
||
|
|
||
|
class _HomePageState extends State<HomePage> {
|
||
|
// reference the hive box
|
||
|
final _myBox = Hive.box('mybox');
|
||
|
ToDoDataBase db = ToDoDataBase();
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
// if this is the 1st time ever openin the app, then create default data
|
||
|
if (_myBox.get("TODOLIST") == null) {
|
||
|
db.createInitialData();
|
||
|
} else {
|
||
|
// there already exists data
|
||
|
db.loadData();
|
||
|
}
|
||
|
|
||
|
super.initState();
|
||
|
}
|
||
|
|
||
|
// text controller
|
||
|
final _controller = TextEditingController();
|
||
|
|
||
|
// checkbox was tapped
|
||
|
void checkBoxChanged(bool? value, int index) {
|
||
|
setState(() {
|
||
|
db.toDoList[index][1] = !db.toDoList[index][1];
|
||
|
});
|
||
|
db.updateDataBase();
|
||
|
}
|
||
|
|
||
|
// save new task
|
||
|
void saveNewTask() {
|
||
|
setState(() {
|
||
|
db.toDoList.add([_controller.text, false]);
|
||
|
_controller.clear();
|
||
|
});
|
||
|
Navigator.of(context).pop();
|
||
|
db.updateDataBase();
|
||
|
}
|
||
|
|
||
|
// create a new task
|
||
|
void createNewTask() {
|
||
|
showDialog(
|
||
|
context: context,
|
||
|
builder: (context) {
|
||
|
return DialogBox(
|
||
|
controller: _controller,
|
||
|
onSave: saveNewTask,
|
||
|
onCancel: () => Navigator.of(context).pop(),
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// delete task
|
||
|
void deleteTask(int index) {
|
||
|
setState(() {
|
||
|
db.toDoList.removeAt(index);
|
||
|
});
|
||
|
db.updateDataBase();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
backgroundColor: Colors.white,
|
||
|
appBar: AppBar(
|
||
|
backgroundColor: Colors.deepPurple,
|
||
|
title:
|
||
|
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
|
||
|
const Text(
|
||
|
"To-Do List",
|
||
|
style: TextStyle(
|
||
|
fontWeight: FontWeight.w500,
|
||
|
color: Colors.white,
|
||
|
),
|
||
|
),
|
||
|
IconButton(
|
||
|
iconSize: 72,
|
||
|
icon: const Icon(
|
||
|
Icons.info,
|
||
|
color: Colors.white,
|
||
|
size: 30,
|
||
|
),
|
||
|
onPressed: () {
|
||
|
showDialog(
|
||
|
context: context,
|
||
|
builder: (BuildContext context) {
|
||
|
return const AlertDialog(
|
||
|
title: Text("About"),
|
||
|
content: Text("Made by Anuj K."),
|
||
|
);
|
||
|
});
|
||
|
})
|
||
|
]),
|
||
|
),
|
||
|
floatingActionButton: FloatingActionButton(
|
||
|
onPressed: createNewTask,
|
||
|
backgroundColor: Colors.deepPurple,
|
||
|
child: const Icon(
|
||
|
Icons.add,
|
||
|
color: Colors.white,
|
||
|
),
|
||
|
),
|
||
|
body: ListView.builder(
|
||
|
itemCount: db.toDoList.length,
|
||
|
itemBuilder: (context, index) {
|
||
|
return ToDoTile(
|
||
|
taskName: db.toDoList[index][0],
|
||
|
taskCompleted: db.toDoList[index][1],
|
||
|
onChanged: (value) => checkBoxChanged(value, index),
|
||
|
deleteFunction: (context) => deleteTask(index),
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|