Skip to content

Instantly share code, notes, and snippets.

@olumidayy
Created December 4, 2020 11:39
Show Gist options
  • Save olumidayy/a5ff89fb28c04890677145d6bc1d670c to your computer and use it in GitHub Desktop.
Save olumidayy/a5ff89fb28c04890677145d6bc1d670c to your computer and use it in GitHub Desktop.
A basic way to animate shapes
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double index = 0;
void _animateShape() {
setState(() {
index = index == 2 ? 0 : index + 1;
});
print(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Stack(
alignment: Alignment.center,
children: <Widget>[
AnimatedOpacity(
duration: Duration(seconds: 1),
opacity: index == 0 ? 1 : 0,
child: Container(
height: 200,
width: 200,
decoration: BoxDecoration(
color: Colors.yellow,
borderRadius: BorderRadius.circular(20),
)
)
),
AnimatedOpacity(
duration: Duration(seconds: 1),
opacity: index == 1 ? 1 : 0,
child: Container(
height: 150,
width: 150,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(75),
)
)
),
AnimatedOpacity(
duration: Duration(seconds: 1),
opacity: index == 2 ? 1 : 0,
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(20),
)
)
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _animateShape,
tooltip: 'Increment',
child: Icon(Icons.switch_left),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment