Created
October 2, 2021 16:14
-
-
Save gladimdim/116f0f3b5f3937698bb3316367a3edf6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: const Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
); | |
} | |
} | |
class ScaleAnimated extends StatefulWidget { | |
final Widget child; | |
final Duration duration; | |
const ScaleAnimated({Key? key, required this.child, required this.duration}) | |
: super(key: key); | |
@override | |
_ScaleAnimatedState createState() => _ScaleAnimatedState(); | |
} | |
class _ScaleAnimatedState extends State<ScaleAnimated> | |
with SingleTickerProviderStateMixin { | |
late final AnimationController _controller; | |
late final Animation<double> _animation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
vsync: this, | |
duration: widget.duration, | |
upperBound: 1.0, | |
lowerBound: 0.0); | |
_controller.forward(); | |
_animation = CurvedAnimation(parent: _controller, curve: Curves.linear); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ScaleTransition( | |
scale: _animation, | |
child: widget.child, | |
); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
} | |
class MyWidget extends StatefulWidget { | |
const MyWidget({Key? key}) : super(key: key); | |
@override | |
_MyWidgetState createState() => _MyWidgetState(); | |
} | |
class _MyWidgetState extends State<MyWidget> { | |
int counter = 0; | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
ScaleAnimated( | |
duration: const Duration(seconds: 2), | |
child: Text("Widget without key animates only once: $counter"), | |
), | |
ScaleAnimated( | |
key: ValueKey(counter), | |
duration: const Duration(seconds: 2), | |
child: Text("Widget with key animates each time the key is different: $counter"), | |
), | |
ElevatedButton(child: const Text("Press me"), onPressed: _pressed), | |
], | |
); | |
} | |
void _pressed() { | |
setState(() { | |
counter++; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment