easytodo/lib/pages/home_page.dart
2024-03-27 21:45:41 +05:30

151 lines
4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:url_launcher/url_launcher.dart';
import '../data/database.dart';
import '../util/dialog_box.dart';
import '../util/todo_tile.dart';
Uri _url = Uri.parse('https://git.aiquiral.me/aiquiral/easytodo');
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _myBox = Hive.box('mybox');
ToDoDataBase db = ToDoDataBase();
@override
void initState() {
// initial data
if (_myBox.get("TODOLIST") == null) {
db.createInitialData();
} else {
// load data
db.loadData();
}
super.initState();
}
final _controller = TextEditingController();
void checkBoxChanged(bool? value, int index) {
setState(() {
db.toDoList[index][1] = !db.toDoList[index][1];
});
db.updateDataBase();
}
void saveNewTask() {
setState(() {
db.toDoList.add([_controller.text, false]);
_controller.clear();
});
Navigator.of(context).pop();
db.updateDataBase();
}
void createNewTask() {
showDialog(
context: context,
builder: (context) {
return DialogBox(
controller: _controller,
onSave: saveNewTask,
onCancel: () {
setState(() {
_controller.clear();
});
Navigator.of(context).pop();
},
);
},
);
}
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: SizedBox(
height: 100,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
Text(
"This application is made by Anuj Kaushik (a.k.a. Aiqurial).\nLicensed under AGPLv3."),
TextButton(
onPressed: _launchUrl,
child: Text("View Source Code"))
],
)));
});
})
]),
),
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),
);
},
),
);
}
}
Future<void> _launchUrl() async {
if (!await launchUrl(_url)) {
throw Exception('Could not launch $_url');
}
}