easytodo/lib/pages/home_page.dart

151 lines
4 KiB
Dart
Raw Normal View History

2024-03-27 06:55:26 +00:00
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
2024-03-27 16:15:41 +00:00
import 'package:url_launcher/url_launcher.dart';
2024-03-27 06:55:26 +00:00
import '../data/database.dart';
import '../util/dialog_box.dart';
import '../util/todo_tile.dart';
2024-03-27 16:15:41 +00:00
Uri _url = Uri.parse('https://git.aiquiral.me/aiquiral/easytodo');
2024-03-27 06:55:26 +00:00
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() {
2024-03-27 16:15:41 +00:00
// initial data
2024-03-27 06:55:26 +00:00
if (_myBox.get("TODOLIST") == null) {
db.createInitialData();
} else {
2024-03-27 16:15:41 +00:00
// load data
2024-03-27 06:55:26 +00:00
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,
2024-03-27 16:15:41 +00:00
onCancel: () {
setState(() {
_controller.clear();
});
Navigator.of(context).pop();
},
2024-03-27 06:55:26 +00:00
);
},
);
}
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(
2024-03-27 16:15:41 +00:00
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"))
],
)));
2024-03-27 06:55:26 +00:00
});
})
]),
),
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),
);
},
),
);
}
}
2024-03-27 16:15:41 +00:00
Future<void> _launchUrl() async {
if (!await launchUrl(_url)) {
throw Exception('Could not launch $_url');
}
}