Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rrousselGit/1870e726d7e04699bc8f9d78ba71da35 to your computer and use it in GitHub Desktop.
Save rrousselGit/1870e726d7e04699bc8f9d78ba71da35 to your computer and use it in GitHub Desktop.
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,
),
);
}
@craiglabenz
Copy link

This updated code fixes a few analysis warnings on DartPad. Thanks for putting this together!

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(),
            ),
            ElevatedButton(
              onPressed: () => setState(() => showCircle = !showCircle),
              child: const 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 const SizedBox(
      width: 50,
      height: 50,
      child: Material(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(50)),
        ),
        color: Colors.red,
        clipBehavior: Clip.hardEdge,
      ),
    );
  }
}

Widget circle() {
  return const SizedBox(
    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