Flutter Conditional Wrap 🌯

Pub

A widget that allows you to conditionally wrap a child subtree with a parent widget

Usage

Conditional Wrap

ConditionalWrap widget takes a boolean condition parameter and wraps the child subtree with builder if the condition is true.

ConditionalWrap(
  condition: shouldIncludeParent,
  builder: (child) => ParentWidget(child: child),
  child: ChildSubtree(),
),

You can also provide a fallback builder to use when the condition is false.

ConditionalWrap(
  condition: _shouldUseBuilder,
  builder: (child) => ParentWidget(child: child),
  fallback: (child) => FallbackParent(child: child),
  child: ChildSubtree(),
),

Null Safe Wrap

NullSafeWrap<T> widget takes a generic T? value parameter and wraps the child subtree with builder if the value is not null. This builder provides T value argument that can be used safely.

Similarly to conditional wrap, you can provide a fallback to use when the value is null.

NullSafeWrap<Color>(
  value: _color,
  builder: (color, child) => ColoredBox(
    color: color,
    child: child,
  ),
  child: Text('hello, friend.'),
),

Libraries

conditional_wrap