Skip to content

Instantly share code, notes, and snippets.

@vicmans
Created February 11, 2021 03:30
Show Gist options
  • Select an option

  • Save vicmans/7ae7ee0a458a141c29dddb0f9489b7bf to your computer and use it in GitHub Desktop.

Select an option

Save vicmans/7ae7ee0a458a141c29dddb0f9489b7bf to your computer and use it in GitHub Desktop.
Implicit animation tutorial
import 'dart:math';
import 'package:flutter/material.dart';
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();
}
const _duration = Duration(milliseconds: 400);
class _AnimatedContainerDemoState extends State<AnimatedContainerDemo> {
Color color;
double borderRadius;
double margin;
@override
initState() {
color = randomColor();
borderRadius = randomBorderRadius();
margin = randomMargin();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
SizedBox(
width: 128,
height: 128,
child: AnimatedContainer(
duration: _duration,
curve: Curves.elasticOut,
margin: EdgeInsets.all(margin),
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(borderRadius),
),
),
),
ElevatedButton(
child: Text('change'),
onPressed: () => change(),
),
],
);
}
void change() {
setState(() {
color = randomColor();
borderRadius = randomBorderRadius();
margin = randomMargin();
});
}
}
const owl_url =
'https://raw.githubusercontent.com/flutter/website/master/src/images/owl.jpg';
class FadeInDemo extends StatefulWidget {
_FadeInDemoState createState() => _FadeInDemoState();
}
class _FadeInDemoState extends State<FadeInDemo> {
double opacity = 0.0;
@override
Widget build(BuildContext context) {
return Column(children: <Widget>[
Image.network(owl_url),
ElevatedButton(
child: Text(
'Show details',
),
onPressed: () => setState(() {
opacity = 1;
}),
),
AnimatedOpacity(
duration: Duration(seconds: 2),
opacity: opacity,
curve: Curves.bounceIn,
child: Column(
children: <Widget>[
Text('Type: Owl'),
Text('Age: 39'),
Text('Employment: None'),
],
),
)
]);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: ListView(
children: [
FadeInDemo(),
Center(
child: Text('Another example')
),
AnimatedContainerDemo()
],
),
),
);
}
}
void main() {
runApp(
MyApp(),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment