Flutter Course: Using the 7.5 Text Widget

Flutter is a UI toolkit developed by Google that helps create mobile, web, and desktop applications quickly and easily. In this tutorial, we will explore one of the most basic and important widgets in Flutter, the Text widget. The Text widget is used to display text in the app’s user interface and provides various styling options and functionalities.

1. Basic Usage of the Text Widget

The Text widget can be used very simply. In its most basic form, it can be used as follows:

Text('Hello, Flutter!')

The above code will display the text “Hello, Flutter!” on the screen. Below is an example of a simple screen using the Text widget:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Widget Example'),
        ),
        body: Center(
          child: Text('Hello, Flutter!'),
        ),
      ),
    );
  }
}

2. Properties of the Text Widget

The Text widget provides various properties to adjust the text’s style and arrangement. The main properties are:

  • style: Used to specify the text style of the Text widget.
  • textAlign: Specifies the text alignment.
  • overflow: Determines how to handle text that is too long and exceeds the area.
  • maxLines: Specifies the maximum number of lines.
  • softWrap: Specifies whether to allow line breaks.

2.1 Specifying Text Style

The most commonly used method to set the style of the Text widget is by using the TextStyle class. The following example shows how to set the text size, color, and thickness:

Text(
  'Hello, Flutter!',
  style: TextStyle(
    fontSize: 24,
    color: Colors.blue,
    fontWeight: FontWeight.bold,
  ),
)

2.2 Text Alignment

Text alignment can be set using the textAlign property. The example below shows how to align the text to the center:

Text(
  'Hello, Flutter!',
  textAlign: TextAlign.center,
)

2.3 Text Overflow

If text exceeds the specified space, the overflow property can be used to specify how to handle it. For example, the following code displays “…” when the text overflows:

Text(
  'Hello, Flutter! This text is too long and will overflow.',
  overflow: TextOverflow.ellipsis,
)

3. Applying Various Text Styles

Flutter allows for the application of various text effects. Here are examples of different text styles:

3.1 Font Family

If you want to use a specific font, you can use the fontFamily property. For example:

Text(
  'Hello, Flutter!',
  style: TextStyle(
    fontFamily: 'Serif',
  ),
)

3.2 Text Shadow

You can add shadows to the text to create a three-dimensional effect. Below is an example of adding a shadow:

Text(
  'Hello, Flutter!',
  style: TextStyle(
    shadows: [
      Shadow(
        color: Colors.black,
        offset: Offset(2.0, 2.0),
        blurRadius: 3.0,
      ),
    ],
  ),
)

4. Using the RichText Widget

The Text widget is useful for displaying single-style text, but if you want to mix multiple styles, you can use the RichText widget. The RichText widget allows you to combine multiple TextSpan to apply various styles:

RichText(
  text: TextSpan(
    text: 'Hello, ',
    style: TextStyle(color: Colors.black, fontSize: 20),
    children: [
      TextSpan(text: 'Flutter!', style: TextStyle(fontWeight: FontWeight.bold)),
    ],
  ),
)

5. Applications of the Text Widget

The Text widget has various applications beyond basic text display. For example, you can create a dynamic UI that takes user input. Below is a simple example that receives user input:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
  String _inputText = '';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Widget Application Example'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(_inputText),
            TextField(
              onChanged: (text) {
                setState(() {
                  _inputText = text;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

6. Supporting Multiple Languages

The Text widget supports multiple languages, making it easy to create multilingual apps. For example, to display text in a multilingual app, it is recommended to use the Intl package. Below is an example of multilingual support:

import 'package:intl/intl.dart';

String getGreeting(String languageCode) {
  switch (languageCode) {
    case 'en':
      return 'Hello, Flutter!';
    case 'ko':
      return 'Hello, Flutter!';
    default:
      return 'Hello, Flutter!';
  }
}

// Usage example
Text(getGreeting('ko')),

7. Using the Text Widget for Optimal Performance

To maintain optimal performance while using the Text widget, various tips and strategies can be applied. For example, to avoid expensive styling work, you can use the const modifier to avoid unnecessary redraws instead of rebuilding the entire Text widget every time:

const Text(
  'Hello, Flutter!',
  style: TextStyle(fontSize: 24),
),

8. Conclusion

The Text widget is one of the fundamental elements in Flutter and is a powerful tool for displaying text in an app. In this tutorial, we have explored the basic usage of the Text widget, various styles, and application cases in detail. Think of various ways to provide a wonderful user experience with Flutter beyond just text display!

In future tutorials, we will delve into more complex topics such as various text styles, animations, accessibility, and user interactions. Continue to explore the world of Flutter with us!

Thank you!