Simple TODO app using Hive database in Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:hive/hive.dart'; | |
import 'package:hive_db_example/my_home_page.dart'; | |
import 'package:hive_db_example/task_model.dart'; | |
import 'package:path_provider/path_provider.dart' as path_provider; | |
void main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
final applicatonDocumentDir = | |
await path_provider.getApplicationDocumentsDirectory(); | |
Hive.init(applicatonDocumentDir.path); | |
Hive.registerAdapter(TaskAdapter()); | |
await Hive.openBox<Task>('TODOs'); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Simple TODO App Using Hive', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:hive/hive.dart'; | |
import 'package:hive_db_example/task_model.dart'; | |
import 'package:hive_flutter/hive_flutter.dart'; | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
String inputTask; | |
Task _task; | |
Box<Task> todosBox; | |
void _addTodo(Task inputTodo) { | |
todosBox.add(Task(task: inputTodo.task)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// notesBox.clear(); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Simple TODO app using Hive'), | |
), | |
body: ValueListenableBuilder( | |
valueListenable: Hive.box<Task>('TODOs').listenable(), | |
builder: (context, Box<Task> _notesBox, _) { | |
todosBox = _notesBox; | |
return ListView.builder( | |
itemCount: _notesBox.values.length, | |
itemBuilder: (BuildContext context, int index) { | |
final todo = todosBox.getAt(index); | |
return ListTile( | |
title: Text(todo.task), | |
onLongPress: () => todosBox.deleteAt(index), | |
); | |
}); | |
}), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => _simpleDialog(), | |
tooltip: 'AddNewTODOTask', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
_simpleDialog() { | |
showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return SimpleDialog( | |
title: const Text('New TODO Task'), | |
children: <Widget>[ | |
Center( | |
child: Padding( | |
padding: const EdgeInsets.all(15.0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
TextField( | |
decoration: InputDecoration( | |
hintText: 'TODO Task', | |
focusedBorder: OutlineInputBorder( | |
borderSide: BorderSide( | |
color: Colors.blue, | |
), | |
), | |
border: InputBorder.none, | |
), | |
onChanged: (value) => inputTask = value, | |
), | |
Padding( | |
padding: const EdgeInsets.all(15.0), | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: [ | |
FlatButton( | |
color: Colors.blue, | |
onPressed: () { | |
_task = Task(task: inputTask); | |
_addTodo(_task); | |
Navigator.pop(context); | |
}, | |
child: Text('Add'), | |
), | |
FlatButton( | |
color: Colors.blue, | |
onPressed: () => Navigator.pop(context), | |
child: Text('Cancel'), | |
) | |
], | |
), | |
), | |
], | |
), | |
), | |
) | |
], | |
); | |
}, | |
); | |
} | |
@override | |
void dispose() { | |
// remove the deleted index holes/slots from database | |
// to free up the space | |
todosBox.compact(); | |
todosBox.close(); | |
super.dispose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:hive/hive.dart'; | |
part 'task_model.g.dart'; | |
@HiveType(typeId: 0) | |
class Task { | |
@HiveField(0) | |
String task; | |
// Just an example for next attribute | |
// @HiveField(1) | |
// bool checked; | |
Task({this.task}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GENERATED CODE - DO NOT MODIFY BY HAND | |
part of 'task_model.dart'; | |
// ************************************************************************** | |
// TypeAdapterGenerator | |
// ************************************************************************** | |
class TaskAdapter extends TypeAdapter<Task> { | |
@override | |
final int typeId = 0; | |
@override | |
Task read(BinaryReader reader) { | |
final numOfFields = reader.readByte(); | |
final fields = <int, dynamic>{ | |
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), | |
}; | |
return Task( | |
task: fields[0] as String, | |
); | |
} | |
@override | |
void write(BinaryWriter writer, Task obj) { | |
writer | |
..writeByte(1) | |
..writeByte(0) | |
..write(obj.task); | |
} | |
@override | |
int get hashCode => typeId.hashCode; | |
@override | |
bool operator ==(Object other) => | |
identical(this, other) || | |
other is TaskAdapter && | |
runtimeType == other.runtimeType && | |
typeId == other.typeId; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment