패키지, 저장소 및 앱 카테고리 검색

패키지 상세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 업데이트
  • 프로바이더의 경량 대안
  • 테스트 가능한 비즈니스 로직
  • 작은에서 중간 규모의 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.

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.