A Text widget displays text within an application.
Text widgets are created by calling their constructor Text()
, and passing in some arguments. The most basic form is:
Text('Hello, world!')
The information in quotes tells the widget what text to display.
We can also customize the text further.
The Text widget constructor allows additional named parameters, including the following:
style
: Specify the text style, for exampleColors.red
and a size of72
pixelstextAlign
: Specify the text alignment to left, center or right.textDirection
: Specify left to right text direction or the reverse.softWrap
: Specify if the text should wrap to another line if it runs out of room.
To place the Text widget into our app, we will set it to be the body
widget of our Scaffold widget. Here’s what that will look like:
Scaffold(body: Text('Hello!'))
Let’s try it out.
Instructions
Create a Text widget with any text as the body
attribute of our Scaffold.
Press the Run button to run the application. Try changing the text as well!
The text will appear as pretty small, we will fix that in the next exercise.
Hint
Create a body
attribute within Scaffold:
Scaffold(body:
Then create a Text widget:
Text('The text I want to display goes here')
Use that text widget as the body
of the Scaffold.
Solution
The inside of main
should look like:
runApp( const MaterialApp( home: Scaffold( body: Text('Codecademy: Flutter Edition') ) ) );