Text themes help you keep your app's text styles consistent and easy to change in one place.
Text themes in Flutter
ThemeData(
textTheme: TextTheme(
headline1: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black),
bodyText1: TextStyle(fontSize: 14, color: Colors.grey[800]),
),
)TextTheme groups many text styles like headline1, bodyText1, caption, etc.
You apply these styles in your widgets using Theme.of(context).textTheme.headline1.
ThemeData(
textTheme: TextTheme(
headline1: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
),
)Text(
'Hello',
style: Theme.of(context).textTheme.bodyText1,
)Theme(
data: Theme.of(context).copyWith(
textTheme: TextTheme(
bodyText1: TextStyle(color: Colors.red),
),
),
child: Builder(
builder: (context) => Text('Red text', style: Theme.of(context).textTheme.bodyText1),
),
)This app shows a big blue heading and normal body text using the text theme styles defined in the app's theme.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( textTheme: TextTheme( headline1: TextStyle(fontSize: 32, fontWeight: FontWeight.bold, color: Colors.blue), bodyText1: TextStyle(fontSize: 16, color: Colors.black87), ), ), home: Scaffold( appBar: AppBar(title: Text('Text Themes Example')), body: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('Big Blue Heading', style: Theme.of(context).textTheme.headline1), SizedBox(height: 10), Text('This is body text styled by the theme.', style: Theme.of(context).textTheme.bodyText1), ], ), ), ), ); } }
You can customize many text styles like headline1, headline2, bodyText1, caption, button, etc.
Use Theme.of(context).textTheme to access the current theme's text styles.
Changing the text theme in ThemeData updates all widgets using those styles automatically.
Text themes keep text styles consistent across your app.
Define text styles inside ThemeData.textTheme.
Use Theme.of(context).textTheme.styleName to apply styles.
Practice
TextTheme in a Flutter app?Solution
Step 1: Understand the role of TextTheme
TextThemedefines a set of text styles used throughout the app to maintain consistency.Step 2: Compare with other options
Options A, C, and D relate to animations, fonts, and input handling, which are not the purpose ofTextTheme.Final Answer:
To keep text styles consistent across the entire app -> Option AQuick Check:
TextTheme = consistent text styles [OK]
- Confusing TextTheme with animation or input handling
- Thinking TextTheme loads fonts
- Assuming TextTheme is for user interaction
Solution
Step 1: Recall the correct syntax to access text styles
UseTheme.of(context).textTheme.styleNameto get a text style from the current theme.Step 2: Check each option
Theme.of(context).textTheme.headline1 matches the correct syntax. Options A, B, and D use incorrect or invalid syntax.Final Answer:
Theme.of(context).textTheme.headline1 -> Option DQuick Check:
Access text style = Theme.of(context).textTheme.style [OK]
- Omitting Theme.of(context)
- Trying to call textTheme as a method
- Using context.textTheme directly
Text( 'Hello', style: Theme.of(context).textTheme.bodyText1?.copyWith(color: Colors.red), )Assuming the app's theme sets
bodyText1 color to blue.Solution
Step 1: Understand style overriding with copyWith
ThecopyWithmethod creates a copy of the original style but overrides specified properties, here the color is set to red.Step 2: Determine final color
Even thoughbodyText1color is blue in the theme, thecopyWith(color: Colors.red)changes it to red.Final Answer:
Red -> Option AQuick Check:
copyWith overrides color to red [OK]
- Assuming original theme color remains unchanged
- Ignoring copyWith effect
- Thinking default black applies
Text( 'Welcome', style: Theme.textTheme.headline6, )
Solution
Step 1: Check how Theme is accessed
The code usesTheme.textTheme.headline6which is invalid becauseThemeis a class and requires context to access the current theme.Step 2: Correct usage
The correct way isTheme.of(context).textTheme.headline6to get the style from the current context.Final Answer:
Missing context in Theme.of(context) call -> Option CQuick Check:
Theme.of(context) needed to access theme [OK]
- Using Theme without context
- Assuming headline6 is invalid
- Thinking Text needs TextSpan for style
subtitle1 style uses a font size of 18 and green color. Which code snippet correctly defines this inside ThemeData.textTheme?Solution
Step 1: Identify correct TextStyle properties
The correct property for font size isfontSizeand color must be aColorobject likeColors.green.Step 2: Evaluate each option
textTheme: TextTheme(subtitle1: TextStyle(fontSize: 18, color: Colors.green)) uses correct property names and values. textTheme: TextTheme(subtitle1: TextStyle(size: 18, color: Colors.green)) usessizewhich is invalid. textTheme: TextTheme(subtitle1: TextStyle(fontSize: 18, color: 'green')) uses a string for color instead of a Color object. textTheme: TextTheme(subtitle1: TextStyle(font: 18, color: Colors.green)) usesfontwhich is invalid.Final Answer:
textTheme: TextTheme(subtitle1: TextStyle(fontSize: 18, color: Colors.green)) -> Option BQuick Check:
fontSize and Colors.green are correct [OK]
- Using 'size' instead of fontSize
- Passing color as string instead of Color object
- Using invalid property names like font
Ready to prove it?
Pass the test and this pattern moves unknown
