Created
July 19, 2020 09:21
-
-
Save rrousselGit/1870e726d7e04699bc8f9d78ba71da35 to your computer and use it in GitHub Desktop.
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'; | |
// This example showcases how, by using functions over classes, | |
// this _can_ break features such as AnimatedSwitcher | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(context) { | |
return MaterialApp( | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
bool showCircle = false; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
AnimatedSwitcher( | |
duration: const Duration(seconds: 1), | |
child: showCircle ? Circle() : Square(), | |
// Uncomment to break the animation | |
// child: showCircle ? circle() : square(), | |
), | |
RaisedButton( | |
onPressed: () => setState(() => showCircle = !showCircle), | |
child: Text( | |
'Click me to do a fade transition between square and a circle', | |
), | |
) | |
], | |
), | |
), | |
); | |
} | |
} | |
class Square extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: 50, | |
height: 50, | |
color: Colors.red, | |
); | |
} | |
} | |
Widget square() { | |
return Container( | |
width: 50, | |
height: 50, | |
color: Colors.red, | |
); | |
} | |
class Circle extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: 50, | |
height: 50, | |
child: Material( | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.all(Radius.circular(50)), | |
), | |
color: Colors.red, | |
clipBehavior: Clip.hardEdge, | |
), | |
); | |
} | |
} | |
Widget circle() { | |
return Container( | |
width: 50, | |
height: 50, | |
child: Material( | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.all(Radius.circular(50)), | |
), | |
color: Colors.red, | |
clipBehavior: Clip.hardEdge, | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This updated code fixes a few analysis warnings on DartPad. Thanks for putting this together!