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

패키지 상세v0.1.0-dev.1

conditioned

이 패키지는 제공된 조건에 따라 자식 위젯을 표시할 수 있는 위젯을 포함하고 있습니다.

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
83
좋아요
9
Pub 점수
150

패키지 설치

flutter pub add conditioned

패키지 개요

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

'conditioned' 패키지는 하나 이상의 부울 조건에 따라 자식을 조건부로 표시하는 간단한 위젯을 제공합니다. 로직을 깔끔하고 재사용 가능한 컴포넌트로 감싸서 UI 요소를 표시하거나 숨기는 과정을 단순화합니다. 상태 변화에 따라 가시성이 달라지는 동적 UI에 적합하며, 중첩된 삼항 연산자나 if-else 블록보다 가독성이 향상됩니다. 이 패키지는 Android, iOS, Web, macOS, Windows, Linux 등 모든 주요 플랫폼을 지원합니다.

사용 사례

  • 조건부 UI 렌더링
  • 동적 위젯 표시
  • 복잡한 가시성 로직 간소화
  • 폼 검증 피드백
  • 로딩 상태 관리

주요 기능

  • 간단한 조건 기반 표시
  • 여러 조건을 지원합니다
  • 크로스 플랫폼 호환성
  • 깔끔하고 읽기 쉬운 구문
  • 최소한의 보일러플레이트

적합한 대상

  • 간결한 조건부 렌더링을 찾는 개발자
  • 자주 UI 상태가 변경되는 프로젝트
  • 코드 가독성을 우선시하는 팀
  • 반응형 UI 구성 요소가 필요한 앱
  • Flutter 초보자가 상태 관리 배우기

고려 사항

  • 불리안 조건에 한정
  • 비동기 상태를 직접 처리하지 않음
  • 내장 애니메이션 지원 없음
  • 상태 관리 솔루션과 함께 사용하는 것이 가장 좋습니다
  • 복잡한 조건 워크플로우에 적합하지 않습니다

원본 README

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

conditioned

A package that contains a set of Widgets to remove ugly if, if else and switch statements from your layout code and keep it more consistent.

Usage

The Conditioned Widget is just like any other and can be used in your Widget tree. Conditioned will have one child. What that child will be is up to you. You will provide the cases and Conditioned will figure out which builder to call.

Note: You will always provide a builder method that results in a Widget. Providing builders instead of Widgets is better for performance and allows us to use null objects.

Basics

When you want to add a specific child based on a condition you can avoid the if, else if and else drama by using the Conditioned Widget. Conditioned will use the builder of the first Case whos expression validates as true.

Without Conditioned

We cannot use if .. else if .. else directly in our layout code. We have to propagate it to a function (not recommended) or to a new Widget.

Widget build(BuildContext context){
  return Container(
    child: _getIcon(),
  );
}

Widget _getIcon() {
  if (myNumber < 25)
    return Icon(Icons.ac_unit);
  else if (myNumber < 50)
    return Icon(Icons.home);
  else if (myNumber < 75)
    return Icon(Icons.wb_cloudy);
  else
    return Icon(Icons.wb_sunny);
}
With Conditioned

With the use of Conditioned you don't have to break up the tree at the point of the if .. else if .. else. You can break up the tree where it makes sense, which is often right after the condition.

Widget build(BuildContext context) {
  return Container(
    child: Conditioned(
      cases: [
        Case(myNumber < 25, builder: () => Icon(Icons.ac_unit)),
        Case(myNumber < 50, builder: () => Icon(Icons.home)),
        Case(myNumber < 75, builder: () => Icon(Icons.wb_cloudy)),
      ],
      defaultBuilder: () => Icon(Icons.wb_sunny),
    ),
  );
}
Simple boolean checks

For simple true/false checks you can use the Conditioned.boolean constructor. It will not reduce your lines of code, it does however make it more consistent with the rest of your layout code.

Without Conditioned.boolean
Widget build(BuildContext context) {
    return Container(
        child: myNumber > 50
          ? Text(
              'The condition is true!',
              style: TextStyle(color: Colors.green[200]),
            )
          : Text(
              'The condition is false!',
              style: TextStyle(color: Colors.green[800]),
        ),
    );
}
With Conditioned.boolean
Widget build(BuildContext context) {
    return Container(
        child: Conditioned.boolean( myNumber > 50,
            trueBuilder: () => Text(
              'The condition is true!',
              style: TextStyle(color: Colors.orange[200]),
            ),
            falseBuilder: () => Text(
              'The condition is false!',
              style: TextStyle(color: Colors.orange[800]),
            ),
        ),
    );
}
Switch cases

If instead of checking for boolean conditions but want to check by equality where you would normally want to use a switch, you can use the Conditioned.equality<T> static method.

Let's say we need to build a screen based on the following information:

enum State {empty, loading, full, error}
final state = State.idle;
Without Conditioned.equality<T>

We cannot use a switch directly in our layout code. We have to propagate it to a function (not recommended) or to a new Widget.

Widget build(BuildContext context) {
  return Container(
    child: _getStateText(),
  );
}

Widget _getStateText() {
  switch(state) {
    case State.loading:
      return Text('The device is loading');
    case State.full:
      return Text('The device is fully loaded');
    case State.error:
      return Text('An error occured');
    default:
      return Text('The device is operating as expected');
    }
}
With Conditioned.equality<T>

With the use of Conditioned.equality<T> you don't have to break up the tree at the point of the switch or introduce a Builder Widget. You can break up the tree where it makes more sense, right after the switch for example.

Widget build(BuildContext context) {
  return Container(
    child: Conditioned.equality<State>( state,
      values: [
        Value(State.loading, builder: () => Text('The device is loading')),
        Value(State.full, builder: () => Text('The device is fully loaded')),
        Value(State.error, builder: () => Text('An error occured')),
      ],
      defaultBuilder: () => Text('The device is operating as expected')
    ),
  );
}

카테고리

소스 저장소

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

아직 데이터가 없습니다.