didvan-app/lib/widgets/state_handlers/sliver_state_handler.dart

64 lines
2.1 KiB
Dart

import 'package:didvan/models/enums.dart';
import 'package:didvan/providers/core_provider.dart';
import 'package:didvan/widgets/state_handlers/empty_connection.dart';
import 'package:flutter/material.dart';
class SliverStateHandler<T extends CoreProvier> 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;
SliverStateHandler({
Key? key,
required this.state,
required this.builder,
required this.childCount,
required this.onRetry,
this.itemPadding,
this.placeholder,
this.emptyState,
this.enableEmptyState = false,
}) : super(
key: key,
delegate: SliverChildBuilderDelegate(
(context, index) {
if (state.appState == AppState.failed) {
return SizedBox(
height: MediaQuery.of(context).size.height - 240,
child: EmptyConnection(
onRetry: onRetry,
),
);
}
if (enableEmptyState && state.appState == AppState.idle) {
return SizedBox(
height: MediaQuery.of(context).size.height - 240,
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,
),
);
}