Created
June 3, 2025 05:41
-
-
Save gsandaru/9b9482cfce159483b81b82bd67c558ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'AnimationController Example', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { | |
late AnimationController _controller; | |
late Animation<double> _animation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
vsync: this, // requires a TickerProvider | |
duration: const Duration(seconds: 2), | |
); | |
_animation = Tween<double>(begin: 100, end: 200).animate(_controller) | |
..addListener(() { | |
setState(() { | |
// The state that has changed here is managed by _animation | |
}); | |
}); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('AnimationController Example'), | |
), | |
body: Column( | |
children: <Widget>[ | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
ElevatedButton( | |
onPressed: () { | |
_controller.forward(); | |
}, | |
child: const Text('Forward'), | |
), | |
const SizedBox(width: 10), | |
ElevatedButton( | |
onPressed: () { | |
_controller.reverse(); | |
}, | |
child: const Text('Reverse'), | |
), | |
const SizedBox(width: 10), | |
ElevatedButton( | |
onPressed: () { | |
if (_controller.isAnimating) { | |
_controller.stop(); | |
} else { | |
_controller.repeat(); | |
} | |
}, | |
child: const Text('Repeat/Stop'), | |
), | |
], | |
), | |
Expanded( | |
child: Center( | |
child: Container( | |
width: _animation.value, | |
height: _animation.value, | |
color: Colors.red, | |
), | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment