搜尋套件、儲存庫與 APP 分類

套件詳情v0.0.4

mobx_provider

用於輕鬆將 UI 與您的儲存區(檢視模型)繫結的套件,此套件靈感來自填滿堆疊架構模式

近 30 天下載
12
喜歡數
14
Pub 分數
40

安裝套件

flutter pub add mobx_provider

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

MobX Provider 是一個輕量級套件,可簡化 Flutter 應用程式中 UI 與狀態管理儲存(檢視模型)之間的連接。受到 Clean Architecture 模式的啟發,它能無縫整合 MobX 的反應式狀態管理與 Flutter 的元件樹。該套件簡化了相依性注入和狀態存取,減少樣板程式碼並提升程式碼可維護性。它支援高效能的反應式更新,並在底層狀態變更時自動更新 UI。

適用情境

  • 使用 MobX 儲存的響應式 UI
  • 大型 Flutter 應用中的狀態管理
  • 清潔架構實現
  • 視圖模型的依賴注入
  • 簡化跨小部件的商店存取

主要特色

  • 與 MobX 的輕鬆整合
  • 狀態改變時自動重新建構 UI
  • 最小化樣板程式碼
  • 支援巢狀的儲存層次結構
  • 關注點的清晰分離

適合對象

  • 使用 MobX 進行狀態管理的開發者
  • 遵循清潔架構的專案
  • 尋求簡單、可擴展狀態解決方案的團隊
  • 具有複雜狀態需求的 Flutter 應用
  • 需要反應式UI更新的應用程式

使用建議

  • 需要設定 MobX
  • 僅限基於 MobX 的狀態管理
  • 無內建錯誤處理
  • 不適用於非 MobX 工作流程
  • 進階用法可能需要額外的設定

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

mobx_provider

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].

Getting Started

NOTE : if you are not familiar with Mobx, take a look on it to see how you can use this package.

what's new in version 0.0.3

  • The packages now depends on the latest versions of [mobx],[flutter_mobx],[provider] see the change log for thier versions
  • Fix MobxStatefulProvider exception on dispose , Thanks to wbemanuel
  • change [Provider.of(context)] to [Context.read()]

Breaking Changes

  • in older versions (before 0.0.2) MobxStatefulProvider now changed to MobxStatefulObserver and MobxStatelessProvider now changed to MobxStatelessObserver with the same functionalities
  • in the new version (0.0.2) the MobxStatefulProvider 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 it
  • MobxWidgetProvider 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:

  • the class MobxBase contains a dispose function which should be implemented in your store .
  • additionally, it has some helper functions and states which indicates the state of your store whether it's 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 .
  • it contains initFunction that's called in initState if you wanted to run some code when the widget is rendered for the first time
  • it'll call the dispose function when the widget it removed from the widget tree.

If you want to have more optimization when rendring the widget tree

  • you can use MobxStatefulProvider which exposes only the store from the provider package (donsn't observe any state)
  • use MobxWidgetProvider to observe the state of the store and only this widget will be rendered when the store changes
class 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 .

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。