Hey guys, welcome to Profacoder's Point
Okay, so I got a comment from one of my subscribers saying
he wants to make a switch that keeps active or off or on depending on the user action
Such as if a user turned on the switch and he exits the app
and his future is to open the app again so the state of the switch should be same
As you can see, the switch is still on and now if I off it and exit it
and again open it so it should be off means if a user turned it on, it should be on in future
if he opens the app and if he turns it off, then it should be off
So, in this video tutorial, we are going to check out how to do it with two methods
So, I have just created two switches over here. One is by making use of setState
and one is by making use of getStateManagement. So, we will just check out how to do it in two different ways
So, let's start with the tutorial. So, let's move to our Android Studio to build it
So, as you can see, I have already created a new Quarter project in my Android Studio IDE
and I have removed all the default code given by Quarter team
and I have just created my own scaffold widget by name MyHomePage
So, this HomePage just returns a scaffold widget with a list of widgets
So, here we are going to pass our switches. So, before doing that, first of all, we need to add two dependencies
One is get, also called as getText. The version is 4.1.4 and the second dependency, getTextStorage
So, this is an alternative of StatePreference. So, just add it
So, this is used as an alternative of StatePreference and this library is used for performing state management
So, after adding that, just hit this pop-down button. So, now go to main.darksource
Over here, you need to import getTextStorage. So, after doing that, here in main method, before loading our actual app
you need to initialize this getTextStorage. So, for that, we will just make use of our web. getStorage.init
So, just do it. So, now we will just create our switches
So, for that, first of all, here create a variable, e6
and we will set the initial value as false. So, current, first the button will be off whenever the user opens the app for the first time
and init state. So, we need this. So, we will create a switch over here
So, this accepts two parameters that we need to pass. One is the value we need to pass, the current state of our app, which is off or on
So, we will just make use of this. The current initial state is false, it will be off
And what will happen when the user on it. So, here, value
So, here, we will set the switch back to the value
Means, if the switch is on, then when the user turns on the switch, the value will be false
So, this is true, means this is false. If this is false, then this is true
So, we are just changing this. But this will not work because we will make use of set state
So, we will just try what will happen. Let's see. So, our app got restarted
So, now if I press on this, so you can see it is not getting turned on
But it is working, but it is not showing to the user. That's because we are not setting the state, current state, we are not updating the state
So, for that, we must make use of set state. So, inside the set state, we need to change this value
So, now if I restart, so you can see, it is getting turned on and off
Okay, so if I turn this on and I will just close the app and restart the app again
Now you can see that the app, I have just turned on, it is off again after restarting our app
So, to remember this state, that user has turned on the switch, we will make use of getStorage
Okay, so to make use of getStorage, so first of all, you need to initialize it in your main method before calling your actual app
Okay, so just do it. And then you need to create an instance of getStorage
So, for that, we will just create an instance object. You can name it anything
I will just name it as hitData because we are storing all the data that user performed in this getStorage
In this hitData. Okay, and now we can use this instance to store our data that a user performed
So, to store our data, over here, hitData.write. Okay, so here, this accepts two variables
One is the key and one is the value. The key is used to identify the data
So, we will just name it as hitSaved. And the value we are going to store over here is hit
Okay, so we are just storing the value of hit. So, if it is on, then we will just store true
If it is off, then we will store false. Okay, so we will store this data in this getStorage by making use of delete key
Okay, so now, in your init method, over here, you need to make use of if statement
Over here, you just read the key. Okay, so here we have made use of write to write the data
So, now, this time we are making use of read to read the data
So, if it is null, if it is not equal to null, over here, if it is not null, then we will just change our hitData
hitData is hit to false, true. Okay, so here we must make use of the data that is stored in this getStorage
So, just paste it over here. Okay, so here what is happening is, we are checking if the data is null in our getStorage by this key
If this key is null, then this will not work. And if it is not null, there is some value that is stored in this key
That means we can just check it what is the data and we can just assign the data that is either true or false to this switch
So, we are just updating the switch. So, now to update the UI, we must make use of setState
Okay, so now let's check the app. Okay, so app got installed
I will just turn this on and off our app, close our app, and I will just reopen the app again
So, you can see that the switch is still on by making use of this data
Okay, so now I will just off it and close the app, reopen it
So, you can see now that the switch button is off. If I do it again, on it, reopen our app again
Sorry. Sorry. This is the app. Okay, so as you can see, it is turned on
Okay, so in this technique, we have made use of setState method to update our UI, update the changes that we have done
Okay, so I will just create a second part of this video tutorial. In that part, I will just make use of getState management to do the same thing
Okay, so thanks for watching. Please do subscribe to Autocoder's Point. Thank you
Autocoder's Point