cargando.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:google_fonts/google_fonts.dart';
  4. class Cargando extends StatefulWidget {
  5. final String text;
  6. const Cargando({
  7. Key? key,
  8. required this.text
  9. }) : super (key: key);
  10. @override
  11. _CargandoState createState() => _CargandoState();
  12. }
  13. class _CargandoState extends State<Cargando> {
  14. late Timer _timer;
  15. int _start = 3;
  16. void _startTimer(){
  17. const oneSec = Duration(
  18. seconds: 1,
  19. );
  20. _timer = Timer.periodic(
  21. oneSec,
  22. (timer){
  23. if(_start == 0){
  24. setState((){
  25. _start = 3;
  26. });
  27. }else if(_start == 2){
  28. setState((){
  29. _start--;
  30. });
  31. }else{
  32. setState(() => _start--);
  33. }
  34. }
  35. );
  36. }
  37. @override
  38. void dispose() {
  39. super.dispose();
  40. _timer.cancel();
  41. }
  42. @override
  43. void initState() {
  44. super.initState();
  45. _startTimer();
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. return AnimatedCrossFade(
  50. firstChild: Text(
  51. widget.text,
  52. textAlign: TextAlign.center,
  53. style: GoogleFonts.robotoSlab(
  54. fontWeight: FontWeight.bold,
  55. color: const Color(0xFFB40404),
  56. fontSize: 16.0,
  57. ),
  58. ),
  59. secondChild: Text(
  60. widget.text,
  61. textAlign: TextAlign.center,
  62. style: GoogleFonts.robotoSlab(
  63. fontWeight: FontWeight.bold,
  64. color: const Color(0x55B40404),
  65. fontSize: 16.0,
  66. ),
  67. ),
  68. duration: const Duration(
  69. seconds: 2,
  70. ),
  71. crossFadeState: _start > 0 && _start <= 2 ? CrossFadeState.showFirst : CrossFadeState.showSecond,
  72. );
  73. }
  74. }