import 'package:flutter/material.dart'; import '../infrastructure/controller.dart'; import '../infrastructure/loadable_state.dart'; import '../infrastructure/state_extensions.dart'; class LoadableControllerConsumer, TState extends LoadableState> extends StatelessWidget { final Widget Function(BuildContext context, TState state) child; const LoadableControllerConsumer({required this.child, super.key}); @override Widget build(BuildContext context) { var state = context.readController().state; return Stack( children: [ if(!state.hasStateData()) const Center(child: CircularProgressIndicator()), AnimatedSwitcher( duration: const Duration(milliseconds: 100), transitionBuilder: (Widget child, Animation animation) => SlideTransition( position: Tween( begin: const Offset(0.0, -1.0), end: Offset.zero, ).animate(animation), child: child, ), child: state.isBackgroundLoading() ? const LinearProgressIndicator() : const SizedBox.shrink(), ), AnimatedOpacity( opacity: state.hasStateData() ? 1.0 : 0.0, duration: const Duration(milliseconds: 100), curve: Curves.easeInOut, child: state.hasStateData() ? child(context, state) : const SizedBox.shrink() ), ], ); } }