Last active
August 10, 2021 21:05
-
-
Save funwithflutter/e10fcbfdaf6d4b030d2e5486e62d16a6 to your computer and use it in GitHub Desktop.
Flutter AnimatedBuilder example
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 'dart:math'; | |
| 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 MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| title: 'Animation Course', | |
| home: Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Explicit Animations'), | |
| ), | |
| body: const Center( | |
| child: AnimatedBuilderExample(), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class AnimatedBuilderExample extends StatefulWidget { | |
| const AnimatedBuilderExample({Key? key}) : super(key: key); | |
| @override | |
| _AnimatedBuilderExampleState createState() => _AnimatedBuilderExampleState(); | |
| } | |
| class _AnimatedBuilderExampleState extends State<AnimatedBuilderExample> | |
| with SingleTickerProviderStateMixin { | |
| late AnimationController _controller; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _controller = | |
| AnimationController(vsync: this, duration: const Duration(seconds: 10)); | |
| _controller.repeat(); | |
| } | |
| @override | |
| void dispose() { | |
| _controller.dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return AnimatedBuilder( | |
| animation: _controller, | |
| builder: (context, child) { | |
| return Transform.rotate( | |
| angle: _controller.value * 2 * pi, | |
| child: child, | |
| ); | |
| }, | |
| child: Container(color: Colors.red, width: 100, height: 100), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment