import 'package:flutter/material.dart'; import 'package:test_sa/extensions/context_extension.dart'; import 'package:test_sa/extensions/int_extensions.dart'; import 'package:test_sa/new_views/common_widgets/app_lazy_loading.dart'; import '../app_style/app_color.dart'; class AppLoadingManager extends StatefulWidget { final bool isLoading; final bool isFailedLoading; final bool isNotPage; final int? progress; final bool askOnBack; final int? stateCode; final Future Function() onRefresh; final Widget child; const AppLoadingManager({ Key? key, required this.isLoading, required this.isFailedLoading, required this.onRefresh, required this.child, this.stateCode, this.progress, this.isNotPage = false, this.askOnBack = false, }) : super(key: key); @override State createState() => _AppLoadingManagerState(); } class _AppLoadingManagerState extends State { @override void initState() { if (widget.stateCode == null) { WidgetsBinding.instance.addPostFrameCallback((timeStamp) { widget.onRefresh(); }); } super.initState(); } @override Widget build(BuildContext context) { Widget? placeHolder; // if loading is still not started or in progress if (widget.isLoading || widget.stateCode == null) { placeHolder = Container( height: 60.toScreenHeight, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: context.isDark ? AppColor.neutral50 : Colors.white, ), child: const AppLazyLoading(), ); } else if (widget.isFailedLoading && !widget.isNotPage) { // if failed, return failed widget placeHolder = Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(context.translation.httpError, style: Theme.of(context).textTheme.titleMedium), 4.height, OutlinedButton( onPressed: widget.onRefresh, child: Text(context.translation.tryAgain), ), ], ), ); } // if loading ended successfully, return loaded widget return RefreshIndicator( onRefresh: widget.onRefresh, child: AnimatedSwitcher( duration: const Duration(milliseconds: 400), child: placeHolder ?? widget.child, ), ); } }