import 'package:flutter/material.dart'; import '../../../controllers/http_status_manger/http_status_manger.dart'; import '../../../controllers/localization/localization.dart'; import '../../../models/subtitle.dart'; import 'app_loading.dart'; import 'failed_loading.dart'; class LoadingManager 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; LoadingManager({ Key? key, this.isLoading, this.isFailedLoading, this.stateCode, this.onRefresh, this.child, this.progress, this.isNotPage = false, this.askOnBack = false, }) : super(key: key); @override State createState() => _LoadingManagerState(); } class _LoadingManagerState extends State { @override void initState() { if(widget.onRefresh != null && widget.stateCode == null){ WidgetsBinding.instance.addPostFrameCallback((timeStamp) { widget.onRefresh!(); }); } super.initState(); } @override Widget build(BuildContext context) { Subtitle? subtitle = AppLocalization.of(context)?.subtitle; Widget? placeHolder; // to load data if load not start if(widget.isLoading == false && widget.stateCode == null){ WidgetsBinding.instance.addPostFrameCallback((timeStamp) { widget.onRefresh!(); }); } // if loading of still not start in loading (true or null) // return loading widget if(widget.isLoading != false || widget.stateCode == null){ placeHolder = ALoading(); }else if((widget.isFailedLoading??false) && !(widget.isNotPage??false)){ // if failed return failed widget placeHolder = FailedLoading( message: HttpStatusManger.getStatusMessage( status: widget.stateCode, subtitle: subtitle), onReload: widget.onRefresh, ); } // if load end successfully return loaded widget return RefreshIndicator( onRefresh: () async{ await widget.onRefresh!(); }, child: AnimatedSwitcher( duration: const Duration(milliseconds: 400), child: placeHolder ?? widget.child, ), ); } }