Created
February 28, 2023 05:57
-
-
Save chooyan-eng/9ce4c25a95e244f9031913f1ba056f5e to your computer and use it in GitHub Desktop.
Demo app for GlobalKey research
This file contains 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 StatefulWidget { | |
const MyApp({super.key}); | |
@override | |
State<MyApp> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
var _colored = false; | |
@override | |
Widget build(BuildContext context) { | |
const counterWidget = _StatefulCounter(); | |
final child = _colored | |
? ColoredBox(color: Colors.blue[50]!, child: counterWidget) | |
: counterWidget; | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: const Text('GlobalKey practice')), | |
body: SizedBox.expand(child: child), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => setState(() => _colored = !_colored), | |
child: const Icon(Icons.change_circle_outlined), | |
), | |
), | |
); | |
} | |
} | |
class _StatefulCounter extends StatefulWidget { | |
const _StatefulCounter(); | |
@override | |
State<_StatefulCounter> createState() => _StatefulCounterState(); | |
} | |
class _StatefulCounterState extends State<_StatefulCounter> { | |
var _count = 0; | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text('$_count', style: const TextStyle(fontSize: 24)), | |
const SizedBox(width: 32), | |
IconButton( | |
onPressed: () => setState(() => _count++), | |
icon: const Icon(Icons.add), | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment