Consider this Flutter widget that uses a text theme:
Text( 'Hello Flutter', style: Theme.of(context).textTheme.headlineMedium, )
What style will the text have if the app's theme sets headlineMedium to 24px font size and blue color?
Text('Hello Flutter', style: Theme.of(context).textTheme.headlineMedium)TextTheme styles override default text styles when applied.
The headlineMedium style from the theme sets both font size and color, so the text uses those.
Choose the correct statement about TextTheme in Flutter.
Think about what TextTheme controls in an app's look.
TextTheme provides a set of named text styles for different uses like headlines and body text. It can be customized and used throughout the app.
Analyze this Flutter code snippet:
Text( 'Welcome', style: Theme.of(context).textTheme.headlineLarge.copyWith(fontWeight: FontWeight.bold), )
What error will occur when running this code?
Text('Welcome', style: Theme.of(context).textTheme.headlineLarge.copyWith(fontWeight: FontWeight.bold))Consider if headlineLarge can be null in some themes.
If the theme does not define headlineLarge, it can be null, so calling copyWith on null causes a runtime error.
If the app's ThemeData changes at runtime, when will widgets using Theme.of(context).textTheme update their text styles?
Think about Flutter's reactive UI system and how it rebuilds widgets.
Flutter rebuilds widgets that depend on inherited widgets like Theme when the theme changes, so text styles update immediately on the next frame.
Given this Flutter app snippet:
MaterialApp(
theme: ThemeData(
textTheme: TextTheme(
bodyMedium: TextStyle(color: Colors.red),
),
),
home: Scaffold(
body: Center(
child: Text('Hello'),
),
),
)Why does the text appear black instead of red?
Check the default text style used by Text widget.
The Text widget uses bodyText2 style by default, but the theme sets bodyMedium. Since bodyText2 is not set, default black color is used.