import 'package:didvan/models/enums.dart'; import 'package:didvan/providers/core_provider.dart'; import 'package:didvan/widgets/didvan/text.dart'; import 'package:flutter/material.dart'; class SliverStateHandler extends SliverList { final T state; final Widget Function(BuildContext context, T state, int index) builder; final int childCount; final VoidCallback? onRefresh; final bool enableEmptyState; final Widget? emptyState; final Widget? placeholder; final EdgeInsets? itemPadding; SliverStateHandler({ Key? key, required this.state, required this.builder, required this.childCount, this.itemPadding, this.placeholder, this.emptyState, this.enableEmptyState = false, this.onRefresh, }) : super( key: key, delegate: SliverChildBuilderDelegate( (context, index) { if (state.appState == AppState.failed) { return const DidvanText('مشکل اتصال'); } if (enableEmptyState) { return emptyState; } if (state.appState == AppState.busy) { return Padding( padding: itemPadding ?? const EdgeInsets.all(0), child: placeholder, ); } return Padding( padding: itemPadding ?? const EdgeInsets.all(0), child: builder(context, state, index), ); }, childCount: state.appState == AppState.idle ? childCount : state.appState == AppState.busy ? 3 : 1, ), ); }