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

패키지 상세v0.2.0

reactives

StatefulWidget의 공통 로직을 추출/그룹화/재사용하는 새로운 방법입니다. React 훅처럼 Flutter용으로 생각하시면 됩니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
37
좋아요
44
Pub 점수
140

패키지 설치

flutter pub add reactives

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

ReactiveS는 React에서 영감을 받은 훅과 유사한 패턴을 사용하여 StatefulWidget 컴포넌트 내에서 상태 로직을 추출하고 그룹화하며 재사용할 수 있도록 해주는 Flutter/Dart 패키지입니다. 복잡한 상태 관리를 단순화하여 재사용 가능하고 조합 가능한 상태 행동 단위를 허용합니다. 깔끔하고 모듈식 코드를 위한 설계로 관심사의 분리와 봄보일러플레이트 감소를 촉진합니다. 모든 주요 플랫폼을 지원하므로 외부 종속성 없이 구조적인 상태 처리가 필요한 크로스 플랫폼 앱에 이상적입니다.

사용 사례

  • 위젯 간에 재사용 가능한 상태 로직
  • 복잡한 StatefulWidget 클래스 간소화
  • 크로스 플랫폼 상태 관리
  • 모듈러 UI 개발
  • 상태 있는 컴포넌트에서 부가 코드 줄이기
  • 반복적인 상태 패턴 교체

주요 기능

  • Flutter용 훅 유사 구문
  • 상태 로직 추출 및 재사용
  • 조합 가능한 상태 단위
  • 크로스 플랫폼 지원
  • 최소한의 보일러플레이트
  • 상태와 UI의 명확한 분리

적합한 대상

  • Flutter에서 React 스타일의 훅을 찾는 개발자
  • StatefulWidget의 복잡성을 줄이고자 하는 팀
  • 모듈식 상태 관리가 필요한 프로젝트
  • 크로스플랫폼 플러터 애플리케이션
  • 재사용 가능한 UI 컴포넌트 개발자

고려 사항

  • 함수형 프로그래밍 개념을 이해하고 있어야 합니다
  • 초보자에게 학습 곡선을 도입할 수 있습니다
  • 완전한 상태 관리 솔루션이 아님
  • 작은 규모에서 중간 규모의 상태 로직에 가장 적합
  • 성숙한 상태의 라이브러리에 비해 문서가 제한적

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

reactives

A new way for Flutter to reuse/group common logic. Think of them like React hooks but for and of flutter.

Idea copied from the lit world

Motive

Have you ever written code like this?

class AwesomeWidget extends StatefulWidget {
  const AwesomeWidget({Key? key}) : super(key: key);

  @override
  _AwesomeWidgetState createState() => _AwesomeWidgetState();
}

class _AwesomeWidgetState extends State<AwesomeWidget>
    with TickerProviderStateMixin {
  late final TextEditingController emailCtrl;
  late final TextEditingController passwordCtrl;
  late final AnimationController entryAnimation;
  late final AnimationController highLightAnimation;

  @override
  void initState() {
    super.initState();
    emailCtrl = TextEditingController();
    passwordCtrl = TextEditingController();
    entryAnimation = AnimationController(vsync: this);
    highLightAnimation = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    emailCtrl.dispose();
    entryAnimation.dispose();
    highLightAnimation.dispose();
    passwordCtrl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

There are a couple of problems here.

  • Most of the object management logic is generic and can be reused.
  • It is extremely easy to forget that one dispose call (confession).
  • Coupling the logic to the widget makes it harder to test.

Using reactives this gets transformed to,

class AwesomeReactiveWidget extends StatefulWidget {
  const AwesomeReactiveWidget({Key? key}) : super(key: key);

  @override
  _AwesomeReactiveWidgetState createState() => _AwesomeReactiveWidgetState();
}

class _AwesomeReactiveWidgetState extends State<AwesomeReactiveWidget> with ReactiveHostMixin {
  late final emailCtrl = ReactiveTextEditingController(this);
  late final passwordCtrl = ReactiveTextEditingController(this);
  late final entryAnimation = ReactiveAnimationController(this);
  late final exitAnimation = ReactiveAnimationController(this);

  @override
  Widget build(BuildContext context) {
    return Container(
        // ....
    );
  }
}

See Examples for examples of writing a reactive or just browse the source, it's really small.

Comparision to flutter_hooks:

From a user perspective flutter_hooks is a replacement for StatefulWidget. reactives is not. If you look at the code for ReactiveHostMixin it is about 60 lines (blank lines included). Reactives do not try to replace StatefulWidget, it just solves the reusability problem inherent due to mixin's "is-a" relationship. Reactives have a "has-a" relationship.

There are no new rules to writing reactives. See examples of existing reactives. It is basically the same logic isolated in a different class. For the same reason it doesn't need a lot of the hooks like useState which would be needed if tried to replace StatefulWidget.

The learning curve of reactives is next to negligible. Hooks need you to learn a few concepts and how to/not to do things. It also requires you to transform entire widgets. Reactives can be adapted incrementally even for a single widget.

카테고리

소스 저장소

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

아직 데이터가 없습니다.