Created
February 3, 2023 11:49
-
-
Save karabanovbs/0139e7427edee34b4c1d523d86ec713c to your computer and use it in GitHub Desktop.
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'; | |
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