In this Flutter lesson we want to learn How to Create Text Widget in Flutter, now first of all let’s talk about Flutter.
What is Flutter?
Flutter is an open-source UI software development kit (SDK), Flutter is created by Google. It is used to develop applications for mobile, web and desktop from a single codebase. Flutter was first introduced in 2015 and since that time flutter has gained a lot of popularity among developers due to its fast development process, expressive and flexible UI and native performance.
What is Text Widget in Flutter?
In Flutter Text widget is a fundamental component, and it is used for displaying text inside the user interface of an application. It allows you to present single-line or multi-line text with different styles and formatting options. Text widget is part of the Flutter framework and it is essential for building UI elements such as labels, headings, paragraphs, buttons and more.
Now let’s create a simple example of Text Widget in Flutter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Importing the necessary Flutter library import 'package:flutter/material.dart'; // Main function, the entry point of the Flutter application void main() { // Running the Flutter application runApp( // Center widget to horizontally and vertically center its child const Center( // Text widget to display the welcome message child: Text( 'Welcome to Codeloop.org', // Text content textDirection: TextDirection.ltr, // Text direction (left-to-right) ), ), ); } |
Now let’s describe above code
This line imports the necessary Flutter library, material.dart, which contains widgets and utilities for building Material Design-style user interfaces in Flutter.
1 |
import 'package:flutter/material.dart'; |
This defines main() function, which serves as the entry point of the Flutter application. It’s the starting point from which execution begins.
1 |
void main() { |
runApp() is a function provided by Flutter that takes a widget as its argument and renders it as the root of the application. It’s used to initialize the application and start the Flutter framework.
1 |
runApp( |
The Center widget is used to horizontally and vertically center its child inside the available space. It’s a layout widget that positions its child widget at the center of its parent widget.
1 |
const Center( |
Inside the Center widget, we have a Text widget. Text widget displays the text “Welcome to Codeloop.org” on the screen. It also specifies the text direction as left-to-right (TextDirection.ltr).
1 2 3 4 |
child: Text( 'Welcome to Codeloop.org', // Text content textDirection: TextDirection.ltr, // Text direction (left-to-right) ), |
Run your code in emulator and this will be the result
Also you can change the color and font size of Text Widget in Flutter
1 |
style: TextStyle(color: Colors.red, fontSize: 30), |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Importing the necessary Flutter library import 'package:flutter/material.dart'; // Main function, the entry point of the Flutter application void main() { // Running the Flutter application runApp( // Center widget to horizontally and vertically center its child const Center( // Text widget to display the welcome message child: Text( 'Welcome to Codeloop.org', // Text content textDirection: TextDirection.ltr, style: TextStyle(color: Colors.red, fontSize: 30), // Text direction (left-to-right) ), ), ); } |
Run your code in emulator and this will be the result
FAQs:
How to use the Text widget in Flutter?
Using the Text widget in Flutter is straightforward. Simply import the necessary Flutter library (import ‘package:flutter/material.dart’;) and then create a Text widget with the desired text content. You can customize the text style, alignment and other properties as needed. This is a basic example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import 'package:flutter/material.dart'; void main() { runApp( MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Text Widget Example'), ), body: Center( child: Text( 'Welcome to Codeloop.org', style: TextStyle(fontSize: 24.0), ), ), ), ), ); } |
This will be the result in emulator
How do I make text a widget in Flutter?
In Flutter, text is treated as a widget using the Text widget. To make text a widget, simply wrap it inside a Text widget and provide the desired text content. You can then customize the text style, alignment, and other properties using the TextStyle class. This is an example of how to make text a widget:
1 2 3 4 |
Text( 'Hello, Codeloop', style: TextStyle(fontSize: 24.0), ) |
How do you make a text widget selectable in Flutter?
To make a text widget selectable in Flutter, you can wrap it inside a SelectableText widget. SelectableText widget allows users to select and copy text from within the app. This is an example of how to make a text widget selectable:
1 2 3 4 |
SelectableText( 'Selectable Text Example', style: TextStyle(fontSize: 18.0), ) |
How do you make text styles in Flutter?
In Flutter, text styles are defined using TextStyle class. You can create custom text styles by instantiating a TextStyle object and specifying properties such as font size, font weight, color, and more. This is an example of how to make text styles in Flutter:
1 2 3 4 5 6 7 8 9 |
Text( 'Custom Text Style Example', style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.bold, fontStyle: FontStyle.italic, color: Colors.blue, ), ) |
Subscribe and Get Free Video Courses & Articles in your Email