Flutter Text Widget

Row widget, Column widget, Extended widget, Padding Widget and Container widget are using apart from the Text widget for give better understanding about alignment.

Let’s make a basic Text

Let’s make a flutter application and add a scaffold as the home screen as follows.

StatefulWidgets are the widgets where we can change the values on the interface at runtime and StatelessWidgets are the widgets where the values of the interface cannot be changed at the run time. For the example the StatelessWidget is used as we don’t have to change the value on the interface. Scaffold is used as a background for this example. Following is the preview of the above code.

Preview of the above code

Let’s add some basic styling

Align the text

Before align the text you have to make some space because if you align the text and there are no space nothing will happen and to align the text you have to use textAlign attribute in Text widget as follows.

Cannot align as there are no space

Above coding example is not doing anything there is no space to align. Then a Container widget is added wrapping Text widget with some width.

Quick tip: Getting the screen width

Following is the code for the text alignment after making some space using Container widget.

Preview of the above code

Font size using textScaleFactor

There are two way of adjust the font size one is using the textScaleFactor in Text widget or using TextStyle class which will be discussed in latter part. textScaleFactor attribute takes a double value which is consider as a scaling factor and scale the text as follows.

Preview of the above code

Style using TextStyle class

Textstyle class has to be used to add some styling to the fonts and following is the code to add styles to the Text widget.

Adding text colours

Basic colouring can be done using style attribute and there are some advanced colouring mechanisms which will be discussed in another blog post. The Colors class can be used for add colours and there are some more methods.

Add colours using Colors class
Preview of the above code

Adding font weights

Font weights can also be added using the Textstyle class as follows.

Following example will show the preview of adding different font weights to different texts.

Preview of different font weights

Changing Font size

Another method to change the font size is adding the fontSize attribute which takes the double value as the input. You can use both textScaleFactor and fontSize to change the font size of a text and when you use the both the fontSize will be multiplied by the textScaleFactor and set the font size as the outputted answer

Preview of the above code

Adding space between letters and words

TextStyle class facilitate both letter spacing and word spacing using two attributes namely letterSpacing and wordSpacing. Both attributes take double value as the input.

Preview of the above code

Adding custom fonts

Before using the custom fonts you have to add them into pubspec.yaml file. I have used the Cavet font in google fonts to this tutorial and add a subfolder inside the project folder and add font files files as follows.

Then you have to update the pubspec.yaml file as follows to add the fonts to the project with the weights indicated in the google font site.

The the following is the sample code to how to add the custom fonts to your project and at there you have to indicate the fontWeights along with the fontFamily or else it will pick up the first font file under the font family name in the pubspec.yaml file.

Let’s do some alignment

Then the problem arise when you need to add more Text widgets on the screen and for that you have to use Row widget and Column widget and there are many more.

Horizontally add Text widgets

When it comes to add horizontally add more text widget you have to add your Text widgets in an array and wrap them inside a Row widget as follows.

As an example lets take following code.

Preview of the above code

But as you see the alignment is not happened as there is no space defined and the following problem will occur when the text exceeds the screen size as follows.

When text exceeds the screen size

When it come to above problem or if you need to divide the screen in to some ratio you have to use Expanded widget.

Avoids overflow using Expanded widget

When you add expanded widgets into the row the row equally divided and it works as a container and if you need to change the ratio you have to change the flex attribute inside the Expanded widget. All the basic alignments are working here.

Equally divided the screen.

Vertically add Text Widgets

Text widgets can be added vertically using the Column widget, ListView widget and many more and for basics the Column widget will be used.

Preview of the above code

Let’s change text using a button click

For the final section It is good to know how to change a text according to the button click and we need to change a widget at the runtime then let’s make a statefulWidget for that and added a private variable and add it to the text field.

Assign variable to the Text

Quick tip : Add a variable to text field

In dart language private variables are begin with a _ mark.

In above code the _value private variable is declared to store the integer value and it is added on to the Text widget and button have added to the screen as follows.

Preview of the above code

Adding the logic

The logic should be added to the onPressed attribute in the RaisedButton widget which will trigger when button is pressed. Important factor is if you need to change a variable at runtime you have to change it inside the set state function as follows.

The _value is increase by one when the button is pressed and the final code is as follows.

Preview of the above code

Hopefully this is helpful.

If you have found this helpful please hit that 👏 and share it on social media :).

You can find the source code of this application on the following link https://github.com/yashodgayashan/flutter-tutorial/tree/text