Skip to content

Instantly share code, notes, and snippets.

@vogdb
Last active August 19, 2022 18:45
Show Gist options
  • Save vogdb/170945f94615d4e7474e540c58e60cc2 to your computer and use it in GitHub Desktop.
Save vogdb/170945f94615d4e7474e540c58e60cc2 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SquareAnimation(),
);
}
}
class SquareAnimation extends StatefulWidget {
@override
State<SquareAnimation> createState() {
return SquareAnimationState();
}
}
class SquareAnimationState extends State<SquareAnimation> {
static const squareSize = 50.0;
bool isLeft = true;
bool isAnimating = false;
@override
Widget build(BuildContext context) {
return Column(
children: [
SizedBox(
height: 100,
child: AnimatedAlign(
alignment: isLeft ? Alignment.centerLeft : Alignment.centerRight,
duration: const Duration(seconds: 1),
onEnd: () => setState(() {
isAnimating = false;
}),
child: Container(
width: squareSize,
height: squareSize,
decoration: BoxDecoration(
color: Colors.red,
border: Border.all(),
),
),
),
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed: isLeft || isAnimating
? null
: () => setState(() {
isLeft = true;
isAnimating = true;
}),
child: const Text("To the left")),
ElevatedButton(
onPressed: isLeft && !isAnimating
? () => setState(() {
isLeft = false;
isAnimating = true;
})
: null,
child: const Text("To the right"))
],
)
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment