text
stringlengths
1
474
),
// Define the default `TextTheme`. Use this to specify the default
// text styling for headlines, titles, bodies of text, and more.
textTheme: TextTheme(
displayLarge: const TextStyle(
fontSize: 72,
fontWeight: FontWeight.bold,
),
// ···
titleLarge: GoogleFonts.oswald(
fontSize: 30,
fontStyle: FontStyle.italic,
),
bodyMedium: GoogleFonts.merriweather(),
displaySmall: GoogleFonts.pacifico(),
),
),
home: const MyHomePage(
title: appName,
),
);<code_end>
Most instances of ThemeData set values for the following two properties. These properties affect the entire app.To learn what colors, fonts, and other properties, you can define,
check out the ThemeData documentation.<topic_end>
<topic_start>
Apply a theme
To apply your new theme, use the Theme.of(context) method
when specifying a widget’s styling properties.
These can include, but are not limited to, style and color.The Theme.of(context) method looks up the widget tree and retrieves
the nearest Theme in the tree.
If you have a standalone Theme, that’s applied.
If not, Flutter applies the app’s theme.In the following example, the Container constructor uses this technique to set its color.
<code_start>Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 12,
),
color: Theme.of(context).colorScheme.primary,
child: Text(
'Text with a background color',
// ···
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.onPrimary,
),
),
),<code_end>
<topic_end>
<topic_start>
Override a theme
To override the overall theme in part of an app,
wrap that section of the app in a Theme widget.You can override a theme in two ways:<topic_end>
<topic_start>
Set a unique ThemeData instance
If you want a component of your app to ignore the overall theme,
create a ThemeData instance.
Pass that instance to the Theme widget.
<code_start>Theme(
// Create a unique theme with `ThemeData`.
data: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.pink,
),
),
child: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
),
);<code_end>
<topic_end>
<topic_start>
Extend the parent theme
Instead of overriding everything, consider extending the parent theme.
To extend a theme, use the copyWith() method.
<code_start>Theme(
// Find and extend the parent theme using `copyWith`.
// To learn more, check out the section on `Theme.of`.
data: Theme.of(context).copyWith(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.pink,
),
),
child: const FloatingActionButton(
onPressed: null,
child: Icon(Icons.add),
),
);<code_end>
<topic_end>
<topic_start>
Watch a video on Theme
To learn more, watch this short Widget of the Week video on the Theme widget:<topic_end>
<topic_start>
Try an interactive example
<code_start>import 'package:flutter/material.dart';
// Include the Google Fonts package to provide more text format options
// https://pub.dev/packages/google_fonts
import 'package:google_fonts/google_fonts.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});