搜尋套件、儲存庫與 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.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。