Created
December 27, 2021 12:45
-
-
Save reasje/94e3a93dfb38cc1d72cd4e99f78df1a6 to your computer and use it in GitHub Desktop.
hive_tut all
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/foundation.dart' show kIsWeb; | |
import 'package:flutter/material.dart'; | |
import 'package:hive/hive.dart'; | |
import 'package:hive_flutter/hive_flutter.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
String someRandomName = "someRandomName"; | |
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); | |
} | |
await Hive.openBox<String>(someRandomName); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: HiveSaver(), | |
); | |
} | |
} | |
class HiveSaver extends StatefulWidget { | |
HiveSaver({Key? key}) : super(key: key); | |
@override | |
_HiveSaverState createState() => _HiveSaverState(); | |
} | |
class _HiveSaverState extends State<HiveSaver> { | |
TextEditingController _textEditingController = new TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
final width = MediaQuery.of(context).size.width; | |
return Scaffold( | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Container( | |
margin: EdgeInsets.symmetric(horizontal: width * 0.25), | |
child: TextField( | |
controller: _textEditingController, | |
), | |
), | |
MyBottom( | |
title: "Save your name and move on .", | |
function: () { | |
Box simpleBox = Hive.box<String>(someRandomName); | |
simpleBox.put('name', _textEditingController.text); | |
Navigator.push(context, MaterialPageRoute( | |
builder: (context) { | |
return HiveLoader(); | |
}, | |
)); | |
}, | |
) | |
], | |
), | |
); | |
} | |
} | |
class HiveLoader extends StatelessWidget { | |
const HiveLoader({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
Box simpleBox = Hive.box<String>(someRandomName); | |
String name = simpleBox.get('name'); | |
return Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text("Good job $name :) ."), | |
MyBottom( | |
function: () { | |
Navigator.pop(context); | |
}, | |
title: "Go back !") | |
], | |
), | |
)); | |
} | |
} | |
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