Last active
May 8, 2023 15:36
-
-
Save Blquinn/072fa91d6da3927a2d1d5aa7e57c8ffa to your computer and use it in GitHub Desktop.
Opacity Animation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp(home: HomePage()); | |
} | |
} | |
class HomePage extends StatelessWidget { | |
HomePage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: SizedBox( | |
width: double.infinity, | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Stack( | |
alignment: AlignmentDirectional.center, | |
children: const [ | |
Text('Hello world'), | |
OpacityAnimation(), | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class OpacityAnimation extends StatefulWidget { | |
const OpacityAnimation({super.key}); | |
@override | |
_OpacityAnimationState createState() => _OpacityAnimationState(); | |
} | |
class _OpacityAnimationState extends State<OpacityAnimation> | |
with SingleTickerProviderStateMixin { | |
late Animation<Color?> _colorAnimation; | |
late AnimationController _controller; | |
late CurvedAnimation _curve; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: const Duration(milliseconds: 600), | |
vsync: this, | |
); | |
_curve = CurvedAnimation(parent: _controller, curve: Curves.easeInOut); | |
_colorAnimation = | |
ColorTween(begin: Colors.transparent, end: Colors.blue).animate(_curve); | |
_controller.addListener(() => setState(() {})); | |
_controller.addStatusListener((status) { | |
if (status == AnimationStatus.completed) { | |
_controller.reverse(); | |
} else if (status == AnimationStatus.dismissed) { | |
_controller.forward(); | |
} | |
}); | |
_controller.forward(); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
_controller.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: <Widget>[ | |
Container( | |
margin: const EdgeInsets.all(8.0), | |
color: _colorAnimation.value, | |
height: 25, | |
width: 2, | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment