hive implementation
save todo in hive first release
This commit is contained in:
27
lib/data/database.dart
Normal file
27
lib/data/database.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
|
||||
class ToDoDataBase {
|
||||
List todoList = [];
|
||||
|
||||
//reference hive todo box
|
||||
final _myBox = Hive.box('todoBox');
|
||||
|
||||
//init db
|
||||
void createInitialData() {
|
||||
todoList = [
|
||||
["Make Tutorial", true],
|
||||
["Buy car", false],
|
||||
["Buy new wife", true],
|
||||
["buy card", false],
|
||||
];
|
||||
}
|
||||
|
||||
//load data
|
||||
void loadData() {
|
||||
todoList = _myBox.get('todos');
|
||||
}
|
||||
|
||||
void updateData() {
|
||||
_myBox.put('todos', todoList);
|
||||
}
|
||||
}
|
||||
@@ -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'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
95
lib/pages/home_page.dart
Normal file
95
lib/pages/home_page.dart
Normal file
@@ -0,0 +1,95 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:todo/data/database.dart';
|
||||
import 'package:todo/utils/dialog_box.dart';
|
||||
import 'package:todo/utils/todo_tile.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
|
||||
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> {
|
||||
//reference hive todo box
|
||||
final _myBox = Hive.box('todoBox');
|
||||
|
||||
ToDoDataBase db = ToDoDataBase();
|
||||
|
||||
final _controller = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
// TODO: implement initState
|
||||
// first time
|
||||
if (_myBox.get('todos') == null) {
|
||||
db.createInitialData();
|
||||
} else {
|
||||
db.loadData();
|
||||
}
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void checkBoxChanged(bool? value, int index) {
|
||||
setState(() {
|
||||
db.todoList[index][1] = !db.todoList[index][1];
|
||||
});
|
||||
db.updateData();
|
||||
}
|
||||
|
||||
void saveNewTask() {
|
||||
setState(() {
|
||||
db.todoList.add([_controller.text, false]);
|
||||
_controller.clear();
|
||||
});
|
||||
db.updateData();
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
void createNewTask() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return DialogBox(
|
||||
controller: _controller,
|
||||
onCancel: () => Navigator.of(context).pop(),
|
||||
onSave: saveNewTask,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void deleteTask(int index) {
|
||||
setState(() {
|
||||
db.todoList.removeAt(index);
|
||||
});
|
||||
db.updateData();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.title),
|
||||
),
|
||||
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),
|
||||
);
|
||||
}),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: createNewTask,
|
||||
tooltip: 'Increment',
|
||||
child: const Icon(Icons.add),
|
||||
), // This trailing comma makes auto-formatting nicer for build methods.
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +1,59 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
|
||||
class ToDoTile extends StatelessWidget {
|
||||
const ToDoTile(
|
||||
{Key? key,
|
||||
required this.taskName,
|
||||
required this.taskCompleted,
|
||||
required this.onChanged})
|
||||
required this.onChanged,
|
||||
required this.deleteFunction})
|
||||
: super(key: key);
|
||||
|
||||
final String taskName;
|
||||
final bool taskCompleted;
|
||||
final Function(bool?)? onChanged;
|
||||
final Function(BuildContext)? deleteFunction;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(25.0, 25, 25, 0),
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(24.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
child: Slidable(
|
||||
endActionPane: ActionPane(
|
||||
motion: StretchMotion(),
|
||||
children: [
|
||||
Text(
|
||||
taskName,
|
||||
style: TextStyle(
|
||||
decoration: taskCompleted
|
||||
? TextDecoration.lineThrough
|
||||
: TextDecoration.none),
|
||||
),
|
||||
Checkbox(
|
||||
value: taskCompleted,
|
||||
onChanged: onChanged,
|
||||
activeColor: Colors.redAccent,
|
||||
SlidableAction(
|
||||
onPressed: deleteFunction,
|
||||
icon: Icons.delete,
|
||||
backgroundColor: const Color.fromARGB(255, 249, 137, 135),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
)
|
||||
],
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).primaryColor,
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(24.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
taskName,
|
||||
style: TextStyle(
|
||||
decoration: taskCompleted
|
||||
? TextDecoration.lineThrough
|
||||
: TextDecoration.none),
|
||||
),
|
||||
Checkbox(
|
||||
value: taskCompleted,
|
||||
onChanged: onChanged,
|
||||
activeColor: Colors.redAccent,
|
||||
)
|
||||
],
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).primaryColor,
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user