import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; class LoaderWidget extends StatefulWidget { final double size; final bool isContained; const LoaderWidget({super.key, required this.size, required this.isContained }); @override _LoaderWidgetState createState() => _LoaderWidgetState(); } class _LoaderWidgetState extends State with SingleTickerProviderStateMixin { late AnimationController _rotationController; int _currentShapeIndex = 0; late Timer _shapeTimer; late Animation _rotationAnimation; bool _isInitialized = false; final List _svgShapes = [ (color) => ''' ''', (color) => ''' ''', (color) => ''' ''', (color) => ''' ''', (color) => ''' ''', (color) => ''' ''', (color) => ''' ''' ]; @override void initState() { super.initState(); _rotationController = AnimationController( vsync: this, duration: Duration(milliseconds: 800), )..repeat(); _rotationAnimation = CurvedAnimation( parent: _rotationController, curve: Curves.easeInOut, ); Future.delayed(Duration(milliseconds: 0), () { setState(() { _isInitialized = true; }); }); _shapeTimer = Timer.periodic(Duration(milliseconds: 800), (_) { setState(() { _currentShapeIndex = (_currentShapeIndex + 1) % _svgShapes.length; }); }); } @override void dispose() { _rotationController.dispose(); _shapeTimer.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return widget.isContained ? Container( decoration: BoxDecoration( color: Theme.of(context).colorScheme.primaryContainer, borderRadius: BorderRadius.circular(50), ), child: animationBlock()) : animationBlock(); } Widget animationBlock() { return AnimatedBuilder( animation: _rotationController, builder: (context, child) { return Transform.rotate( angle: _rotationAnimation.value * 2 * 3.14159, child: AnimatedSwitcher( duration: Duration(milliseconds: 500), transitionBuilder: (child, animation) { return ScaleTransition( scale: CurvedAnimation( parent: animation, curve: Curves.easeOutBack, ), child: child, ); }, child: SvgPicture.string( _svgShapes[_currentShapeIndex](colorToHex(Theme.of(context).colorScheme.primary)), key: ValueKey(_svgShapes[_currentShapeIndex]), width: widget.size, height: widget.size, ), ), ); }, ); } } String colorToHex(Color color) => '#${color.value.toRadixString(16).substring(2).toUpperCase()}';