Last active
March 29, 2020 10:10
-
-
Save paulallies/debd1d2c25b1d717f2fa26671a43d14b to your computer and use it in GitHub Desktop.
HiveDemo File
This file contains hidden or 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_flutter/hive_flutter.dart'; | |
class HiveDemo extends StatelessWidget { | |
final TextEditingController controller = TextEditingController(); | |
final _nameBox = Hive.box("name"); | |
_saveNameAt(id) async { | |
await _nameBox.put(id, controller.text); | |
controller.text = ""; | |
} | |
_deleteNameAt(id) async { | |
await _nameBox.delete(id); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: ValueListenableBuilder( | |
valueListenable: _nameBox.listenable(), | |
builder: (context, value, child) { | |
var _id = "settings_name"; | |
var _name = _nameBox.get(_id) ?? "<No Name>"; | |
return Center( | |
child: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
_name, | |
style: TextStyle(fontSize: 60), | |
), | |
SizedBox( | |
height: 80, | |
), | |
Container( | |
child: TextField( | |
controller: controller, | |
decoration: InputDecoration( | |
hintText: "Provide Name", | |
), | |
), | |
), | |
FlatButton( | |
color: Colors.green, | |
onPressed: () => _saveNameAt(_id), | |
child: Text( | |
"Save Name", | |
style: TextStyle(color: Colors.white), | |
), | |
), | |
FlatButton( | |
color: Colors.red, | |
onPressed: () => _deleteNameAt(_id), | |
child: Text( | |
"Delete Name", | |
style: TextStyle(color: Colors.white), | |
), | |
) | |
], | |
), | |
), | |
); | |
}), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment