Skip to content

Instantly share code, notes, and snippets.

@chooyan-eng
Created February 28, 2023 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chooyan-eng/9ce4c25a95e244f9031913f1ba056f5e to your computer and use it in GitHub Desktop.
Save chooyan-eng/9ce4c25a95e244f9031913f1ba056f5e to your computer and use it in GitHub Desktop.
Demo app for GlobalKey research
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