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

套件詳情v0.5.2

mvvm

一個 Flutter MVVM。它使用基於屬性的資料繫結,在 ViewModel 和 View 之間建立連接,並透過 ViewModel 驅動 View 的變更。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
63
喜歡數
38
Pub 分數
150

安裝套件

flutter pub add mvvm

套件介紹

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

mvvm 包是 Model-View-ViewModel(MVVM)架構模式的 Flutter 實現,支援 ViewModel 與 View 之間的屬性資料繫結。它透過在 ViewModel 屬性變更時自動更新 UI 來簡化狀態管理,減少重複程式碼。此套件支援跨平台開發,涵蓋 Android、iOS、Web、macOS、Windows 及 Linux,非常適合可擴展且易於維護的應用程式。採用清晰的關注點分離設計,提升測試性與開發者生產力。

適用情境

  • 建構可擴展的 Flutter 應用
  • 實作反應式 UI
  • 將業務邏輯與呈現分離
  • 促進單元測試
  • 管理複雜的狀態流
  • 啟用跨平台一致性

主要特色

  • 基於屬性的資料繫結
  • 自動 UI 更新
  • 視圖與 ViewModel 的清晰分離
  • 跨平台支援
  • 最小化樣板程式碼
  • 可測試的架構

適合對象

  • 使用 MVVM 模式的開發者
  • 重視可維護程式碼的團隊
  • 需要反應式 UI 的專案
  • 大規模 Flutter 應用
  • 需要強大可測試性的應用

使用建議

  • 需要理解 MVVM 原則
  • 可能為簡單應用增加複雜性
  • 與主流套件相比,文件較少
  • 與其他狀態管理解決方案整合可能需要謹慎處理

原始 README

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

pub package

A Flutter MVVM (Model-View-ViewModel) implementation. It uses property-based data binding to establish a connection between the ViewModel and the View, and drives the View changes through the ViewModel.

一个 Flutter 的 MVVM(Model-View-ViewModel) 实现。 它使用基于属性 (property) 的数据绑定在视图模型 (ViewModel) 与视图 (View) 之间建立关联,并通过视图模型 (ViewModel) 驱动视图 (View) 变化。

import 'package:flutter/material.dart';
import 'package:mvvm/mvvm.dart';

/// ViewModel
class MyHomePageViewModel extends ViewModel {
  final timer$ = BindableProperty.$tick(
      duration: const Duration(milliseconds: 10), autostart: true, initial: 0);

  @override
  init() {
    registerProperty(#counter, BindableProperty.$value(initial: 0));
  }

  void incrementCounter() {
    updateValue<int>(#counter, (value) => value + 1);
  }
}

/// View
class MyHomePage extends View<MyHomePageViewModel> {
  final String title;
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  @override
  MyHomePageViewModel createViewModel() => MyHomePageViewModel();

  pad(int value) => '$value'.padLeft(2, '0');

  @override
  Widget build(BuildContext context, MyHomePageViewModel model) {    
    return Scaffold(
        appBar: AppBar(title: Text(title)),
        body: Center(
            child:
                Column(mainAxisAlignment: MainAxisAlignment.center, children: [
          $watch<int>(model.timer$,
              builder: (context, value, child) =>
                  Text('${pad(value ~/ 100)}.${pad(value % 100)}')),
          const Text('You have pushed the button this many times:'),
          model.$watchFor<int>(#counter,
              builder: (context, value, child) =>
                  Text('$value', style: Theme.of(context).textTheme.headline4))
        ])),
        floatingActionButton: FloatingActionButton(
            onPressed: model.incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add)));
  }
}

/// run
void main() => runApp(MaterialApp(
      title: 'Flutter MVVM Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyHomePage(title: 'Flutter MVVM Demo Home Page'),
    ));

Examples

APIs

Documentation

BindableProperty
  • $value
  • $adaptive
  • $async
  • $custom
  • $periodic
  • $tick
  • $merge
  • $mergeMap
  • $transform
  • $filter
WidgetBuilder
  • $watch
  • $watchFor
  • $any
  • $anyFor
  • $anyMap
  • $anyMapFor
  • $cond
  • $condFor
  • $if
  • $ifFor
  • $switch
  • $switchFor
  • $select
  • $build
ViewModel
  • registerProperty
  • getProperty
  • requireProperty
  • getPropertyOf
  • requirePropertyOf
  • getProperties
  • requireProperties
  • getValue
  • requireValue
  • setValue
  • setValues
  • updateValue
  • notify
View
  • createViewModel
  • build
  • didChangeDependencies
  • activate
  • deactivate
  • didUpdateWidget
ViewBuildContext
  • setState
  • model

License

MIT

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。