AlexeyPerov / proviso

A complete set of Flutter tools for conditional rendering (if-else and switch conditions), subtree wrapping with a parent widget, and some handy shortcuts (like DebugWidget, WebOnlyWidget, SafeBuilder, and many more).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

proviso

stability-stable License

A complete set of tools for conditional rendering (if-else and switch conditions), subtree wrapping with a parent widget, and some handy shortcuts (like DebugWidget, WebOnlyWidget, SafeBuilder, and many more). 👍

Install

In flutter project add the dependency:

dependencies:
  ...
  proviso: ^1.0.5

Why

To make a more readable and simpler conditional statement code.

All variants of conditional rendering you will ever need in a single package

in the form of widgets:

Row(
  children: [
    ConditionWidget(
      condition: starred,
      widget: Icon(
        Icons.favorite
      ),
      fallback: fallbackWidget
    ),
    ConditionWidget(
      condition: archived,
      widget: Icon(
        Icons.archive
      )
    )  
  ]
)
ConditionBuilder(
  condition: (_) => someCondition,
  trueBuilder: (_) => trueWidget,
  fallbackBuilder: (_) => fallbackWidget
);

builders:

ConditionalBuilder.widget(
  context: context,
  condition: (_) => _evaluateSomething(),
  trueBuilder: (_) => trueWidget,
  fallbackBuilder: (_) => fallbackWidget,
);

Switch case widgets and builders:

final Widget targetConditionWidget = Container();
final Widget fallbackWidget = Container();

SwitchCaseBuilder.widget<String>(
  context: context,
  condition: (_) => '1',
  caseBuilders: {'1': (_) => targetConditionWidget, '2': (_) => Container()},
  fallbackBuilder: (_) => fallbackWidget,
);  

Conditional parent widget

ConditionalWrap(
  shouldWrap: shouldWrapChildInParent,
  child: Container(),
  parentBuilder: (child) => Container(
    child: child,
  ),
)

try/catch builders

SafeBuilder(
  widgetBuilder: (_) => validWidget,
  fallbackBuilder: (e, _) => fallbackWidget, // called if widgetBuilder fails with error
)

Conditionally render a single widget or a list of widgets

ConditionalBuilder.widgets(
  context: context,
  condition: (_) => true,
  trueBuilder: (_) => [Container(), Container()],
  fallbackBuilder: (_) => [],
)

Some more helpers like:

DebugOnlyWidget, WebOnlyWidget etc

Contributions

Feel free to report bugs, request new features or to contribute to this project!

About

A complete set of Flutter tools for conditional rendering (if-else and switch conditions), subtree wrapping with a parent widget, and some handy shortcuts (like DebugWidget, WebOnlyWidget, SafeBuilder, and many more).

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Dart 100.0%