搜索插件、代码仓与 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

所属分类

源码仓库

使用此插件的项目

暂无相关数据。