loading_overlay.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import 'dart:ui';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_spinkit/flutter_spinkit.dart';
  4. class LoadingOverlay extends StatefulWidget {
  5. const LoadingOverlay({
  6. Key? key,
  7. required this.child,
  8. this.delay = const Duration(milliseconds: 500),
  9. }) : super(key: key);
  10. final Widget child;
  11. final Duration delay;
  12. static LoadingOverlayState of(BuildContext context) {
  13. return context.findAncestorStateOfType<LoadingOverlayState>()!;
  14. }
  15. @override
  16. State<LoadingOverlay> createState() => LoadingOverlayState();
  17. }
  18. class LoadingOverlayState extends State<LoadingOverlay> {
  19. bool _isLoading = false;
  20. void show() {
  21. setState(() {
  22. _isLoading = true;
  23. });
  24. }
  25. void hide() {
  26. setState(() {
  27. _isLoading = false;
  28. });
  29. }
  30. @override
  31. Widget build(BuildContext context) {
  32. return Stack(
  33. children: [
  34. widget.child,
  35. if (_isLoading)
  36. BackdropFilter(
  37. filter: ImageFilter.blur(sigmaX: 4.0, sigmaY: 4.0),
  38. child: const Opacity(
  39. opacity: 0.2,
  40. child: ModalBarrier(dismissible: false, color: Colors.black),
  41. ),
  42. ),
  43. if (_isLoading)
  44. const SpinKitDualRing(
  45. color: Colors.red,
  46. size: 50.0,
  47. ),
  48. ],
  49. );
  50. }
  51. }