container.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'theme.dart';
  4. class LoadingContainer extends StatefulWidget {
  5. final Widget indicator;
  6. final String status;
  7. final bool animation;
  8. LoadingContainer({
  9. Key key,
  10. this.indicator,
  11. this.status,
  12. this.animation = true,
  13. }) : super(key: key);
  14. @override
  15. LoadingContainerState createState() => LoadingContainerState();
  16. }
  17. class LoadingContainerState extends State<LoadingContainer> {
  18. double _opacity = 0.0;
  19. Duration _animationDuration;
  20. String _status;
  21. @override
  22. void initState() {
  23. super.initState();
  24. _status = widget.status;
  25. _animationDuration = widget.animation
  26. ? const Duration(milliseconds: 300)
  27. : const Duration(milliseconds: 0);
  28. if (widget.animation) {
  29. Future.delayed(const Duration(milliseconds: 30), () {
  30. if (!mounted) return;
  31. setState(() {
  32. _opacity = 1.0;
  33. });
  34. });
  35. } else {
  36. setState(() {
  37. _opacity = 1.0;
  38. });
  39. }
  40. }
  41. @override
  42. void dispose() {
  43. super.dispose();
  44. }
  45. void dismiss(Completer completer) {
  46. _animationDuration = const Duration(milliseconds: 300);
  47. setState(() {
  48. _opacity = 0.0;
  49. });
  50. Future.delayed(_animationDuration, () {
  51. completer.complete();
  52. });
  53. }
  54. void updateStatus(String status) {
  55. setState(() {
  56. _status = status;
  57. });
  58. }
  59. @override
  60. Widget build(BuildContext context) {
  61. Widget loading = Align(
  62. alignment: EasyLoadingTheme.alignment,
  63. child: Container(
  64. margin:EasyLoadingTheme.contentMargin,
  65. decoration: BoxDecoration(
  66. color: EasyLoadingTheme.backgroundColor,
  67. borderRadius: BorderRadius.circular(
  68. EasyLoadingTheme.radius,
  69. ),
  70. ),
  71. padding: EasyLoadingTheme.contentPadding,
  72. child: Column(
  73. mainAxisAlignment: MainAxisAlignment.end,
  74. crossAxisAlignment: CrossAxisAlignment.center,
  75. mainAxisSize: MainAxisSize.min,
  76. children: <Widget>[
  77. widget.indicator != null
  78. ? Container(
  79. margin: _status?.isNotEmpty == true
  80. ? EasyLoadingTheme.textPadding
  81. : EdgeInsets.zero,
  82. child: widget.indicator,
  83. )
  84. : null,
  85. _status?.isNotEmpty == true
  86. ? Text(
  87. _status,
  88. style: TextStyle(
  89. color: EasyLoadingTheme.textColor,
  90. fontSize: EasyLoadingTheme.fontSize,
  91. decoration: TextDecoration.none,
  92. fontWeight: FontWeight.normal),
  93. textAlign: EasyLoadingTheme.textAlign,
  94. )
  95. : null,
  96. ].where((w) => w != null).toList(),
  97. ),
  98. ),
  99. );
  100. return AnimatedOpacity(
  101. opacity: _opacity,
  102. duration: _animationDuration,
  103. child: Stack(
  104. children: <Widget>[
  105. IgnorePointer(
  106. ignoring: EasyLoadingTheme.ignoring,
  107. child: Container(
  108. width: double.infinity,
  109. height: double.infinity,
  110. color: EasyLoadingTheme.maskColor,
  111. ),
  112. ),
  113. loading,
  114. ],
  115. ),
  116. );
  117. }
  118. }