Skip to content

Instantly share code, notes, and snippets.

@imrhk
Created April 13, 2022 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imrhk/f391fc075e2cbcc70c4d3e384eb0eada to your computer and use it in GitHub Desktop.
Save imrhk/f391fc075e2cbcc70c4d3e384eb0eada to your computer and use it in GitHub Desktop.
AnimatedPositioned example
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final targetX = 200;
final targetY = 200;
int currentX = 0;
int currentY = 0;
@override
void initState() {
super.initState();
Future.delayed(const Duration(seconds: 3), () {
if (mounted) {
setState(() {
currentX = targetX;
currentY = targetY;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('AnimatedPositionedExample')),
body: Stack(
children: [
AnimatedPositioned(
child: Container(color: Colors.lime, height: 50, width: 50),
left: currentX.toDouble(),
top: currentY.toDouble(),
duration: const Duration(seconds: 1),
curve: Curves.linear,
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment