Created
December 11, 2023 23:18
-
-
Save amendezcabrera/e1eb20f2c14250ec1d760ca8422d5823 to your computer and use it in GitHub Desktop.
Widget Fade In Flutter Demo
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(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: FadeInDemo(), | |
); | |
} | |
} | |
class FadeInDemo extends StatefulWidget { | |
@override | |
_FadeInDemoState createState() => _FadeInDemoState(); | |
} | |
class _FadeInDemoState extends State<FadeInDemo> with SingleTickerProviderStateMixin { | |
late AnimationController _controller; | |
late Animation<double> _animation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: const Duration(seconds: 3), | |
vsync: this, | |
); | |
_animation = CurvedAnimation(parent: _controller, curve: Curves.easeInOut); | |
// Repeat the animation forward and reverse | |
_controller.repeat(reverse: true); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Fade In Demo'), | |
), | |
body: Center( | |
child: FadeTransition( | |
opacity: _animation, | |
child: Container( | |
width: 200, | |
height: 200, | |
decoration: BoxDecoration( | |
color: Colors.blue, | |
borderRadius: BorderRadius.circular(20), | |
), | |
child: Center( | |
child: Text( | |
'Flutter!', | |
style: TextStyle(fontSize: 24.0, color: Colors.white), | |
), | |
), | |
), | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
// Restart the animation when the button is pressed | |
if (_controller.status == AnimationStatus.completed) { | |
_controller.reverse(); | |
} else if (_controller.status == AnimationStatus.dismissed) { | |
_controller.forward(); | |
} | |
}, | |
child: Icon(Icons.refresh), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment