Flutter Tutorial for Beginners #20 - Extracting Widgets

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

Extracts widget trees into reusable class widgets using Flutter

Full Transcript

okay then going so far we've seen how to map through our list up here and then for each quote what we're doing is calling the quote template function over here and we're returning a card widget so that for each quote up here we're out putting a card widget to the screen and that's fine but there is a better way to do this which doesn't include creating this function over here and it also helps to modular eyes our code too so we can do this by creating our own custom stateless widget which represents this card template right here now a quick way of doing this of creating this stateless widget for this card template is to go into the flutter outline up here and then select the card that we want which is this thing this is the thing we want to put in its own stateless widget we can right-click this and we can go to extract widget right here so press that and then we need to give this widget a name now I'm going to call it quote card which makes sense right because it's a template for a quote card then click on refactor I'm going to close the flutter outline and notice what happens first of all it's removed all of the return statement here where we returned the card and instead it says return new quote card now down here we actually see that it's put quote card into its own class which extends stateless widget so it's created a new widget called quote card and then down here we have a build method which returns the same card template so basically it's taken that template and it's put it in its own widget so if ever we wanted to use this template in the future we could just use an instance of this quote card class now we also see this constructor right here with the Souper we don't need all of this stuff for now and that's kind of beyond the scope of this tutorial so I'm going to delete this but we do have a problem inside this thing over here we're trying to output the quilt text and also the quotes author now inside this stateless widget we don't actually know what the quote is when we up here create a new instance of the quote card then we're not passing any date into that we're taking a quote into this function quote template but we're not passing any data into the quote card no kind of parameters so what I'm going to do is actually pass that through into the quote card class and then down here we can accept that into a constructor so first of all I'm going to create a variable called quote which is going to be equal to object-- so I'm gonna say quote and then quotes so now we're going to have this local quote variable inside this stateless widget and I want to accept that in when we instantiate a class now we're passing it in here but we're not passing it in as a named parameter so I'll do that now I'll say the quotes property is going to be the quote right here and then down here we'll do our constructor so quote and then we're using named parameters so I can just use curly braces and then say this dot quote so whatever we pass in is going to be assigned now to this value now at the minute we're getting some squiggly lines over here and that's because we're using a stateless widget at the minute but we're trying to use data inside that stateless widget now we can use data inside a stateless widget but it's not allowed to change over time so what we have to do is put a final keyword in front of this right here and then this is saying look this is going to be the final value of this variable so it's not going to change now one more thing we need to do is actually change this to quote card because that's what the class is called and the constructor should be the same as the class so we're receiving this in now and we're storing it inside this local quote variable so now we can use that down here and now we no longer get the squiggly lines where we try to output the author and the text so if we were to save this now it should all work because from the top we're mapping through the data we're firing this function taking the quote we're calling quote template and passing in that variable the quotes an inside quote template we receive that and we're returning a new quote card instance by the we don't need this new keyword anymore the new version of dart doesn't require that so we can take that off and we're calling this to return a new quote card instance and we're passing that in as a name parameter so down here we're receiving that name parameter and assigning it to this value right here this variable so now we have access to that quote inside this stateless widget remember we have to put final in front of that to say this is not going to change over time it allows us to use the data this way now we have that quote we can inside return this card template and in there we can output the information from that quote so essentially we're returning this card for every quote inside the list so this should all work now I'm going to save this and see if this works and we see no change over here and that's a good sign because it shouldn't change over here you know we're not doing anything new over here we're just rewriting our code to be better more efficient and more reusable so now we've done this there's one thing I'd like to do and that is to actually delete this function because it's become a bit redundant why do we need to call this function down here just to return a new instance of the quote card can't we do this directly down there inside the map function so let's instead get rid of this dude and delete this and what I'm going to do is paste this down here so now what we're doing is we're saying okay well still map through the quotes and fire a function for each one then for each quotes I want you to get a new instance of the quote card which is returning this widget tree to us and outputting the quote data so that's going to do exactly the same thing and if I save we can see nothing changes so that's a good sign again this all still works now one more thing that promise because we've externalized all of this template into its old quote card class we've made this a bit more modular and reusable right so we might want to use this in a different file later on now if that's the case what we should do is maybe put this inside its own file so then we can just import this into what the file needs it in the future so let's do that let me now just highlight all of this class and cut it and we'll go to our project files over here and what I'm going to do is right click and go to new and then we'll choose a new doubt file down here and I'm going to call this quote underscore card art so now we have this file I'm going to paste this in and I'm just going to save it now notice we get a lot of these different squiggly lines at the minute and that's because we've not even imported the quotes into this file yet and we've not imported the material library so let's just do that at the top we can just copy this from main darts so let's copy those things and import them here we need to import this because we're using the flutter material package and also we need to import this because we're using the quotes object right here so now we have those imported we don't see any of the errors so let's save this now and the next thing we need to do is import this into our other file so let me go to main darts and now let me import quote underscore card dart like so and now this should work now we've imported it we should be able to use this quote card class so if I save it again again nothing changes over here which is always a good sign I'm even going to do a hot restart just to see if it didn't catch any changes when I saved but still it still works over here so everything still works but now we've made our code much more modular we've created an external template in its own widget and we can use that custom widget whenever we want now in the template

Original Description

Hey gang, in this Flutter tutorial I'll show you how you can extract widget trees into their own re-usable class widget. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: Course files - https://github.com/iamshaunjp/flutter-beginners-tutorial Android Studio - https://developer.android.com/studio Git - https://git-scm.com/downloads Flutter Installation - https://flutter.dev/docs/get-started/install 🐱‍💻 🐱‍💻 Other Related Courses: + Modern JavaScript Tutorial - https://www.udemy.com/modern-javascript-from-novice-to-ninja/?couponCode=NINJAYT
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →