Widgets 02 | Text

This time I will tell you more about the Text.It fits from beginners to experts.

What is the Text?

This problem is very fooling but needs to know, The text show our string and it also is a widget in the flutter, we can use it as a common widget, so be careful about their parameters.

Facebook Page

Twitter

GitHub

NieBin

NieBin

Before start

In the widgets 01, we create a page to show our container, also we create a page show our Text. The way of creating a page is the same as before, you do not care about this, you just focus on the Text widget now. We will put them all in the children as follows.

class TextPage extends StatefulWidget {
  @override
  _TextState createState() => _TextState();
}

class _TextState extends State<TextPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Text"),
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text("Hello World"),
          ],
        ),
      ),
    );
  }
}

Then, we can put our Text into the children to continue our tutorial.

Constructor

You must make a custom to look at the constructor of the widgets before using it.

The this.data is where we put the String, this is easy to understand, we do not talk about this. the style is most common to use. this.locale control its language,this time I don't plan to talk about it, I will write another article to explain it. So let's talk about the parameters one by one.

style

Let's look at its definition.

It is a TextStyle class, look at its definition,so many important parameters are here,such as the fontSize, color, height.

The function of these parameters is a definition with its name. The color parameter control the color of Text.The fontSize controls its font size. We just show an example for simple param.

We just change the parameters,they will show as follows.

Above code, we can learn how to use color ,fontSize and backgroundt,the background is a Paint that controls the color or other things, I don't want to expand this for more detail.

1. foreground

We can see that the foreground is also as the background above, so just use the Paint,the ..is a special operator in the flutter.

Is same as below.

We just code as follows.

The paint draw the Text with the foreground, be careful that we can't use the color parameter with this, so you can only choose one parameter.

2. fontWeight

You can see the definition, the class is FontWeight, you can use its constant with a fore definition. We will use the FontWeight.w700 , it represents bold text. The other constants you can find in the class FontWeight,remain to you.

3. fontStyle

We know the type is FontStyle, we use its only two constants.As follows, this example will show an italic text.

4. letterSpacing

The parameter will control the distance between the letter inside a word. For example, the word Helloif set this param, the distance between H,e,l,l,o will depend on this param.

5. wordSpacing

The parameter controls the distance between the two words, I will set two different wordSpacing to compare.

6. shadows

When we see the definition of the shadows, we can see this.

The Shadow constructor is below.

The offset is the direction from the origin point of the widget, the blurRadius is the distance of the blurred space.

So let's see an example.

7. decoration

In this part, I will tell three params:decoration, decorationColor, decorationStyle. They draw the same line show in the Text, the decoration controls the location, decorationColor control the color, decorationStyle control the shape. The definition is below.

Let's see an example.

8. fontFamily

This parameter changes the typeface for the font. It is a bit difficult, but be patient.

First, we should download a typeface file on the web and in our project, I put into the assets/font. You should know the folders are created by myself, you can do that too.

Then, config the pubspec.yaml file in our project,as I note with the red rectangle and paste the config as well.

Finally, we can use the font name maniguas we set above.

It is easy for use, if you want more detail about the typeface to use, you can click this link. So far, we finish the using of TextStyle, let's look at the other using.

textAlign

This param controls the Text location in the parent widget, I will show you it in a Container. The TextAlign constants are below, I just use the end to show.

1

2

3

left

center

right

start

justify

end

textDirection

Normally, we draw the Text from left from right, but we can change it with the textDirection parameter.

softWrap

This param will control the whether or not the show all content when there is not enough space for the text to show. If true means that it will show the all content, otherwise not show.

overflow

When there is not enough place to show, how we hand the end part, the overflow is a choice to handle this, above we end just cover the other part, but we can end with three points like follows, you also can use clip,fade.

maxLines

Also, you can use the maxLines to limit the space, in above part we can see the Text show two lines when there is no space in the horizontal direction.

textScaleFactor

When we want to make the Text larger or more little, we can use the fontSize to change its size. But there is another choice to change its scale, the textScaleFactor. It depends on the fontSize, for example, when we set fontSize=10 ,textScaleFactor =2, the real scale of the Text will be equal to 20, means that the real-scale= fontSize*textScaleFactor for the Text.

Conclusion

Today we learn how to use the Text, the most common used param is the style, it mainly controls the Text itself, but some other parameters are controlled with its parent widget. But you needn't know all of them, it's just used with your requirement.

Thanks for your reading!

The end.

Whole code in GitHub,star to support.

Facebook Page

Twitter

QQ Group

GitHub

NieBin

963828159

NieBin

Last updated

Was this helpful?