Created
October 8, 2025 14:50
-
-
Save ulisseshen/a2a0f93e50189ea7b0a31434901fb234 to your computer and use it in GitHub Desktop.
POC para mostrar o use de MediaQuery.of vs MediaQuery.sizeOf com o use de construtores const e não const.
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(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| home: TestScreen(), | |
| ); | |
| } | |
| } | |
| class TestScreen extends StatefulWidget { | |
| @override | |
| State<TestScreen> createState() => _TestScreenState(); | |
| } | |
| class _TestScreenState extends State<TestScreen> { | |
| double customPadding = 0; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar(title: Text('MediaQuery Rebuild Test')), | |
| body: SingleChildScrollView( | |
| child: Column( | |
| children: [ | |
| SizedBox(height: 20), | |
| Text( | |
| 'Clique no botão e veja o console!', | |
| style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), | |
| ), | |
| SizedBox(height: 10), | |
| ElevatedButton( | |
| onPressed: () { | |
| setState(() { | |
| customPadding += 10; | |
| }); | |
| print('\n📢 Botão clicado! Padding agora: $customPadding\n'); | |
| }, | |
| child: Text('Mudar Padding (muda MediaQuery, mas não o SIZE)'), | |
| ), | |
| SizedBox(height: 20), | |
| // Widget usando MediaQuery.of - vai rebuildar | |
| MediaQuery( | |
| data: MediaQuery.of(context).copyWith( | |
| padding: EdgeInsets.all(customPadding), | |
| ), | |
| child: Column( | |
| children: [ | |
| Text('Widgets COM const (não rebuildam):', | |
| style: TextStyle(fontWeight: FontWeight.bold)), | |
| SizedBox(height: 10), | |
| const WidgetUsandoOf(id: 'CONST #1'), | |
| SizedBox(height: 10), | |
| const WidgetUsandoSizeOf(id: 'CONST #2'), | |
| SizedBox(height: 30), | |
| Divider(thickness: 3), | |
| SizedBox(height: 10), | |
| Text('Widgets SEM const (testam rebuilds):', | |
| style: TextStyle(fontWeight: FontWeight.bold)), | |
| SizedBox(height: 10), | |
| WidgetUsandoOf(id: 'SEM CONST #3'), | |
| SizedBox(height: 10), | |
| WidgetUsandoSizeOf(id: 'SEM CONST #4'), | |
| ], | |
| ), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class WidgetUsandoOf extends StatelessWidget { | |
| final String id; | |
| const WidgetUsandoOf({Key? key, required this.id}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| print('🔴 WidgetUsandoOf [$id] REBUILDOU!'); | |
| final size = MediaQuery.of(context).size; | |
| return Container( | |
| color: Colors.red.withOpacity(0.3), | |
| padding: EdgeInsets.all(16), | |
| child: Column( | |
| children: [ | |
| Text( | |
| 'MediaQuery.of() - $id', | |
| style: TextStyle(fontWeight: FontWeight.bold), | |
| ), | |
| Text('Width: ${size.width.toStringAsFixed(0)}'), | |
| ], | |
| ), | |
| ); | |
| } | |
| } | |
| class WidgetUsandoSizeOf extends StatelessWidget { | |
| final String id; | |
| const WidgetUsandoSizeOf({Key? key, required this.id}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| print('🟢 WidgetUsandoSizeOf [$id] REBUILDOU!'); | |
| final size = MediaQuery.sizeOf(context); | |
| return Container( | |
| color: Colors.green.withOpacity(0.3), | |
| padding: EdgeInsets.all(16), | |
| child: Column( | |
| children: [ | |
| Text( | |
| 'MediaQuery.sizeOf() - $id', | |
| style: TextStyle(fontWeight: FontWeight.bold), | |
| ), | |
| Text('Width: ${size.width.toStringAsFixed(0)}'), | |
| ], | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment