Learn how to style text in Flutter using the TextStyle class, explore font features, and set default text styles for consistent UI design.
In the world of mobile app development, text is a fundamental component of user interfaces. Whether it’s displaying a headline, a button label, or a paragraph of content, how text appears can significantly impact the user experience. In Flutter, the Text
widget is the primary way to display text, and the TextStyle
class provides a powerful way to customize its appearance. This section will guide you through the intricacies of text styling in Flutter, from basic usage to advanced techniques, ensuring your app’s typography is both beautiful and functional.
Text
Widget: A Quick RecapBefore diving into text styling, let’s briefly revisit the Text
widget. The Text
widget is a versatile and straightforward way to display a string of text in your Flutter app. Here’s a basic example:
Text('Hello, Flutter!');
This simple widget displays the text “Hello, Flutter!” using the default text style provided by the app’s theme. However, to make your text stand out and align with your app’s design language, you’ll often need to customize its appearance using the TextStyle
class.
TextStyle
: Customizing Your TextThe TextStyle
class in Flutter allows you to apply a wide range of styles to your text, including font size, weight, color, and more. Here’s how you can use it:
Here’s an example demonstrating these properties:
Text(
'Styled Text',
style: TextStyle(
fontSize: 20,
color: Colors.purple,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
),
);
In this example, the text “Styled Text” is displayed with a font size of 20, a purple color, bold weight, and italic style.
Beyond the basics, TextStyle
offers advanced styling options to fine-tune your text’s appearance:
Here’s how you can use these features:
Text(
'Advanced Styled Text',
style: TextStyle(
fontSize: 18,
letterSpacing: 2.0,
wordSpacing: 5.0,
shadows: [
Shadow(
offset: Offset(2.0, 2.0),
blurRadius: 3.0,
color: Colors.grey,
),
],
),
);
This example demonstrates how to add letter and word spacing, as well as a shadow effect, to your text.
Flutter’s flexibility extends to using custom fonts, including those from Google Fonts. To use Google Fonts in your Flutter app, you’ll need to add the google_fonts
package to your pubspec.yaml
file:
dependencies:
flutter:
sdk: flutter
google_fonts: ^2.1.0
After adding the package, you can use it to apply a custom font to your text:
import 'package:google_fonts/google_fonts.dart';
Text(
'Custom Font Text',
style: GoogleFonts.lato(
textStyle: TextStyle(fontSize: 18),
),
);
This example uses the Lato font from Google Fonts, applying it to the text with a font size of 18.
DefaultTextStyle
In many cases, you’ll want to maintain consistent text styles across your app. Flutter provides the DefaultTextStyle
widget to set a default style for all descendant Text
widgets. This ensures uniformity and reduces repetitive code.
Here’s how you can use DefaultTextStyle
:
DefaultTextStyle(
style: TextStyle(
fontSize: 16,
color: Colors.black87,
),
child: Column(
children: [
Text('First line of text'),
Text('Second line of text'),
],
),
);
In this example, both Text
widgets within the Column
inherit the default style specified in DefaultTextStyle
.
To better understand the impact of different text styles, let’s visualize some examples. Below are images showing how various styles look when applied to text:
Basic TextStyle with fontSize, color, and fontWeight.
Advanced TextStyle with letterSpacing, wordSpacing, and shadows.
DefaultTextStyle
to keep text styles consistent across your app.DefaultTextStyle
to maintain consistency.Mastering text styling in Flutter is crucial for creating visually appealing and user-friendly apps. By understanding and utilizing the TextStyle
class, you can customize text to fit your app’s design language while ensuring readability and accessibility. Remember to experiment with different styles, maintain consistency, and consider accessibility to create a polished and professional app.