hive implementation

save todo in hive
first release
This commit is contained in:
2023-09-29 11:22:28 +02:00
parent 37719bd696
commit 979fcfa2a1
8 changed files with 631 additions and 98 deletions

View File

@@ -1,8 +1,13 @@
import 'package:flutter/material.dart';
import 'package:todo/utils/dialog_box.dart';
import 'package:todo/utils/todo_tile.dart';
import 'package:todo/pages/home_page.dart';
import 'package:hive_flutter/hive_flutter.dart';
void main() async {
//init hive
await Hive.initFlutter();
//open the box
var box = await Hive.openBox('todoBox');
void main() {
runApp(const MyApp());
}
@@ -22,75 +27,7 @@ class _MyAppState extends State<MyApp> {
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _controller = TextEditingController();
List todoList = [
["Make Tutorial", true],
["Buy car", false],
["Buy new wife", true],
["buy card", false],
];
void checkBoxChanged(bool? value, int index) {
setState(() {
todoList[index][1] = !todoList[index][1];
});
}
void saveNewTask() {
setState(() {
todoList.add([_controller.text, false]);
_controller.clear();
});
Navigator.of(context).pop();
}
void createNewTask() {
showDialog(
context: context,
builder: (context) {
return DialogBox(
controller: _controller,
onCancel: () => Navigator.of(context).pop(),
onSave: saveNewTask,
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
itemCount: todoList.length,
itemBuilder: (context, index) {
return ToDoTile(
taskName: todoList[index][0],
taskCompleted: todoList[index][1],
onChanged: (value) => checkBoxChanged(value, index),
);
}),
floatingActionButton: FloatingActionButton(
onPressed: createNewTask,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
home: const MyHomePage(title: 'Ma liste de tache'),
);
}
}