Flutter Crash Course #14 - Expanded Widget

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

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 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

This video teaches how to use the Expanded widget in Flutter to manage layout and constrain child widgets. The Expanded widget is useful for taking up the remaining space in a column and restricting the size of its child widget.

Key Takeaways
  1. Add an image to the home widget
  2. Use the Expanded widget to wrap the image
  3. Constrain the image size using the Expanded widget
  4. Use the fit property to make the image fit the width
  5. Add alignment to the image to start it at the bottom and center
💡 The Expanded widget behaves like a Flex item in CSS, taking up the remaining space and constraining the size of its child widget.

Related Reads

📰
DeepSeek V4 API Pricing Explained: Pro vs Flash, and How to Call Both With One Key
Learn how to optimize DeepSeek V4 API usage by understanding Pro vs Flash pricing and using a single key for both variants
Dev.to AI
📰
What the VPA Recommender Is Actually Computing (And Why It Disagrees With You)
Understand what the VPA recommender computes and why it may disagree with you, to improve your usage and configuration of VPA
Dev.to · Prasad MK
📰
AI Watermark Remover: How EzMaker AI Turns Messy Images into Usable Assets
Learn how EzMaker AI removes watermarks from images, enabling businesses to reuse and repurpose visual assets efficiently
Dev.to · Lily Chen
📰
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Learn how to choose the right AI API for your project by considering tradeoffs, not just picking the 'best' model
Dev.to · Shaw Sha
Up next
15 NotebookLM Hacks That Change How You Work
SCALER
Watch →