v0.0.4
mobx_provider
用于轻松将 UI 与您的存储(视图模型)绑定的包,该包受填充栈架构模式启发
14喜欢 12近 30 天下载40Pub 分数
平台暂无数据
用于轻松将 UI 与您的存储(视图模型)绑定的包,该包受填充栈架构模式启发
{"sdk":"flutter"}^1.2.1+1^4.3.1^1.1.0+1——{"sdk":"flutter"}以下为英文项目原文快照,最新内容请访问 GitHub。
Additional package for Mobx, it allows you to bind your (viewmodels) mobx stores with UI more easily the package is inspired by filled stacks pattern [view model provider].
NOTE : if you are not familiar with Mobx, take a look on it to see how you can use this package.
Breaking Changes
MobxStatefulProvider now changed to MobxStatefulObserver and
MobxStatelessProvider now changed to MobxStatelessObserver with the same functionalitiesMobxStatefulProvider widget is integration between Mobx and Provider package
it contains Provider.of<T>(context) where T extends MobxBase , this widget exposes only the value of the store wihtout observing itMobxWidgetProvider uses also Provider to get the value of the store and observe it in specific widget (see the example to have a better understanding)1 - create you own store and make it extends MobxBase class and write your own logic
class CounterStore = _CounterStore with _$CounterStore;
abstract class _CounterStore extends MobxBase with Store {
_CounterStore():_counter=0;
@observable
int _counter;
int get counter => _counter;
@action
void increment() => _counter++;
@override
void dispose() {}
}
NOTE:
MobxBase contains a dispose function which should be implemented in your store .loading ,error ,initial,success.2- bind your logic with the UI like this
class CounterWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MobxStatefulObserver<CounterStore>(
store: CounterStore(),
initFunction:(store){
//do api calls here
//store.fetchData();
}
builder: (context, store) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: store.increment,
),
appBar: AppBar(
title: Text('mobx provider example'),
),
body: Center(
child: Column(
children: <Widget>[
Text('counter value is ${store.counter}'),
],
),
),
);
},
);
}
}
MobxStatefulObserver is a stateful widget that takes a generic type which extends MobxBase .initFunction that's called in initState if you wanted to run some code when the widget is rendered for the first timeIf you want to have more optimization when rendring the widget tree
MobxStatefulProvider which exposes only the store from the provider package (donsn't observe any state)MobxWidgetProvider to observe the state of the store and only this widget will be rendered when the store changesclass CounterWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MobxStatefulProviderr<CounterStore>(
initFunction: (store){
//do api calls here
//store.fetchData();
}
builder: (context, store) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: store.increment,
),
appBar: AppBar(
title: Text('mobx provider example'),
),
body: Center(
child: Column(
children: <Widget>[
MobxWidgetProvider<CounterStore>(
builder:(context,store){
//this widget will only be rendered when the value changes
return Text('counter value is ${store.counter}');
}
)
],
),
),
);
},
);
}
}
if you have any suggestions or improvments please let me know .