搜尋套件、儲存庫與 APP 分類

套件詳情v0.1.0-dev.1

conditioned

此套件包含一個 Widget,可根據提供的條件顯示子 Widget。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
83
喜歡數
9
Pub 分數
150

安裝套件

flutter pub add conditioned

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

‘conditioned’ 套件提供了一個簡單的 Widget,可根據一個或多個布林條件來選擇性地顯示子組件。它透過將邏輯封裝在乾淨、可重用的元件中,簡化了顯示或隱藏 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')
    ),
  );
}

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。