Created
September 11, 2025 17:56
-
-
Save gildaswise/5ad31ae34a88765f883b85e2d4145ee5 to your computer and use it in GitHub Desktop.
Explicação sobre funções que retornam Widget, com animação
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'; | |
| // Esse exemplo visa mostrar o que acontece ao utilizar | |
| // funções que retornam Widget ao invés de classes | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(context) { | |
| return MaterialApp(home: AnimationClass()); | |
| } | |
| } | |
| class Title extends StatelessWidget { | |
| const Title(); | |
| @override | |
| Widget build(context) { | |
| print('build Title - Stateless'); | |
| return Text('Stateless'); | |
| } | |
| } | |
| enum TipoDoExemplo { animationFunc, animationClass } | |
| /// Ao trocar o tipo do exemplo utilizando a variável abaixo você verá os resultados no terminal | |
| /// do canto inferior direito. | |
| var _tipoDoExemplo = TipoDoExemplo.animationFunc; | |
| class AnimationClass extends StatefulWidget { | |
| const AnimationClass({super.key}); | |
| @override | |
| State<AnimationClass> createState() => _AnimationClassState(); | |
| } | |
| class _AnimationClassState extends State<AnimationClass> | |
| with SingleTickerProviderStateMixin { | |
| late AnimationController _controller; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _controller = AnimationController(vsync: this); | |
| _controller.repeat(period: Duration(seconds: 5)); | |
| } | |
| Widget title(BuildContext context) { | |
| print('build Title - função'); | |
| return Text('Função'); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| print('build AnimationClass'); | |
| return AnimatedBuilder( | |
| animation: _controller, | |
| builder: (context, _) { | |
| return Scaffold( | |
| backgroundColor: Color.lerp( | |
| Colors.red, | |
| Colors.blue, | |
| _controller.value, | |
| )!, | |
| body: Center( | |
| child: switch (_tipoDoExemplo) { | |
| TipoDoExemplo.animationClass => const Title(), | |
| TipoDoExemplo.animationFunc => title(context), | |
| }, | |
| ), | |
| ); | |
| }, | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment