Flutter – Concept of Key in Widgets
In this article, we will study Keys and when and where to use them. So you may ask what are keys? Well, Keys are the ones to preserve state when widgets move around the widget tree. It is used to preserve the user scroll location or keeping state when modifying a collection. Key’s aren’t needed if the entire widget subtree is stateless. Now let us study when to use the keys.
As we know that we use keys to preserve states when widgets move around the widget subtree. So, basically, keys are not needed in a stateless widget but would be needed in a Stateful widget.
To explain the keys we will create a basic application in which on tapping the button the boxes will swap the colors. The values of the colors would be stored using the keys. The following code is of the application created without the use of keys.
Stateless Widget Tree:
Here in this code, we have created the application using the stateless widget. A class PositionedTiles is created here. The comment you are seeing in this code is for the unique color generate to generate the colors for the boxes randomly. On tapping the button the swapTiles function gets activated then the swapping takes place. The onTap button is the smiley face at the bottom.
Here you can see that the colors are changing but when we would change it to the Stateful widget the code will work correctly but nothing will be shown on the application. But as we use the keys here in the Stateful widget the application will work fine. The following code shown below is direct with the keys.
Stateful Widget Tree:
Here in this code, you can see we have used the keys feature here, because of which the application is working fine. The rest of the code is the same, only the Key feature is newly added here.
Now it is clear from the above example where to add them. The answer is simple, if you have to use the Stateful widget then you have to use the Keys else no need to use them.
Now there are many types of Keys in flutter. All of them have been discussed below:
Types of keys in Flutter:
- Value Key: The value key is used when it has to be assigned to something constant and extraordinary. For example, in the To-Do List the text entered is the value.
- Object Key: The Object key is used when any single field such as name or birthday, they maybe the same of more than two people but would be unique for each person or each data combination.
- Unique Key: The unique key is used when there are many widgets with the same incentive then we must use define each widget as a unique widget. We used it in our code also because we didn’t know what random color would be assigned to it until the widgets were assembled.
- Global key: The use of Global key is that it contains the data which can be accessed by other widgets present in the code. In case you don’t want to share it with other widgets then you must use the Global Key<From State> that holds a form of state and won’t allow other widgets to access the data stored in it.
So, in conclusion, we can say that keys are a very good feature in flutter. It only works with the Stateful widget and the purpose to use it to store the value of the current state when modifying the collection.