Skip to content

Instantly share code, notes, and snippets.

@reasje
Last active February 10, 2022 09:29
Show Gist options
  • Save reasje/c90b654a1c706636f33b5c6c8b66935b to your computer and use it in GitHub Desktop.
Save reasje/c90b654a1c706636f33b5c6c8b66935b to your computer and use it in GitHub Desktop.
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hive_tut/models/note_model.dart';
import 'package:hive_tut/models/timedetail_model.dart';
import 'package:path_provider/path_provider.dart';
String noteBoxName = "noteBoxName";
void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (kIsWeb) {
Hive.initFlutter();
} else {
// getting the path of the document in the device for accesing the database
final document = await getApplicationDocumentsDirectory();
Hive.init(document.path);
}
// registering adapters
Hive.registerAdapter(NoteAdapter());
Hive.registerAdapter(TimeDetailAdapter());
await Hive.openBox<Note>(noteBoxName);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
HomeScreen({Key? key}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
TextEditingController _textEditingController = new TextEditingController();
@override
Widget build(BuildContext context) {
Box<Note> noteBox = Hive.box<Note>(noteBoxName);
// noteBox.clear();
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return NoteScreen();
},
));
},
child: Icon(Icons.add),
),
body: Container(
height: height,
width: width,
child: SafeArea(
child: Column(
children: [
Center(
child: Text(
"Your Notes !",
style: TextStyle(fontSize: 25),
)),
ValueListenableBuilder(
valueListenable: noteBox.listenable(),
builder: (context, Box<Note> noteBox, _) {
return noteBox.isEmpty
? Container()
: ListView.builder(
shrinkWrap: true,
itemCount: noteBox.length,
itemBuilder: (context, index) {
var noteItem = noteBox.getAt(index);
return ListTile(
tileColor: Colors.amber,
title: Text(
noteItem!.title,
),
subtitle: Text(
noteItem.desc,
softWrap: false,
overflow: TextOverflow.ellipsis,
),
trailing: Column(
children: [
Text(noteItem.tags
.toString()
.replaceAll("]", "")
.replaceAll("[", "")),
Spacer(),
Text(
"Created : ${noteItem.timeDetail.createdAt}",
softWrap: false,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 10,
)),
Text(
"Updated : ${noteItem.timeDetail.updatedAt}",
softWrap: false,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 10,
)),
],
),
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return NoteScreen(
noteItem: noteItem,
noteIndex: index,
);
},
));
},
);
},
);
},
),
],
),
),
));
}
}
class NoteScreen extends StatefulWidget {
final noteIndex;
final noteItem;
const NoteScreen({Key? key, this.noteItem, this.noteIndex}) : super(key: key);
@override
State<NoteScreen> createState() => _NoteScreenState();
}
class _NoteScreenState extends State<NoteScreen> {
var _titleController = TextEditingController(text: "");
var _descController = TextEditingController(text: "");
var _tagController = TextEditingController(text: "");
String? timeDetial;
Note? noteItem;
@override
void initState() {
noteItem = widget.noteItem;
if (noteItem != null) {
_titleController.text = noteItem!.title;
_descController.text = noteItem!.desc;
_tagController.text =
noteItem!.tags.toString().replaceAll("]", "").replaceAll("[", "");
timeDetial =
"Created : ${noteItem!.timeDetail.createdAt} Updated : ${noteItem!.timeDetail.updatedAt}";
}
super.initState();
}
@override
Widget build(BuildContext context) {
Box<Note> noteBox = Hive.box<Note>(noteBoxName);
final width = MediaQuery.of(context).size.width;
return Scaffold(
body: Center(
child: Container(
width: width * 0.6,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _titleController,
decoration: InputDecoration(hintText: "Title"),
),
TextField(
controller: _descController,
decoration: InputDecoration(hintText: "Desc"),
),
TextField(
controller: _tagController,
decoration: InputDecoration(hintText: "Tags"),
),
MyBottom(
function: () {
var title = _titleController.text;
var desc = _descController.text;
List<String> tagsList = _tagController.text.split(",");
var timedetail = TimeDetail(
noteItem == null
? DateTime.now()
: noteItem!.timeDetail.createdAt,
DateTime.now());
var finalNote = Note(title, desc, tagsList, timedetail);
noteItem == null
? noteBox.add(finalNote)
: noteBox.putAt(widget.noteIndex, finalNote);
Navigator.pop(context);
},
title: "Done"),
timeDetial == null ? Container() : Text(timeDetial!)
],
),
),
));
}
}
class MyBottom extends StatelessWidget {
final void Function() function;
final String title;
const MyBottom({
Key? key,
required this.function,
required this.title,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
onTap: function,
child: Container(
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(top: 10),
decoration: BoxDecoration(
color: Colors.black38,
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Text(title)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment