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.
		
		
		
		
		
			
		
			
				
	
	
		
			95 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			95 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Dart
		
	
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<void> Function() onRefresh;
 | 
						|
  final Widget child;
 | 
						|
 | 
						|
  const AppLoadingManager({
 | 
						|
    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<AppLoadingManager> createState() => _AppLoadingManagerState();
 | 
						|
}
 | 
						|
 | 
						|
class _AppLoadingManagerState extends State<AppLoadingManager> {
 | 
						|
  @override
 | 
						|
  void initState() {
 | 
						|
    if (widget.onRefresh != null && widget.stateCode == null) {
 | 
						|
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
 | 
						|
        widget.onRefresh();
 | 
						|
      });
 | 
						|
    }
 | 
						|
    super.initState();
 | 
						|
  }
 | 
						|
 | 
						|
  @override
 | 
						|
  Widget build(BuildContext context) {
 | 
						|
    Widget placeHolder;
 | 
						|
    // to load data if load not start
 | 
						|
    if (widget.isLoading != true && 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 = 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 load end successfully return loaded widget
 | 
						|
    return RefreshIndicator(
 | 
						|
      onRefresh: () async {
 | 
						|
        await widget.onRefresh();
 | 
						|
      },
 | 
						|
      child: AnimatedSwitcher(
 | 
						|
        duration: const Duration(milliseconds: 400),
 | 
						|
        child: placeHolder ?? widget.child,
 | 
						|
      ),
 | 
						|
    );
 | 
						|
  }
 | 
						|
}
 |