To bold a string in Dart, particularly when using Flutter, you apply text styling through the Text widget and the TextStyle class. You can set the font weight to bold using the fontWeight property. While Dart itself doesn’t provide a way to bold text (since it’s primarily a backend language), you can achieve this in Flutter by adjusting the text style properties when rendering text in the user interface.
How to make text bold in Flutter
Key takeaways:
The
fontWeightproperty inTextStyleprovides granular control over text weight, with options ranging fromFontWeight.w100(Ultra Light) toFontWeight.w900(Black). The default isFontWeight.w400(Regular).The
RichTextwidget combines withTextSpanto apply bold styling to specific parts of a text string, enabling dynamic and flexible text formatting.Flutter supports custom fonts through the
fontFamilyproperty, offering developers the ability to load and style text with personalized fonts while maintaining compatibility withfontWeight.We can combine different font weights (
w100,w500,w700, etc.) within the same UI to create visually distinct and appealing typography, suited for headers, labels, or content emphasis.
In Flutter, text styling is crucial to creating visually appealing user interfaces. Making text bold is a common requirement when emphasizing certain parts of our app’s content. Fortunately, Flutter provides a straightforward and flexible way to achieve this. This Answer will explore different methods of bold text in Flutter and provide code examples for each approach.
Method 1: Using Flutter font weights
To make text bold in Flutter, we can use the fontWeight property of the TextStyle class. The fontWeight property takes a value from the FontWeight enum, which ranges from 100 to 900. The higher the value, the bolder the font will appear.
Here is a list of the available font weights and their corresponding descriptions:
FontWeight.w100: Ultra LightFontWeight.w200: ThinFontWeight.w300: LightFontWeight.w400: RegularFontWeight.w500: MediumFontWeight.w600: Semi-BoldFontWeight.w700: BoldFontWeight.w800: HeavyFontWeight.w900: Black
Default Flutter text weights
If we omit the fontWeight property, the default value (FontWeight.w400) will be used, and the text will be displayed in the normal weight of the default font as shown below:
How to use Flutter fontWeight
The simplest method to make text bold in Flutter is by using the Text widget. The Text widget allows us to define the text’s style using the style property. To make the text bold, set the fontWeight property of the TextStyle to FontWeight.bold.
Text('Educative Answers',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 18,),),
After running the above code, we will be able to see the following output:
When FontWeight.bold is used, it also bolds the text and uses the default value of FontWeight.w700.
Method 2: Bolding part of a text using RichText with TextSpan
Sometimes, we may want to make only a portion of the text bold while keeping the rest normal. To achieve this, we can use the RichText widget with the TextSpan as shown in the code below:
RichText(text: TextSpan(text: 'Hello, ',style: DefaultTextStyle.of(context).style.copyWith(fontSize: 24),children: <TextSpan>[TextSpan(text: 'Flutter',style: TextStyle(fontWeight: FontWeight.bold),),TextSpan(text: '!',),],),),
After running the above code, we'll be able to see the following output:
Code example
We'll use different font weights in the code below to style text.
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('Bold Text Example'),
),
body: Center(
child:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Educative Answers: 900 Weight',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w900
),
),
Text(
'Educative Answers: 500 Weight',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w500
),
),
Text(
'Educative Answers: 100 Weight',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w100
),
),
],
),
),
),
);
}
}
Explanation
Lines 20–26: We display the first
Textwidget with the text'Educative Answers: 900 Weight'. It has a font size of22and a font-weight ofFontWeight.w900, which makes the text appear bold and heavy.Lines 27–33: We display the second
Textwidget with the text'Educative Answers: 500 Weight'. It also has a font size of22, but this time, the font weight is set to theFontWeight.w500, resulting in a regular weight font.Lines 34–40: We display the third
Textwidget with the text'Educative Answers: 100 Weight'. It shares the same font size of22but uses theFontWeight.w100, representing an ultralight font.
After running the above code, we will be able to see the following output:
Method 3: Using custom fonts and bold styling in Flutter
In addition to using Flutter’s built-in font weights, developers can also utilize custom fonts to achieve unique, bold effects. Custom fonts can be loaded into the app and styled using the same fontWeight property. This allows for more design flexibility and customization, giving apps a distinctive look.
To use a custom font in Flutter:
Download the desired font and add it to the app’s assets folder.
Define the font in the
pubspec.yamlfile.Use the
fontFamilyproperty along withfontWeightin theTextStyleto style the text with the custom font.
Example
Text('Custom Bold Font Example',style: TextStyle(fontFamily: 'CustomFont', // Define custom fontfontWeight: FontWeight.bold,fontSize: 24,),),
This allows for more personalized font experiences in the app and works well with Flutter’s fontWeight system.
Conclusion
Flutter provides a variety of font weights from the FontWeight enum, ranging from ultralight to bold and even heavier weights. Using the fontWeight property of the TextStyle class, we can easily make text bold in Flutter. The Text widget is a simple method to achieve overall bold text, while the RichText widget allows for selective styling of specific text parts. With this knowledge, developers can effectively style text elements in their Flutter applications, ensuring visually appealing and emphasized content for their users.
Further learning
Explore various state management techniques, such as Provider, Riverpod, and Bloc, to efficiently manage and update the app’s state.
Learn how to create smooth, engaging animations and transitions to enhance the user experience in your Flutter apps.
Understand how to connect Flutter apps to backend services like Firebase or REST APIs for real-time data and authentication.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
How do I bold a string in Dart?
How can I make my text messages bold?
How do I bold the text in a string?
Free Resources