prefman 0.2.4 prefman: ^0.2.4 copied to clipboard
A better way to manage App Preferences using persistant variables in a Structured Way.
PrefMan #
A structured way to manage Preferences.
Preference Class #
var settings = AppPreferences();
class AppPreferences extends SettingManifest {
final theme = Preference<String>.options(
key: 'theme',
defaultValue: 'blue',
options: [
Option('blue'),
Option('red'),
Option('orange'),
],
);
final username = Preference<String>.any(
key: 'username',
defaultValue: 'user',
);
final volume = Preference.integer(
key: 'volume',
defaultValue: 10,
min: 0,
max: 15,
);
final askAgain = Preference.boolean(
key: 'ask_again',
defaultValue: true,
);
List<Preference> get preferences => [theme, username, volume, askAgain];
}
Use #
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await PrefMan.initialize();
// ...
}
var username = settings.username.get();
settings.username.setValue('new username');
Persistent Variables #
You don't have to declare your preferences in a SettingManifest, you can use them as persistent variables.
final count = Preference.integer(key: 'counter', defaultValue: 0);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(count.get().toString()),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () async {
await count.setValue(count.get() + 1);
setState(() {});
},
),
);
}
The count will persist between runs!
Observability #
The Preference
class implements the Listenable
interface, so you can use it
with AnimatedBuilder
or other means of reacting to its changes.
final count = Preference.integer(key: 'counter', defaultValue: 0);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: AnimatedBuilder(
animation: count,
builder: (context, _) => Text(count.get().toString()),
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => count.setValue(count.get() + 1),
),
);
}