Skip to content

Instantly share code, notes, and snippets.

@karabanovbs
Created February 3, 2023 11:49
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 karabanovbs/0139e7427edee34b4c1d523d86ec713c to your computer and use it in GitHub Desktop.
Save karabanovbs/0139e7427edee34b4c1d523d86ec713c to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
final GlobalKey<State<YellowBird>> key = GlobalKey<State<YellowBird>>();
class _MyAppState extends State<MyApp> {
bool showFirst = true;
bool showSecond = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
children: [
TextButton(
child: Text('switch in one frame'),
onPressed: () {
setState(() {
showFirst = !showFirst;
showSecond = !showSecond;
});
},
),
TextButton(
child: Text('switch in other frame'),
onPressed: () async {
late VoidCallback next;
setState(() {
if (showFirst) {
showFirst = false;
next = () {
showSecond = true;
};
}
if (showSecond) {
showSecond = false;
next = () {
showFirst = true;
};
}
});
await Future.delayed(Duration(seconds: 1));
setState((){
next();
});
},
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (showFirst)
Container(
color: Colors.red,
child: YellowBird(
key: key,
),
),
if (showSecond)
YellowBird(
key: key,
),
],
),
],
),
),
),
);
}
}
class YellowBird extends StatefulWidget {
const YellowBird({super.key});
@override
State<YellowBird> createState() => _YellowBirdState();
}
class _YellowBirdState extends State<YellowBird> {
int increment = 0;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
setState(() {
increment++;
});
},
child: Text(increment.toString()),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment