搜索插件、代码仓与 APP 分类

插件详情v3.0.0

minimal_mvn

使用 Model-View-Notifier (MVN) 模式为 Flutter 应用程序提供的一种极简状态管理解决方案

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
54
喜欢数
44
Pub 分数
150

安装插件

flutter pub add minimal_mvn

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Minimal MVN 是一个轻量级的 Flutter 状态管理解决方案,实现了模型-视图-通知器(MVN)模式。它提供了一种简单且响应式的状态管理方式,代码冗余极少。通过将数据(模型)、UI(视图)和状态更新(通知器)分离,它促进了清晰的架构设计和易于测试。该包专为寻求比更重的状态管理方案更简单替代方案的开发者设计,同时不牺牲性能或可扩展性。

适合场景

  • 简单的应用状态管理
  • 响应式 UI 更新
  • providers 的轻量级替代方案
  • 可测试的业务逻辑
  • 中小型 Flutter 应用

主要特点

  • 最小化样板代码
  • 模型-视图-通知者模式
  • 响应式状态更新
  • 与现有代码的轻松集成
  • 支持所有 Flutter 平台

适合对象

  • 追求简洁的开发者
  • 避免使用复杂状态库的项目
  • 注重可维护性的团队
  • 学习状态管理基础
  • 中等状态复杂度的应用程序

使用建议

  • 不太适合非常大规模的应用程序
  • 有限的内置调试工具
  • 无内置持久化
  • 高级功能需要手动设置
  • 最适合具有基本到中级状态需求的应用程序

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Minimal Logo

Pub Version License

Minimal MVN

A minimal state management package for Flutter. Part of a minimalistic architecture based on the MVN (Model-View-Notifier) pattern.

This package aims for the simplest possible architecture, making it easier to understand and use, while offering an alternative to the growing complexity found in many other state management solutions, in an attempt to minimize side effects.

Try it

Check out the live example at https://alesalv.github.io/minimal/

Counter

The classical Flutter counter app demonstrates basic state management. The counter on the top is a non disposable notifier, so it keeps memory when re-entering the page. The one on the bottom is a disposable notifier, so it resets when re-entering the page.

Chroma Counter

A more advanced counter widget, which increases the counter, and randomly changes color and shape. The reveal button shows a second widget using the same notifier. Thanks to the autodispose feature, the notifier is disposed only when both the widgets are not visible anymore. The string on top of the choma counter updates every 10 counts, thanks to the select feature, which is used to avoid unnecessary rebuilds.

The complete source code is in the example folder.

Getting Started

Add Minimal to your pubspec.yaml:

dart pub add minimal_mvn
# or
flutter pub add minimal_mvn

and import the package:

import 'package:minimal_mvn/minimal_mvn.dart';

You can now start using Minimal's MVN pattern in your application. The quickest way is to follow the 4 steps below.

Features

  • 🎯 MVN (Model-View-Notifier) pattern
  • 🚀 Lazy initialization of notifiers
  • 🔄 Optional autodispose for notifiers
  • ⚡ State selection for optimized rebuilds
  • 📦 Dependency injection with locator

State Management in 4 Steps

1. Create an immutable UI state
@MappableClass()
class ChromaCounterUIState with ChromaCounterUIStateMappable {
  const ChromaCounterUIState({
    this.backgroundColor = Colors.blue,
    this.count = 0,
  });
  final Color backgroundColor;
  final int count;
}
2. Create a notifier to hold your state
class ChromaCounterNotifier extends MMNotifier<ChromaCounterUIState> {
  ChromaCounterNotifier() : super(const ChromaCounterUIState());

  void nextMetamorph() => notify(
        state.copyWith(
          backgroundColor: _randomColor(),
          count: state.count + 1,
        ),
      );
}
3. Create a manager to access your notifier
final MMManager<ChromaCounterNotifier> chromaCounterManager =
    MMManager(ChromaCounterNotifier.new, autodispose: true);
4 Use the notifier from UI
Access the notifier upon user's actions
FloatingActionButton(
  onPressed: () => chromaCounterManager.notifier.nextMetamorph(),
);
Rebuild the UI when state changes
final notifier = chromaCounterManager.notifier;
return ListenableBuilder(
  listenable: notifier,
  builder: (context, _) => Container(
    color: notifier.state.backgroundColor,
    child: const Text('Count: ${notifier.state.count}'),
  ),
);
(Optimized) Rebuild the UI only when part of the state changes
final notifier = chromaCounterManager.notifier;
return ListenableBuilder(
  listenable: notifier.select((state) => state.backgroundColor),
  builder: (context, _) => Container(
    color: notifier.state.backgroundColor,
  ),
);

Testing

Widget Testing

In tests, you can override the notifier with a mock notifier through the minimal manager:

testWidgets('should update UI when state changes', (tester) async {
  // Use the minimal manager to override the notifier
  chromaCounterManager.override(MockCounterNotifier.new);

  await tester.pumpWidget(const MaterialApp(home: ChromaCounter()));

  // Change state through the mock notifier
  chromaCounterManager.notifier.nextMetamorph();
  await tester.pump();

  final newColor = _getContainerColor(tester);
  // Test the exact color instead of a random one
  expect(newColor, equals(Colors.red));
});

See the example app tests for more testing examples.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。