Created
November 28, 2020 10:38
-
-
Save taciomedeiros/81e546b53287881cb528e2b8a5ec09f7 to your computer and use it in GitHub Desktop.
Implicit animation 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
| import 'dart:math'; | |
| import 'package:flutter/material.dart'; | |
| const _duration = Duration(milliseconds: 400); | |
| double randomBorderRadius() { | |
| return Random().nextDouble() * 64; | |
| } | |
| double randomMargin() { | |
| return Random().nextDouble() * 64; | |
| } | |
| Color randomColor() { | |
| return Color(0xFFFFFFFF & Random().nextInt(0xFFFFFFFF)); | |
| } | |
| class AnimatedContainerDemo extends StatefulWidget { | |
| _AnimatedContainerDemoState createState() => _AnimatedContainerDemoState(); | |
| } | |
| class _AnimatedContainerDemoState extends State<AnimatedContainerDemo> { | |
| Color color; | |
| double borderRadius; | |
| double margin; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| color = Colors.deepPurple; | |
| borderRadius = randomBorderRadius(); | |
| margin = randomMargin(); | |
| } | |
| void change() { | |
| setState(() { | |
| color = randomColor(); | |
| borderRadius = randomBorderRadius(); | |
| margin = randomMargin(); | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Center( | |
| child: Column( | |
| children: <Widget>[ | |
| SizedBox( | |
| width: 128, | |
| height: 128, | |
| child: AnimatedContainer( | |
| margin: EdgeInsets.all(margin), | |
| decoration: BoxDecoration( | |
| color: color, | |
| borderRadius: BorderRadius.circular(borderRadius), | |
| ), | |
| duration: _duration, | |
| ), | |
| ), | |
| ElevatedButton( | |
| child: Text('change'), | |
| onPressed: () => change(), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| home: AnimatedContainerDemo(), | |
| ); | |
| } | |
| } | |
| void main() { | |
| runApp( | |
| MyApp(), | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment