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

套件詳情v2.1.0

flutter_conditional_rendering

一個增強條件渲染的 Flutter 套件,支援 if-else 和 switch 條件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
9070
喜歡數
114
Pub 分數
160

安裝套件

flutter pub add flutter_conditional_rendering

套件介紹

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

一個 Flutter 套件,透過擴展組件功能以支援 if-else 與 switch 語句,簡化了 UI 開發中的條件渲染。它讓開發者能根據執行時值來條件性地顯示組件時撰寫更乾淨、更具可讀性的程式碼。此套件可無縫整合至現有的 Flutter 項目中,並支援所有主要平台,包含 Android、iOS、Web、macOS、Windows 與 Linux。非常適合需要邏輯驅動可見性控制的動態 UI。

適用情境

  • 基於狀態的動態UI元素
  • 表單中的條件性小部件顯示
  • 基於交換機的版面變更
  • 使用者角色基礎的介面渲染
  • 簡化嵌套三元表達式
  • 具有平台特定邏輯的響應式設計

主要特色

  • 支援在小部件樹中使用 if-else 語法
  • 提供 switch-case 條件渲染
  • 減少複雜條件中的樣板程式碼
  • 適用於所有 Flutter 平台
  • 使用 pub add 命令進行最小化設定
  • 乾淨、可讀的條件邏輯

適合對象

  • 尋求更乾淨條件式 UI 程式碼的開發人員
  • 頻繁狀態依賴渲染的專案
  • 優先考慮可維護的元件邏輯的團隊
  • 需要平台特定使用者介面變化的應用程式
  • 使用宣告式 UI 模式的前端開發人員

使用建議

  • 可能會稍微增加封裝大小
  • 需要理解 Dart 的表達式語法
  • 不適合非常複雜的狀態管理
  • 僅限簡單條件類型
  • 最佳搭配狀態管理解決方案使用
  • 避免在深度嵌套的 widget 結構中過度使用

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

flutter_conditional_rendering

A flutter package which enhances conditional rendering, supports if-else and switch conditions.

Why

In flutter if you want to do conditional rendering, you may do this:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        if (someCondition == true)
          Text('The condition is true!'),
      ],
    );
  }
}

But what if you want to use a tertiary (if-else) condition?

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        someCondition == true ?
          Text('The condition is true!'):
          Text('The condition is false!'),
      ],
    );
  }
}

It is okay for a single Text widget, but the readability quickly become very bad when the child widget is multi-layered.

Usage

If-Else condition:
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Conditional.single(
          context: context,
          conditionBuilder: (BuildContext context) => someCondition == true,
          widgetBuilder: (BuildContext context) => Text('The condition is true!'),
          fallbackBuilder: (BuildContext context) => Text('The condition is false!'),
        ),
      ],
    );
  }
}
Switch condition:
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ConditionalSwitch.single<String>(
          context: context,
          valueBuilder: (BuildContext context) => 'A',
          caseBuilders: {
            'A': (BuildContext context) => Text('The value is A!'),
            'B': (BuildContext context) => Text('The value is B!'),
          },
          fallbackBuilder: (BuildContext context) => Text('None of the cases matched!'),
        ),
      ],
    );
  }
}

Want a list of widgets?

If you want to conditionally render a list of widgets (List<Widget>) instead of a single one. Use Conditional.list() and ConditionalSwitch.list()!

Contributions

Feel free to contribute to this project.

If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue. If you fixed a bug or implemented a feature, please send a pull request.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。