Flutter Crash Course #14 - Expanded Widget
Key Takeaways
This video tutorial demonstrates how to use the Expanded widget in Flutter to manage layout and constrain child widgets, using a coffee-themed app as an example. The Expanded widget is used to take up the remaining space in a column and restrict the size of its child widget, in this case an image.
Full Transcript
all right then so we have one more image left to use and we're going to add that to the home widget right here at the bottom of the column so again we can just use the name Constructor image. asset to add the image right here and we need to add in the path to that image which is the assets folder then into the image folder then the file name is coffee bg. jpeg okay so we should see that image then on the home screen at the bottom beneath those other two things but when we preview this we can't actually see that image and instead we see this yellow and black warning box at the bottom and it also says though you probably won't be able to read this on the video that we've overflowed by some amount of pixels so what's happened here is that the image we're using is quite tall right and it's overflowing the available space left to us on the screen now when we're making a layout in flutter if we go outside of the bounds of the screen it doesn't automatically let us scroll down to what's overflowed it's not the same as a web page right that's not the way it works when we're making mobile apps using flutter instead we have to stay within the bounds of the screen and only use up the space within those bounds available to us now you can add scrollable sections within the screen so users can scroll down within those scrollable sections but to fix this problem we're not going to do that instead we're going to use a widget to wrap this image called an exp expanded widget now the expanded widget behaves very much like a flex item in CSS it automatically expands or grows to take up the rest of the available space it has and no more than that it also restricts its child widget which is going to be the image in this case to that maximum size as well for example if we use the expanded widget in this column as the third item inside it then it's going to take up all of the available space after the the first two items right up to the bottom of the screen and then if we add the image as a child to the expanded widget then the image size will be constrained by that expanded widget and only take up the space inside the expanded widget up to the bottom of the screen okay so let's give that a shot now an easy way to wrap a widget with another one is to click on that widget in vs code click on the light bulb and then choosing wrap with Widget now you can wrap with these pre-built widget right here but if your widget isn't there like expanded isn't then you can just choose wrap with Widget and then this right here we can say is expanded now it automatically adds this child field right here and the child is the image itself okay now I'm just going to format this a little differently so it's like this okay now if I save this we should see that the image doesn't take up any more Heights than is available because the expanded widget Su up all the available height no more and then it passed on those constraints those height constraints to its child all right now at the minute we have a strip of white on the left and a strip of white on the right and that is just because of the dimensions of the image so the image is taking up 100% of the height and when that is the case it doesn't take up 100% of the width so we can change the fit of this by adding in a few properties so this image thing right here after the path we can add a comma and then go down to the next line then I'm going to add a fit property and to do this if you hover over this it's going to be a box fit so we can say box fit like so and then Dot and I'm going to say fit width because we want it to fit the width and if I save this now you can see it does take up the full width now but a lot of it is gone or rather is hidden after the bottom of the screen now I want all that to be visible because that is the actual coffee beans and the cup of coffee so after this I'm also going to say the alignment of this image is going to be alignment dot bottom center and basically what that does is start the image at the bottom and the center of the image so we should see that if I save it you can see now bottom center we have it and that looks a lot better all right so that is the expanded widget right there in a nutshell it behaves very much like a flex item in that it takes up the entire space available to it but it also constrains the size of the child to be that as well now there's actually one more example I want to show you using this expanded widget and to do that we're going to go to cofy prep over here and see here where we have this sized box and it takes up a width of 50 pixels they are just after the images right here the Coffee Bean and the sugar so we don't really see it because there's nothing after them but if we add some text here so if I say con text and then inside here we'll just do a plus sign this later is going to be a button that we can press to increase the strength but we'll do a similar one for the sugars down here as well if we oops spell that correctly if we preview this now we can see that between this plus icon and the images we have that sized box right now we can specify the width in here so that could be 100 if we wanted it to be larger or wider and we get more of a space but if you want it to take up the rest of the available space so that the plus icons are way over here well we can use expanded to wrap this now when we do that we no longer need a width because it's taking up all the available space so I can click on sized box wrap with Widget call it expanded and then I'm going to say const right here and because we've used const there we don't need it here then I'm going to copy this and I'm going to get rid of this one and paste the other one in so now we have two expanded sized boxes and now they take up all the available space right here between the image and the plus sign and the plus signs go all the way over to the right and that is where the buttons are going to be later on to increase the strength and also the sugars all right so it is really useful this expanded widget you'll probably find yourself using it quite a lot
Original Description
In this Flutter Crash Course tutorial series, you'll learn how to make Flutter applications from scratch.
🥷🏼🔥 Get the Flutter Masterclass Course:
https://netninja.dev/p/flutter-masterclass
💎 Use promo FLUTTERNINJA50 for 50% off!
🥷🏼🔥 Sign up to Net Ninja Pro:
https://netninja.dev/p/net-ninja-pro
📂🥷🏼 Access the course files on GitHub:
https://github.com/iamshaunjp/flutter-masterclass
🔗🥷🏼 Flutter Getting Started Guides - https://docs.flutter.dev/get-started/install
🔗🥷🏼 Install Node.js - https://nodejs.org/en
🔗🥷🏼 VS Code - https://code.visualstudio.com/
🔗🥷🏼 DartPad (Dart sandbox) - https://dartpad.dev/
🔗🥷🏼 Homebrew (for mac) - https://brew.sh/
🔗🥷🏼 Zapp (Flutter sandbox) https://zapp.run/new
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: UI Design
View skill →Related Reads
📰
📰
📰
📰
DeepSeek V4 API Pricing Explained: Pro vs Flash, and How to Call Both With One Key
Dev.to AI
What the VPA Recommender Is Actually Computing (And Why It Disagrees With You)
Dev.to · Prasad MK
AI Watermark Remover: How EzMaker AI Turns Messy Images into Usable Assets
Dev.to · Lily Chen
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Dev.to · Shaw Sha
🎓
Tutor Explanation
DeepCamp AI