import 'package:didvan/models/enums.dart'; import 'package:didvan/providers/core.dart'; import 'package:didvan/views/widgets/state_handlers/empty_connection.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 onRetry; final bool enableEmptyState; final Widget? emptyState; final Widget? placeholder; final EdgeInsets? itemPadding; final bool centerEmptyState; SliverStateHandler({ Key? key, required this.state, required this.builder, required this.childCount, required this.onRetry, this.itemPadding, this.placeholder, this.emptyState, this.enableEmptyState = false, this.centerEmptyState = true, }) : super( key: key, delegate: SliverChildBuilderDelegate( (context, index) { if (state.appState == AppState.failed) { return Padding( padding: EdgeInsets.only( top: centerEmptyState ? 120 : 20, bottom: 20, ), child: EmptyConnection(onRetry: onRetry), ); } if (enableEmptyState && state.appState == AppState.idle) { return Padding( padding: EdgeInsets.only( top: centerEmptyState ? MediaQuery.of(context).size.height / 4 : 20, bottom: 20, ), child: 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 ? enableEmptyState ? 1 : childCount : state.appState == AppState.busy ? 3 : 1, ), ); }