Created
December 27, 2021 13:31
-
-
Save reasje/bbba6b328d8f7826511dc1e91b749005 to your computer and use it in GitHub Desktop.
hive_tut all 2
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) { | |
Box<String> simpleBox = Hive.box<String>(someRandomName); | |
final width = MediaQuery.of(context).size.width; | |
return Scaffold( | |
body: ValueListenableBuilder( | |
valueListenable: simpleBox.listenable(), | |
builder: (context, Box<String> simpleBox, _) { | |
String? name = simpleBox.get('name'); | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Container( | |
margin: EdgeInsets.symmetric(horizontal: width * 0.25), | |
child: TextField( | |
controller: _textEditingController, | |
onChanged: (textFieldText) { | |
simpleBox.put('name', textFieldText); | |
}, | |
), | |
), | |
MyBottom( | |
title: "Save your name and move on .", | |
function: () { | |
Navigator.push(context, MaterialPageRoute( | |
builder: (context) { | |
return HiveLoader(); | |
}, | |
)); | |
}, | |
), | |
Container( | |
padding: EdgeInsets.all(10), | |
child: Text(name ?? "Nothing"), | |
) | |
], | |
); | |
}, | |
)); | |
} | |
} | |
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