An inherited widget that holds data and rebuilds the widgets whom depend on the changed data.
This package uses InharitedModel
as its base.
- lightweight (~100 lines of code)
- uses flutter's own
InheritedModel
&InheritedModel
for detecting what needs to be rebuilt. - no boilerplate code
- no computed values
- using keys is annoying
The code for the example is in the example tab.
Store(
data: {
'counter': 1,
},
child: MaterialApp(
/* ... */
),
);
class CounterText extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(Store.of(context, 'counter').get().toString());
}
}
If you edit counter
using Store.of(context,'counter').set(2)
the CounterText widget
will be automatically rebuilt.
When you call Store.of(context,key)
all the widgets that use the current context
will be subscribed to changes for that key.
If the value for the key changes the widget that uses the subscribed context will be rebuilt.
In the Example bellow, when the counter
is modified,
only the text widget inside the builder gets updated
because it was subscribed using the c
context and only decedents of c
will be rebuilt.
If instead of calling Store.of(c,'counter')
we called Store.of(context,'counter')
,
when counter
changes, the entire widget will get rebuilt and the text that shows the time will also update.
class StoreText extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Text(DateTime.now().toString()), // show the time which this text widget was built
Builder(builder: (c) {
return Text(Store.of(c, 'counter').get().toString());
}),
],
);
}
}
Things that are stored in the data map should only be replaced with the set
method.
Store(
data: {
UnmodifiableListView<Post> : UnmodifiableListView<Post>([]), // using add on a list will not notify subscribers
User : User.getSavedUser(),
MyService : MyService(),
AppTheme : AppTheme(),
420 : false, // a key can be anything
'balance' : 12000,
},
child: MaterialApp(
/* ... */
),
);
You can use Inherited_Store
to effectively manage your app preferences.
Load your preferences into the data
field of the store then update the stored preferences using the onValueModified
callback.