asSkeleton method

Widget asSkeleton({
  1. bool enabled = true,
  2. bool leaf = false,
  3. Widget? replacement,
  4. bool unite = false,
  5. AsyncSnapshot? snapshot,
})

Converts the widget to a skeleton with advanced configuration options.

Provides comprehensive skeleton transformation with multiple modes and automatic handling of problematic widget types. Supports AsyncSnapshot integration for automatic skeleton state management based on data loading.

Special handling:

  • Avatar and Image widgets use Skeleton.leaf to avoid rendering issues
  • snapshot parameter automatically enables/disables based on data state
  • Various skeleton modes (leaf, unite, replace) for different use cases

Parameters:

  • enabled (bool, default: true): Whether skeleton effect is active
  • leaf (bool, default: false): Use leaf mode for simple widgets
  • replacement (Widget?, optional): Custom replacement widget in replace mode
  • unite (bool, default: false): Use unite mode for complex layouts
  • snapshot (AsyncSnapshot?, optional): Auto-enable based on data loading state

Returns: A skeleton-wrapped widget with appropriate configuration for the specified mode.

Example:

FutureBuilder<String>(
  future: loadData(),
  builder: (context, snapshot) => Text(snapshot.data ?? 'Loading...').asSkeleton(
    snapshot: snapshot,
  ),
);

Implementation

Widget asSkeleton({
  bool enabled = true,
  bool leaf = false,
  Widget? replacement,
  bool unite = false,
  AsyncSnapshot? snapshot,
}) {
  if (snapshot != null) {
    enabled = !snapshot.hasData;
  }
  if (this is Avatar || this is Image) {
    // https://github.com/Milad-Akarie/skeletonizer/issues/17
    return Skeleton.leaf(enabled: enabled, child: this);
  }
  if (unite) {
    return Skeleton.unite(unite: enabled, child: this);
  }
  if (replacement != null) {
    return Skeleton.replace(replace: enabled, child: replacement);
  }
  if (leaf) {
    return Skeleton.leaf(enabled: enabled, child: this);
  }
  return Skeletonizer(enabled: enabled, child: this);
}