today we are going to learn how to set up constant silver functions and some other utilities inside our photo eCommerce application if you are already following the series we are creating flutter Ecommerce application and you can see we are inside the section one which is the configuration of flutter applications this is not directly related to flutter e-commerce application but this configuration or these type of configurations can be applied for any professional large scalable applications okay as you can see in previous tutorials we learn how to create a flutter project and how to set up professional folder structure expandable folder structure then in the previous role we talk about themes light and dark modes create custom themes and in today's tutorial we will be looking at how to create constants how to manage helper functions which we can easily use throughout our multiple different applications and definitely they should save a lot and lot of time and help us designing a professional application so without wasting time let's head to Android Studio okay we are inside our photo project and you can see on the left side in the previous first tutorial of section one we learn how to set up these folder structures and in today's video we will talk about what is this util and how to set up the utilities of letter e-commerce application first of all let's talk about this menu dot because I want to convert this menu dot into two parts the reason is at the top you can see basically the main dot dot or the main function is the entry point of float replication and inside currently we cannot see anything and we are following currently you are in this trade so there is no reason to divide this code into two classes the app in another class and the main function only in this class but the thing is as you can see I added the comments over here these are some basic things that we have to or we need to add inside the main function when we start coding backend so we have to initialize the widgets create local storage and Native Splash initialize Firebase initialize authentication and definitely notifications and bunch of other things that we need to initialize inside this main dot dot main method so that's why inside the slip folder create a new class name it app. import material library and we will paste our app inside this and we have to import both light and dark themes so we have to import this custom theme class we learned in the previous audio if you're new you can get the link from the description below or you can get the playlist link from the right top corner so let's close this appro dot as well and import this app over here now we are good with the design okay let's talk about the constants inside the constants we can add anything which is duplicating itself in the code let's say we have some line some color or some text which is being appeared two to three times inside our application and we have to repeat that code again and again so instead of repeating the best practice is to keep the code clean and for that we have to create a single class or a single function or a single variable which can easily be used throughout in the application wherever it's needed so constraints are the variables or the functions or the features which are constant through your application and are not going to be changed okay let's start with the constants first of all let's create a new file I'm going to name it colors. create again we have to import the material to dot at the top and then we have to create a new class I'm going to name it T Colors you can name it anything that's it this is how we create a class to keep this Constructor also private it's all up to you it's not mandatory you just have to write dot underscore and that's it in dot to create Constructors we have to write Dot and the names but the name is private so that's mean that this is a private Constructor because this class Constructor is private so we can easily call static constant color primary color is equal to we have a property called color so using this we can add colors inside our applications arrow is that we have to change and make it a constant to add any color basically we have uh six digits for color codes but to add colors inside the dot we have to write 0 x and double f for the hexa color format and then we can add our color and you can see these six digits is basically an actual color and this is you can see a hexa domination and this is also representing the opacity of this color and over here you can see we have the color already assigned or let me add few more colors okay here you can see you added bunch of colors just to teach you that you can play with anything sometimes when the applications are not that big we are only using these some primary colors and maybe light dark colors some of the neutral Shades and that's it but as we are learning how to create professional applications so in the large applications you can easily manage or handle colors like this so I have basically is primaries can be in action then we have text colors you can add more remove this background colors background container colors button colors border colors error and validation colors and definitely some neutral colors and on the left side you can see we have the colors being displayed okay one more thing that how we can add gradient colors Now to create a gradient color we again have to go with the static constant as it's a gradient so we have to pass the gradient over here instead of color name it linear gradient because the current example is about to create a linear gradient okay linear gradient requires colors which is a list of colors you can see a list of colors over here we have to create list like this now inside we can pass multiple colors so I pass three colors just for an example and press Ctrl alt L to align and on the top we can also Define begin alignment will be 0.
0 to 0. 0 begin from the start both X action and y axis and then end it with some alignment like this so you can play with these values it's not a fixed some rigid thing so you can create gradients in this manner so now as you can see we have default colors we create a colors class you have some default colors we have some default gradients okay how we can use this let's go to main Dot and inside I'm just going to demonstrate you that let's say we have some field which requires a primary color so we have to call T Colors at the top you can see we have to import this colors dot whatever color we want to use we want to use let's say linear gradient it will go like this we need a primary color we will use it like this and when you press the dot you can see we have all the properties we created right over here so you can use any property you like you know once we have colors let me just create few more classes with the same pattern okay anywhere inside the constants you can see I've added multiple classes AP constraints colors enums image strings sizes and text strings let's talk about each of them first of all let's talk about the sizes it's a same class as we created a colors class same colors class we don't have any private Constructor over here but you can create it it's all up to you inside again we have to create sizes so most of the sizes are in the double so that's why we have to again use static constraint double as a variable type and variable name is access this is a small size then extra small small medium large and XL so these are some values you can play with these values as per the need of your own application then we have icon sizes again icon small medium large and extra small font sizes button sizes AB bar height image size default spacing between sections so we have a default space you can say this is a space of a page then we have space between items and space between sections after that we have border radius small medium and large divider height product item Dimensions definitely you can play with all these values it's all up to you you can use few of them you want to create your own so this is the way we can Define input field radius and size card size is image Carousel loading indicator size grid view spacing definitely we will talk each of them as we go along okay now let's talk about next the image strings now inside the machines you can see it's a simple class again don't have a private Constructor but it's all up to you you can create one to create image strings how we can create it first of all to create its return type string so we have a simple variable which is static constant and a string type variable and then we have to pass string the reason to create a separate file for image strings means that we have different type of images defined inside the assets right over here we will Define each and every image inside this images logos and icons to use images inside our application we have to copy and paste every time this string inside our design so this is definitely a not good practice let's say our image is going to be changed we want to have a new logo and our logo is being used 10 times inside our replication so when we are going to delete that logo or we are going to add a new logo then we have to find each and every time the same thing we have added and then we have to replace it manually which is not a good practice that's why I always use a custom class inside I will Define my Logos each and every image we are going to use locally inside of our application I will Define it over here to get this path you just have to click on that image let's say this is an image right click click on copy path reference and inside you can get this path path from content root which is assets icons Brands and then the name of your image so once you have pasted it over here you just have to call your image like this the way we have called our colors previously just have to call T images dot dark app logo so that's it okay let's let's talk about text strings text strings are again some text inside our application and it's very useful to keep your text inside separate text strings over here you can see I have created few strings onboarding title one two three and some others so without going to design without finding anything if you want to change onboarding title 1 you just have to come over here and replace this text right subtitle title any other thing we have created inside the strings you just have to change it over here and everything will be replaced on your application okay now let's talk about the enums I have already written over here that enums cannot be created inside a class so we have to create enums like this there should be no class and nothing else you just have to write enumn name then name of the enum and in the curly brackets you have to Define all the items so what are the enums why we are going to define the enums stay tuned we will talk the enums about in our later videos and next last but not least you can also create the constant strings like this but it's a good practice to keep everything inside a class so I have written the enums like this just for an example but definitely you have to put this same way inside a class so I've changed it into the class mode but the reason just to tell you that without classes you can also Define your constants when it is inside the class you just have to add extra static element because we want to call Apa constraints dot directory T so you get API or some other API related constants so that's how we create constants let's close the constant okay now let's talk about devices formats helpers HTTP local storage and logging but before going to go on these things let's go to pubspect. aml file pin the tab inside the utility packages first of all we have to add some packages HTTP Intel logger and URL make sure these things are up to date okay also one more replication as we're using get a straight management you just have to add get storage for the local storage you can use some other packages as well click on popcat okay now first of all let's talk about the device utility I've created a class device utility over here if you run the flutter Pub get make sure dependencies are added so it's a simple class as you can see the name t device utilities again all the functions that I have created are again going to be started okay the device utility is something related to devices so whenever we're going to need any function which is related to device as you can see the name hide keyboard set the status particular which is a device color is landscape orientation is the devices in the portrait orientation set the device to full screen get the screen height get screen width get pixel ratio status bar height and you can see we have bunch of other options and it's keep on going you can add bunch of other options it's all up to you you can again get this code from the description below at the bottom you can see we have a launch URL function it helps us to launch any URL which we are going to provide let's check in an example let's say I want to run or open my own web URL which is codingwitty.
com from here you can get the code and when the user is going to click on that link this function n will be called we will pass codingwitty. com to this URL it is going to check that if URL can be launched or if it's even a URL if it is a URL then it is going to call this a wait sign which is going to wait until this URL is being launched and it should be an async function then this URL will be launched else it's going to throw some error so this is for the device utility let's now talk about the formatters these formaters are again it's all up to you it's all up to your needs and demands as an example you can see we have a date formatter we have currency formatter we have phone number formator and we have bunch of other things and to use or to format dates we have to use this interlibrary it's very useful so I have function which is a format date I just have to pass date to this function and it is going to check that if date is null it is going to take the current date and it is going to format the date and return the date to us then same way we have a format currency number format dot currency we have to pass the local in which type we want to to convert the currency to what is the symbol and what is the format amount then same way we have phone number if we need to get this type of format you have to use this code if you want to use international former phone number again it's going to be a bit tricky maybe not valid for all the countries but I have tested for few and it worked okay so that's it for the four meters let's talk about the main thing which is the helper functions we're going to use these helper functions a lot inside our applications so keep adding helper functions inside the helper functions because we are going to use a lot and a lot and they can also be used in other projects so like at the top you can see we have a function which is to get some color so we have to pass the value values in the string if the value is in green I am returning a green color again else I don't know why but it's green again written a green then red blue pink gray purple and you can add some other thing then at the bottom I have created a custom snack bar show alert navigate toward the screen in truncade we are not going to use this but just to teach you or give you an example I have added Navigator or push because we are using get X we just have to write get dot to name of the screen and that's it then we have to check is dark mode if the application is in the dark mode definitely we want to do designing this function is going to be very useful and going to be reusable again and again screen size screen higher screen word get formatted date remove duplicators wrap widgets and you can see we have to keep on using again and again some other features then the pricing and calculator this is again a helper class this is going to be used to calculate total price we will pass you can see product price and location it is going to calculate text and shipping for us and return the actual total price including tax and shipping so how it's going to work I have created you can see a function which is get text rate for location and it's a simple 0. 10 percent currently you can change this or you can customize this you can add some other if else to switch your location let's say if the location is in the UK the tax rate is this if the location is America tax rate is this so it's all up to you you can change the tax rate from here so once we have the tax rate we will simply product price multiply our tax rate get the shipping cost same again I have a function get shipping costs shipping charges are fixed over here you can again add if else and some other checks based on distance weight and some other factors so once you can get the shipping cost you can multiply or plus everything and you will get this so this is the reason we are going to use helper functions because they are going to be very useful calculators calculate shipping cost calculator tax and you can see calculate car total and things are given going after that we have HTTP client this is a very useful thing whenever we want to use some APA calls you have to deal with the HTTP calls and for the HTTP you don't have to call HTTP Library again and again inside your own repositories you just have to call T at HTTP helper dot what you want to do you want to get anything you want to post anything put delete or handle response so these things are going to be worked for any type of API you just have to pass the base URL just an example it's your API based URL and based on that base URL you again have to pass endpoint each time and http.
get is going to handle the response which is a generic function at the bottom the response is 200 you can again customize this as per your own need the response is 200 return the Json else throw some error so it's valid for each and everything then same case for the local storage I have created using get X local storage a class which is a Singleton class this is a factory method a simple variable we have to save data read data remove data and clear data so everything will be managed over here you just have to call it same cases for the logging if you are using some logger which I have added on the pubs pick logo dot we have created a simple logger and using the Blogger we are adding debug message info one error and some other type of messages you can add and then the theme is added in the previous tutorials and then after the theme you can see we have validators over here okay inside the validator you can see I have a validate email which is going to validate email for us return messages or default messages validate password validate phone number and you can add some other things as a validation inside your validator you just have to again call T validator dot validate email pass the email to it and it's going to return some response same for the validate password welded phone number so this is how utilities should be defined or created for your photo applications okay as you can see when the utilities are created themes are set up a professional photo structure is already defined so by using these three things our section one is completed and by using this project structure you can create any large application any expandable application which can easily be managed at any level or which you can add anything extra or new feature without hesitating in your code so this is the power of section one I hope you learned something new and if you learn something new please like the video and if you're new to this channel don't forget to subscribe and hit the Bell icon for more upcoming videos once again you can get the code from codingwitty.