 Hey everybody, it's Brian. Welcome to the 31st Flutter tutorial. We are going to be looking at the switch Which is very similar to the checkbox Or the radio button even in the fact that it has an on or off It's graphically different because it switches back and forth think of it like a light switch It's just an easier way of letting the user know what's going on here So typically you would use this for like a setting of some kind, you know, like do you want to? Give feedback yes or no turn it on or off kind of thing So we're just going to dive right in here and we're going to say import package import material Make our main function here And then let's extend the stateful widget and then we're going to Extend the state of that state for widget And you notice they're kind of deviated a little bit I see underscore state instead of my app state just so it's a little easier to understand what's going on in the hood I had some questions about the difference between the two and remember in Dart anything It starts with an underscore is private. So that's not accessible outside here. So we're gonna say Run app gonna make a new material app. We're gonna set the home to new my app and here we're going to just simply Override this and we're going to say I screwed that one all up. It's been a while since I've done a tutorial I took a little break over the holidays. So now I'm gonna fat finger just everything There we go And then here we're gonna say Override. All right, so pretty simple design pattern we got going on here We have our main function which creates a new material app through the run app. That's what's gonna actually run We have our stateful widget Which is going to hold a state the state of that is going to be called by create state and here's the actual state Now in the state We're going to call the build function, which remember whenever we build Quoty fingers We're gonna actually render something on the screen. So we're gonna return new scaffold and This is where the real meat of the program comes into play. I'm gonna say app bar Yeah fat-fingered that we're just gonna say switch demo and in the body We're gonna say new container and we're gonna have some padding in here And then we're gonna have an array of widgets inside of a column here And in here, we're just going to make a new Switch And you see how it has it value in an on change. So automatically it wants to know. Hey, what's going on here? So with that you should understand that we got to actually set some things here So let's say bool Value false. That's our initial value. And then we're gonna say void Then we're gonna set the state here now in set state. We're gonna actually set the value So I'm gonna say value equals the value Pretty simple So we get our initial value here and then the on changed. We're going to say maybe if I could actually type All right, so then we're gonna say Onchanged value. So then we're gonna just simply set the value here. So if you've kind of skipped the previous 30 tutorials basically what's going on here is we have a Private variable called value that we're holding in the state When the slider, I'm sorry when the switch is actually changed It's calling this on change using the value of the current value of the control whether it's on or off Shoving that into this function here as you can see we're calling it there and we're setting the state That's very important to call set state because if you don't do that. Well, the state is not saved and then when the screen's refreshed Nothing happens or I should say if you don't call set state the screen never refreshes That's actually more accurate here and the initial value or the value that's bound to it is this underscore value So we have this private variable here Let's go ahead and fire this up run this on our emulator here had a lot of positive feedback about the flutter tutorials I'm actually pretty happy to see that. I was kind of wondering when I first started these how How many people would actually even be interested in it and turns out quite a bit of you guys actually are I was really shocked For such a new technology, especially in alpha. I mean, it's not even in beta yet. It's still an alpha There is just you mungus amount of feedback on this Alright, so here's our little switch You can see we turn it on turn it off turn on turn it off And it follows the design pattern very similar to what we've seen previously with checkboxes and things of that nature So with that we're also going to check out the You guess that the switch list title Has the same signature here. So we're just gonna copy and paste this and we're gonna break this down a little bit more Screwed that all up. So embarrassed All right, there we go. So with this we're gonna actually add in a few little things here We're going to say a title Now someone did ask With titles you're saying new text. Why wouldn't you just put in quotes because it's actually expecting a widget So for the title you can put anything you want you can put another switch and put checkbox and put the image You can put custom control anything. I'm just putting a text in there because it's very easy to do that All right, so we have our title Let's set the active color Equal we're gonna say colors red and for secondary We're going to say just a home icon here You notice how often left it actually previews it a little bit rerun this bad boy and now we have our switch and Because they have the same value that they're bound to it doesn't matter which one that we click The difference being this you would have to add like extra text or whatever Well, this is all one control so you can even click on a little house here and it turns it on or off So that's it simple easy to use If you found this Tutorial educational or even funny. Let me know Be sure to visit my website boydromes.com click on tutorials for the source code for this and all other tutorials it takes your right out to github and Note that this website and everything that you're watching is 100% funded by your donations That's why I don't have advertisements on the videos So if you are a corporation or if you've just got some extra money, be sure to you know, feel free to donate a lot of that Times that goes towards funding the website running the host and all that stuff So thank you for watching and I'll talk to you guys later