Last active
September 11, 2025 14:45
-
-
Save gildaswise/3feebfd21be415fad0ca4ee35725f36d to your computer and use it in GitHub Desktop.
Explicação sobre funções que retornam Widget usando TextField
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 Counter( | |
| count: Count(), | |
| child: MaterialApp(home: Home()), | |
| ); | |
| } | |
| } | |
| class Count extends ValueNotifier<int> { | |
| Count() : super(0); | |
| } | |
| class Counter extends InheritedNotifier { | |
| const Counter({Key? key, required this.count, required Widget child}) | |
| : super(key: key, child: child, notifier: count); | |
| final Count count; | |
| static Count of(BuildContext context, {bool listen = true}) { | |
| if (listen) { | |
| return context.dependOnInheritedWidgetOfExactType<Counter>()!.count; | |
| } else { | |
| final Counter counter = | |
| context.getElementForInheritedWidgetOfExactType<Counter>()!.widget | |
| as Counter; | |
| return counter.count; | |
| } | |
| } | |
| } | |
| // Aqui temos um Widget que lê o valor do contador via context e o altera pro tamanho do texto | |
| class TextFieldClass extends StatelessWidget { | |
| @override | |
| Widget build(context) { | |
| print('build TextField - Stateless'); | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| Text('Count ${Counter.of(context).value}'), | |
| TextField( | |
| onChanged: (text) => | |
| Counter.of(context, listen: false).value = text.length, | |
| ), | |
| ], | |
| ); | |
| } | |
| } | |
| enum TipoDoExemplo { textFieldClass, textFieldFunc } | |
| /// Ao trocar o tipo do exemplo utilizando a variável abaixo você verá os resultados no terminal | |
| /// do canto inferior direito. | |
| var _tipoDoExemplo = TipoDoExemplo.textFieldFunc; | |
| /// 1. Ao utilizar uma função que retorna um widget que altera E consome o estado, todo | |
| /// input vai renderizar a Home novamente | |
| /// 2. Ao utilizar uma classe, apenas o widget que consome o Counter vai ser renderizado e a Home | |
| /// segue intacta | |
| class Home extends StatelessWidget { | |
| Widget textField(BuildContext context) { | |
| print('build TextField - função fora do build'); | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| Text('Count ${Counter.of(context).value}'), | |
| TextField( | |
| onChanged: (text) => | |
| Counter.of(context, listen: false).value = text.length, | |
| ), | |
| ], | |
| ); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| print('build Home'); | |
| return Scaffold( | |
| body: Center( | |
| child: switch (_tipoDoExemplo) { | |
| TipoDoExemplo.textFieldClass => TextFieldClass(), | |
| TipoDoExemplo.textFieldFunc => textField(context), | |
| }, | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () => Counter.of(context, listen: false).value++, | |
| child: const Icon(Icons.add), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment