Flutter Tutorial for Beginners #5 - Scaffold & AppBar Widgets

Net Ninja
23 Aug 201908:47

Summary

TLDRThis script provides a step-by-step guide on setting up a basic Flutter app using the Material App as the root widget. It introduces the Scaffold widget for implementing a basic layout, including an app bar, body, and floating action button. The tutorial demonstrates how to customize the app bar with a title and center-align text within the body. It also includes adding a floating action button with a text child. The script emphasizes the ease of using built-in widgets to quickly create a functional app layout and encourages further exploration of the Scaffold widget's properties.

Takeaways

  • 📝 The script is a tutorial on setting up a basic Flutter app using the Material App widget as the root widget.
  • 🏠 The 'home' property is crucial as it defines what appears on the home screen when the app is launched.
  • 📝 Initially, the home screen contains a simple Text widget with the message 'Hey Ninjas'.
  • 🛠 The Scaffold widget is introduced as a way to implement a basic layout, including an app bar and floating action buttons.
  • 🔄 The Scaffold widget is a built-in Flutter widget that can be customized with various properties.
  • 📝 The AppBar widget is used to create a title bar at the top of the app, and it can be customized with properties like 'title'.
  • 🎨 The 'centerTitle' property is used to center the app bar title on the screen, which is common for Android devices.
  • 📝 The 'body' property of the Scaffold widget is used to define the main content area of the app, placed below the app bar.
  • 🔄 The Center widget is used to centralize its child widget, which in this case is a Text widget saying 'Hello Ninjas'.
  • 🔄 The 'floatingActionButton' property is used to add a floating action button to the app, which can contain a child widget like Text.
  • 🔗 The script encourages further exploration of the Scaffold widget and its properties through the official Flutter documentation.

Q & A

  • What is the purpose of the Material App widget in Flutter?

    -The Material App widget in Flutter serves as the root widget for an app, providing a basic structure and acting as a wrapper for the rest of the app.

  • What is the role of the 'home' property in the Material App widget?

    -The 'home' property in the Material App widget specifies the initial screen or widget that is displayed when the app is launched.

  • What is the Scaffold widget and how does it help in app development?

    -The Scaffold widget is a layout widget in Flutter that provides a basic layout for an app, including an app bar, a body, and a floating action button, making it easier to set up the structure of an app.

  • How can you add an AppBar to your app using the Scaffold widget?

    -You can add an AppBar to your app by specifying an 'appBar' property within the Scaffold widget, and setting its value to an AppBar widget with the desired properties.

  • What is the significance of the 'title' property in the AppBar widget?

    -The 'title' property in the AppBar widget determines the text that is displayed in the title section of the app bar.

  • Why is a Text widget used inside the 'title' property of the AppBar?

    -A Text widget is used inside the 'title' property of the AppBar to display text, as Flutter requires the use of a widget to output text rather than using a plain string.

  • What does the 'centerTitle' property do in the AppBar widget?

    -The 'centerTitle' property, when set to true, centers the title text within the AppBar on Android devices.

  • How can you add content to the body of the screen in a Flutter app?

    -You can add content to the body of the screen by specifying a 'body' property within the Scaffold widget, and setting its value to the desired widget, such as a Text widget.

  • What is the purpose of the Center widget in Flutter?

    -The Center widget in Flutter is used to centralize the content of its child widget, which is useful for aligning text or other widgets in the center of the screen.

  • How can you add a floating action button to your app using the Scaffold widget?

    -You can add a floating action button by specifying a 'floatingActionButton' property within the Scaffold widget, and setting its value to a FloatingActionButton widget with a 'child' property containing the desired content, such as a Text widget.

  • What is the 'hot restart' feature in Flutter and when is it used?

    -The 'hot restart' feature in Flutter allows developers to see the changes they made in the code without needing to relaunch the app. It is used to quickly refresh the app and display the updated screen after making modifications.

Outlines

00:00

🛠️ Setting Up a Basic Flutter App Layout

This paragraph introduces the process of setting up a basic Flutter application using the Material App widget as the root widget. It discusses the home property, which determines the home screen content, and the initial inclusion of a simple Text widget. The speaker then guides through the enhancement of the app's UI by introducing the Scaffold widget, which facilitates the creation of a basic layout including an app bar, floating action buttons, and more. The Scaffold widget is explained as a built-in Flutter SDK feature that can be customized with various properties. The paragraph concludes with the implementation of an AppBar widget with a title, demonstrating how to create a basic yet functional title bar for the app.

05:02

📝 Adding Content and Interactivity with Scaffold

The second paragraph delves into adding content to the app's body using the Scaffold widget. It starts by adding a Text widget within the body property and then discusses the use of a Center widget to center the text on the screen. The paragraph also covers the addition of a floating action button, which is a common UI element in mobile apps, and demonstrates how to nest a Text widget inside it to display text. The speaker emphasizes the ease of using the Scaffold widget to quickly implement a basic layout with interactive elements. The paragraph concludes with a reference to the Flutter documentation for further exploration of the Scaffold widget's capabilities and properties.

Mindmap

Keywords

💡Material App

Material App is a Flutter widget that serves as the root widget for an app, providing a consistent look and feel across different platforms by adhering to Material Design guidelines. In the video, it is used as the starting point for setting up the app's basic structure, acting as a wrapper for the rest of the app's widgets.

💡Home Property

The home property in Flutter is a widget that defines what is displayed on the initial screen of an app. In the script, it is mentioned as a part of the Material App setup, where it initially contains a single Text widget displaying 'Hey Ninjas', indicating the starting content of the app.

💡Scaffold Widget

The Scaffold widget in Flutter is a layout widget that provides a basic structure for an app, including an app bar, body, and potentially a floating action button. It is introduced in the script as a means to enhance the app's layout by replacing the simple Text widget with a more complex structure that includes an app bar and a body for additional content.

💡App Bar

An App Bar is a widget in Flutter that typically appears at the top of a screen and can contain a title, navigation buttons, and other interactive elements. In the video, the App Bar is configured within the Scaffold widget to display a title and is later centered using the 'centerTitle' property for aesthetic alignment.

💡Text Widget

The Text widget in Flutter is used to display a short piece of text in an app. Throughout the script, it is used to display messages like 'Hey Ninjas' and 'My First App' within the App Bar's title. It is a fundamental widget for displaying textual information in the user interface.

💡Widget Tree

A widget tree in Flutter represents the hierarchy of widgets that make up the user interface of an app. Each widget can be a parent to other widgets, creating a nested structure. The script discusses the creation of a widget tree programmatically, starting with the Scaffold and nesting other widgets like the App Bar and Text widgets within it.

💡Center Widget

The Center widget in Flutter is used to center its child widget both horizontally and vertically within the available space. In the script, it is used to centralize the 'Hello Ninjas' Text widget within the body of the Scaffold, demonstrating how to align content on the screen.

💡Floating Action Button

A Floating Action Button is a circular button that floats above the UI and is typically used for a primary action, such as adding an item or taking a photo. In the video, it is added to the Scaffold as a property and contains a Text widget with the message 'Click', showing how to place interactive elements in the UI.

💡Hot Reload

Hot Reload is a feature in Flutter that allows developers to see the effects of their code changes in real-time without needing to restart the app. The script mentions using hot reload to quickly view the changes made to the app's layout and UI after modifying the widget structure.

💡Flutter SDK

The Flutter SDK is a software development kit that contains the tools and libraries needed to build Flutter apps. The script refers to the built-in widgets that come with the Flutter SDK, such as Scaffold, App Bar, and Text, which are used to create the app's layout and interface without needing to create custom widgets from scratch.

Highlights

Introduction to setting up a basic hub using the Material App as the root widget.

Explanation of the 'home' property and its role in specifying the home screen content.

Utilization of the Scaffold widget to implement a basic layout for the app.

Inclusion of an AppBar property within the Scaffold to set up the top app bar.

Demonstration of how to use the AppBar widget to create a title bar.

Use of the 'title' property within the AppBar to display text.

Nesting of widgets within widgets to programmatically create a widget tree.

Introduction of the 'center' property to align the text centrally on the screen.

Explanation of the 'body' property in the Scaffold for specifying screen content below the app bar.

Inclusion of a Text widget within the body to display content.

Use of the Center widget to centralize the Text widget on the screen.

Introduction of the 'floatingActionButton' property for adding a floating action button.

Explanation of how to nest a Text widget within a FloatingActionButton.

Demonstration of creating a basic layout with the Scaffold widget.

Highlighting the Scaffold widget as a wrapper for different layout widgets like AppBar, body, and FloatingActionButton.

Encouragement to explore the Scaffold class on the Flutter Docs for more properties and examples.

Transcripts

play00:01

okay then gang so we've seen now how to

play00:04

set up a very very basic hub using the

play00:06

material app as our root widget right

play00:09

here and it acts as a wrapper for the

play00:11

rest of our app now inside it we have

play00:13

this home property which remember

play00:15

specifies what is going to be on the

play00:17

home screen when we load this app and

play00:19

inside that at the minute we just

play00:21

declare a single text widget which says

play00:24

hey ninjas and we see over here looking

play00:27

at a rather ugly so this is all a bit

play00:30

boring at the minute and gradually what

play00:33

we want to do is add more content to

play00:35

this screen to make it look a bit better

play00:37

so to do that the first thing we're

play00:39

going to explore is a widget called the

play00:42

scaffold widget now the scaffold widget

play00:44

is going to allow us to implement a

play00:46

basic layout for our app it's going to

play00:49

allow us to set up an app bar at the top

play00:50

some floating action buttons every one

play00:53

and more so what we're going to do is

play00:55

actually delete this text widget for now

play00:58

and we're saying now the home property

play01:00

should be a scaffold widget and by the

play01:04

way all these widgets come built into

play01:06

the flutter SDK out-of-the-box we can

play01:09

build our own widgets later our own

play01:11

custom ones but these ones come out of

play01:13

the box that we can use so anyway this

play01:16

scaffold widget is going to take a

play01:18

multitude of different properties so we

play01:20

can specify different things about the

play01:22

base layout of our app the first thing

play01:25

I'm going to do inside this is an app

play01:28

bar property so up bar like that and

play01:31

this app bar property is going to

play01:33

specify how our app bar is going to look

play01:35

at the top now the value of this

play01:38

property is actually just going to be

play01:39

another inbuilt widget which is called

play01:41

up but like so and notice the convention

play01:45

our widgets are all starting with a

play01:47

capital letter and each new word starts

play01:50

with a capital letter as well that's the

play01:51

convention of these widgets so inside

play01:55

the scaffold we now have the app bar

play01:57

property and the value of that property

play01:59

is an out bar widget now remember I said

play02:01

put a comma after every value of every

play02:04

property so let's put a comment there

play02:06

and also a comma after scaffold as well

play02:09

just in case we had more properties down

play02:11

here later on okay then so inside the

play02:14

app bar we can also

play02:15

specify a couple of properties and this

play02:17

is the general pattern here we have a

play02:20

property with a value which is sometimes

play02:22

a widget and inside that widget we can

play02:24

have more properties and sometimes the

play02:26

values of those properties and widgets

play02:28

and it's had those widgets we can have

play02:30

more properties and so forth because

play02:32

we're nesting widgets within widgets

play02:34

remember that widget tree structure I

play02:36

showed you in one of the first slides

play02:38

that's basically what we're doing here

play02:40

programmatically creating a widget tree

play02:43

and each widget has different properties

play02:45

so the first property of this app bar is

play02:49

going to be the title property and this

play02:51

is going to say what is actually shown

play02:53

what text is shown on the title of the

play02:56

app bar now you might think we can just

play02:58

say something like this hello and pass a

play03:00

string but that's not the way flutter

play03:02

works if we want to output some text we

play03:05

need to use a text widget so we can say

play03:08

text like so and then put the string and

play03:11

what we want to show as the title inside

play03:14

this text widget so I'm just going to

play03:16

say my first app okay so let's try

play03:20

previewing this I'm going to save it now

play03:21

and go to run and I'm gonna go to hot

play03:24

reload right here or rather hot restart

play03:26

this one's hot reload we'll talk more

play03:28

about that later

play03:29

for now just click on hot restart it's a

play03:31

bit like a refresh button for you to see

play03:33

the new screen so let me press that and

play03:36

now we can see this app bar at the top

play03:39

it's all done for us we didn't have to

play03:40

do any kind of styling or anything like

play03:43

that it just comes out of the box we can

play03:45

change the colors and I'm going to show

play03:46

you how to do that later on but now we

play03:48

can see a pretty decent looking title

play03:50

bar or app bar at the top with a title

play03:52

on it now at the minute

play03:54

this is left-aligned and this is how it

play03:58

looks on Android devices most of the

play04:00

time so what I'm gonna do is use a

play04:02

property called sensor title to

play04:05

essentially align this and this we can

play04:08

just set to a boolean which is going to

play04:09

be true so this doesn't have to be a

play04:12

widget this is just an option if you

play04:14

like we're centralizing this so we set

play04:16

it to true so if we save that now and

play04:19

then we go to hot refresh or hot restart

play04:23

then we can see it's now in the sensor

play04:26

cool so that's our app bar

play04:29

done so inside the scaffold now I'm

play04:31

going to add a second property after the

play04:34

app bar

play04:35

so let's now UPS and do that for a start

play04:38

and let's now go on to the next line

play04:41

after the up bar and this time we'll do

play04:43

a body property now this property is

play04:46

going to specify what content is going

play04:48

to go inside the body of the screen so

play04:51

anything under the app bar over here now

play04:53

all I'm gonna do is add in a text widget

play04:56

and I'll say hello ninjas okay so if I

play05:01

now save this and go down to run and

play05:04

press hot restart we should see this

play05:07

over here hello ninjas okay so it's

play05:10

quite small but we'll talk about

play05:11

textiles later on and it's also in the

play05:14

top left now when we viewed that dummy

play05:16

project it had the text in the center

play05:19

and it looked a bit better so now I want

play05:21

to do the same thing and the way we do

play05:23

that is we wrap this widget inside a

play05:26

center widget so I'm going to actually

play05:29

cut this for him

play05:30

instead of directly putting the text

play05:32

inside the body property I'm going to do

play05:34

a center widget like so so a center

play05:38

widget centralizes whatever is nested

play05:40

inside of it so inside the center widget

play05:44

we're gonna have a child property we

play05:46

don't just place in the text like this

play05:48

we always have to have properties inside

play05:51

widgets and when we nest something

play05:53

directly inside another widget it's

play05:55

normally the child property that we use

play05:58

so we're saying okay well the child of

play06:01

this Center widget the thing we're

play06:03

nesting inside it is now going to be

play06:05

this text widget so that means that it's

play06:08

going to centralize this text widget on

play06:11

the page so if I save it and go to hot

play06:15

restart then we should see now that is

play06:18

in the center cool okay so let's do a

play06:21

one more property inside this scaffold

play06:23

widget so after the body we'll come down

play06:26

and we'll do another property called

play06:28

floating action button and this is

play06:34

basically get a place as a little

play06:35

floating action button in the bottom or

play06:37

right corner so the value for this

play06:40

property again is going to be a widget

play06:42

because like I said we have widgets as

play06:44

values to properties a lot of the time

play06:46

so this widget is called the floating

play06:49

action button now just delete that on

play06:52

pressed we're going to cover that later

play06:53

don't worry about that now

play06:54

that's how we react we use a pressing

play06:56

this button but for now I just want to

play06:58

show you how to get one on the screen so

play07:01

comment after that then inside the

play07:03

floating action button again we have a

play07:06

child property because we're going to

play07:08

nest another widget inside this widget

play07:11

the floating action button just gets us

play07:13

the button with nothing inside it but we

play07:15

want to show text inside it so we'll say

play07:18

that the text widget is going to be a

play07:19

child of this and inside the text widget

play07:22

we'll just say click that's what the

play07:24

text is going to say so let me save it

play07:27

and then hop restart come over here and

play07:30

we should see now this floating action

play07:32

button with this click message inside it

play07:34

now nothing happens when we click on

play07:36

this because we deleted that on pressed

play07:38

property but we'll talk about that later

play07:39

on for now I just wanted to show you how

play07:42

to get a pond ring with a basic layout

play07:44

like this and how quick that was using

play07:47

this scaffold widget right here so

play07:50

remember the scaffold widget is

play07:52

basically like a wrapper to a few

play07:55

different layout widgets things like the

play07:57

app bar the body and also a floating

play08:01

action button ok so generally speaking

play08:03

when we're creating apps we're going to

play08:05

be using the scaffold a lot to flesh out

play08:07

the general layout of our apps but if

play08:10

you want to read more about it now then

play08:12

you can go to the scaffold class on the

play08:15

floor to Docs I'll leave this link down

play08:16

below so you can go and check it out and

play08:17

if we scroll down here it gives you some

play08:19

different examples it looks very similar

play08:22

to what we've done over there and if we

play08:24

scroll down even more then we're going

play08:26

to see all of the different properties

play08:27

we can add to the scaffold widget so app

play08:30

bar we've seen that because I've done

play08:31

that background color body we've got

play08:34

that we've also got these others down

play08:36

here there's loads of different

play08:37

properties so you can have a play around

play08:38

with those if you want to but we are

play08:41

going to explore them more as we go

play08:42

forward as well this is just an

play08:44

introduction to it

Rate This

5.0 / 5 (0 votes)

Связанные теги
FlutterScaffoldApp BarWidgetsLayoutCodingTutorialMobile AppUI DesignDevelopment
Вам нужно краткое изложение на английском?