Skip to content

Instantly share code, notes, and snippets.

@vivekpatel99
Last active September 1, 2020 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vivekpatel99/b50b81fe217af90ce979b104df17703b to your computer and use it in GitHub Desktop.
Save vivekpatel99/b50b81fe217af90ce979b104df17703b to your computer and use it in GitHub Desktop.
Simple TODO app using Hive database in Flutter
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(),
);
}
}
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();
}
}
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});
}
// 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