You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/widgets/loaders/loading_manager.dart

82 lines
2.3 KiB
Dart

import 'package:flutter/material.dart';
3 years ago
import 'package:test_sa/controllers/http_status_manger/http_status_manger.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/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<void> Function() onRefresh;
final Widget child;
LoadingManager({
Key key,
@required this.isLoading,
@required this.isFailedLoading,
@required this.stateCode,
@required this.onRefresh,
@required this.child,
this.progress,
this.isNotPage = false,
this.askOnBack = false,
}) : super(key: key);
@override
State<LoadingManager> createState() => _LoadingManagerState();
}
class _LoadingManagerState extends State<LoadingManager> {
@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 && !widget.isNotPage){
// 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,
),
);
}
}