Created
April 13, 2022 05:56
-
-
Save imrhk/f391fc075e2cbcc70c4d3e384eb0eada to your computer and use it in GitHub Desktop.
AnimatedPositioned example
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'; | |
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