asSkeleton method
Widget
asSkeleton({
- bool enabled = true,
- bool leaf = false,
- Widget? replacement,
- bool unite = false,
- 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 activeleaf
(bool, default: false): Use leaf mode for simple widgetsreplacement
(Widget?, optional): Custom replacement widget in replace modeunite
(bool, default: false): Use unite mode for complex layoutssnapshot
(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);
}