Last active
August 10, 2021 20:19
-
-
Save funwithflutter/4d44b0f2ca4bb5829439c5d6fc094f2d to your computer and use it in GitHub Desktop.
An example of an AnimatedContainer. To be used as an example.
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
| Look at the curve attribute in the AnimatedContainer. |
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'; | |
| void main() => runApp(const MyApp()); | |
| class MyApp extends StatelessWidget { | |
| const MyApp({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return const MaterialApp( | |
| title: 'Implicit Animation Example', | |
| home: Scaffold( | |
| body: Center( | |
| child: Example(), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class Example extends StatefulWidget { | |
| const Example({Key? key}) : super(key: key); | |
| @override | |
| _ExampleState createState() => _ExampleState(); | |
| } | |
| class _ExampleState extends State<Example> { | |
| static const double _startWidth = 100.0; | |
| static const double _startHeight = 100.0; | |
| static const Color _startColor = Colors.red; | |
| double width = _startWidth; | |
| double height = _startHeight; | |
| Color color = _startColor; | |
| void _animateContainer() { | |
| setState(() { | |
| width = (width == _startWidth) ? _startWidth * 2 : _startWidth; | |
| height = (height == _startHeight) ? _startHeight * 2 : _startHeight; | |
| color = (color == _startColor) ? Colors.blue : _startColor; | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return GestureDetector( | |
| onTap: _animateContainer, | |
| child: AnimatedContainer( | |
| duration: const Duration(milliseconds: 600), | |
| curve: Curves.linear, | |
| width: width, | |
| height: height, | |
| color: color, | |
| ), | |
| ); | |
| } | |
| } |
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'; | |
| void main() => runApp(const MyApp()); | |
| class MyApp extends StatelessWidget { | |
| const MyApp({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return const MaterialApp( | |
| title: 'Implicit Animation Example', | |
| home: Scaffold( | |
| body: Center( | |
| child: Example(), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class Example extends StatefulWidget { | |
| const Example({Key? key}) : super(key: key); | |
| @override | |
| _ExampleState createState() => _ExampleState(); | |
| } | |
| class _ExampleState extends State<Example> { | |
| static const double _startWidth = 100.0; | |
| static const double _startHeight = 100.0; | |
| static const Color _startColor = Colors.red; | |
| double width = _startWidth; | |
| double height = _startHeight; | |
| Color color = _startColor; | |
| void _animateContainer() { | |
| setState(() { | |
| width = (width == _startWidth) ? _startWidth * 2 : _startWidth; | |
| height = (height == _startHeight) ? _startHeight * 2 : _startHeight; | |
| color = (color == _startColor) ? Colors.blue : _startColor; | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return GestureDetector( | |
| onTap: _animateContainer, | |
| child: AnimatedContainer( | |
| duration: const Duration(milliseconds: 600), | |
| curve: Curves.bounceOut, // CHANGE THIS | |
| width: width, | |
| height: height, | |
| color: color, | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment