Lastly, whenever you use the widget, pass the same key variable to it: return Container(įull Source: import 'package:flutter/material. Then, in your widget, have a constructor that takes in a key and pass it to the parent class: Foo(key) : super(key: key) Use variable as Global variable in Flutter Ask Question Asked 4 years, 1 month ago Modified 2 years, 10 months ago Viewed 28k times 5 I need to use datosusuario variable in another file how can I do this My main.dart is a Login I need use the query result variable to print that result in my homepage Widget Main. So, if you want the same widget, appearing on very different screens (that hopefully are far from each other), you can use a GlobalKey to do that, with basically 3 lines of code.įirst, declare a variable that you can access from both screens: final _key = GlobalKey() So for this demo, I inserted a "blank page" to prevent that when swiping. For example, when swiping to another screen, there is usually a transition animation where both screens are rendered at the same time. If you can guarantee that, the two widgets will never be on the screen at the same time, or more precisely, they will never be simultaneously inserted into the widget tree on the same frame, then you could try to use GlobalKey to have the same widget on different parts of the layout. Basically right now there is no good way to easily implement the demo using GlobalKey. Saed's answer (which I rewarded 50 bounty points) might be how it was done in the video, but it no longer works in recent Flutter versions. Things have changed since that video was released. For more complex data, you should look into Provider, InheritedWidget or alike. For example, if you want a Switch and a Slider like in the demo, you are better off just pass the actual boolean and double behind those two widgets. You should pass the data around, not the widget, not the widget state. I would not recommend using GlobalKey for this task.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |