Flutter Animation Tutorial #5 - Dart Mixins (Primer)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay then gang so every animation that we've used so far is an implicit animation meaning that flutter does a lot of the heavy lifting for us when it's animating stuff but if we want more finer control and more flexibility over how an animation works we'll need to create an explicit animation and use an animation controller now before we do that i just wanted to cover a quick side dart topic mixins because we'll be using a mixin to help us create an animation controller in the next lesson and i always think it's a good idea to understand the constructs that we use in our code even if it's just from a bird's-eye perspective so i'm going to explain this mix-ins using dart pad which is basically just an online dart playground and i'll leave the link to this down below so what is a mixin exactly well it's a way to add extra functionality or abilities to classes in a way that standard class inheritance doesn't allow us easily and it also allows different classes to share different functionality without just relying on inheritance which can be useful so then to demo this idea of mix-ins i've already gone ahead and created this class called user and this just represents a single user and some kind of website and each user would have access to a post comment method and that would probably post some kind of comments on an article but for the sake of this tutorial we're just printing something to the console now we create an instance of this up here and access that method so that when we run it we see posted comment over here in the console cool now what if we had a second type of user which was a moderator and they should also be able to post comments but as well as that they should also be able to delete comments well what we could do is create that moderator class and inherit from the user class and to do that in dart we use the extends keyword so i'm going to create this class called moderator and it's going to extend the user class and inside all i need to do is declare the new method that this has the moderator and that is to delete comments so i can say void and then delete comments and inside we'll just say print and then comment deleted pretty self-explanatory right so now this moderator is extending this class right here so it automatically gets access to this method we don't need to redefine it because it inherits it but we also have an extra method on moderators called delete comment so i could come to the top over here and i'm going to create now a moderator instead and i could access both post comment which is right here from this user and also the other method if i create another one down here moderator and then access the other method delete comment and that is only available to moderators so if i run this now we should see posted comments and also comment deleted so that works okay so what if we had a third type of user now which was a publisher and again they should have a post comment method but they shouldn't have this one instead they should have an extra method which is to publish an article they can't delete comments so let's create this i'm going to say class and then publisher and this is also going to extend the user class because remember we want to inherit this method not this so we don't extend moderator we extend user so inside now we can declare a new method called publish article and all this does is publish an article but for now we'll just say print and then article published all right so again pretty self-explanatory so now if i comment out both of these and this time create a new instance of publisher and we can access now the publish article method and also the post comment method which we inherit from user but we don't get access to the other method delete comment because we're not inheriting from this right so i could access both of those let me say post comments like so and then underneath that i'm going to say publisher and then we want to publish article so let me run this and see if this works and it does all right so now let's throw a spanner into the work what if we want a fourth type of user and this one is called admin now this admin user should be able to both post a comment and delete a comment but it should also be able to publish articles as well they should be able to do everything well how would i implement this functionality because i can only extend one class so i could for example extend the user but then i only have this and i would have to re-declare these two right here inside the admin class and i don't want to duplicate code i could extend the moderator and i'd get this method and this method via inheritance but not this one and again i'd have to re-declare that and that's code duplication and likewise if i inherit from this one if i extend the publish class i'm going to have to recreate this one in the admin as well so whichever way we look at this i'm going to have some kind of code duplication and this is where mixins come into play so instead what i could do is define these different methods right here or at least one of them as a mixing and then we can mix in that method into a class even though we're extending from a different one so let me write this out then i'm going to explain it so first of all i'm going to comment out this stuff right here because we're going to add this inside a mixing instead so let me come to the bottom and i'm going to use the keyword mixing not with a capital and i'm going to call this can publish article okay and then open up curly braces and all i need to do in here is define some kind of method that i want this mixing to have so i'm going to grab this stuff copy it and paste it right here and i'm going to uncomment it as well and now we have this function inside this mixing so now what i could do is delete this from over here and i'm going to say publisher still extends user so i'm still getting access to this but now i also want to use this mixing as well inside this class and to use a mixing we just say at the end with and then whatever mixins we want to use so can publish article for example and now if we try to use the publisher then we can access this method on it because we're using this mixing and so therefore this class gets the extra ability to publish articles all right so it doesn't matter what we're inheriting from we can just add in this functionality by using a mixing and that makes it a bit more flexible so the same is true for admin so i could now extend the moderator over here so i could say admin extends moderator which means i automatically get access to this and this but i can now say with can publish article and that also means that i get access to this inside the admin class as well and i don't need to worry about duplicating any code so up here let me comment these things out and let me try with the admin so i'll say admin and then i want to use some methods so dot and we can see already we get delete comment publish article and post comments so i can use all of these things let me just use them all to make sure it works so publish article and then admin dot and the last one is delete comment all right so let me see if this works by running over here and yep all of this works so hopefully now you understand what mixins are and how they can help us especially as our class hierarchy and structure gets much bigger as it does typically in floater applications so we'll be using a mixing in the next video when we create an animation controller
Info
Channel: Net Ninja
Views: 24,017
Rating: undefined out of 5
Keywords: flutter, flutter tutorial, flutter animations, flutter animation, flutter animations tutorial, tutorial, animation, app animation, flutter animation tutorial, animation tutorial, flutter custom animations, dart, dart mixins, dart tutorial
Id: ENJaLMUxdmg
Channel Id: undefined
Length: 8min 45sec (525 seconds)
Published: Thu Aug 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.