ThemeData
ThemeData is a class in Flutter that provides a way to define a theme for your application. The theme consists of colors, fonts, and other visual elements that define the look and feel of your app.
Here is a simple code example of how to use ThemeData in Flutter:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
theme: ThemeData(
primaryColor: Colors.blue,
accentColor: Colors.red,
fontFamily: 'Roboto',
),
home: MyHomePage(),
),
);
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Center(
child: Text(
'Hello, world!',
style: Theme.of(context).textTheme.headline4,
),
),
);
}
}In this example, we define a theme for our app using the ThemeData class. We set the primary color to blue, the accent color to red, and the font family to Roboto. We then use this theme in our app by wrapping our MaterialApp widget with it.
We can then access the theme data in any of our widgets using the Theme.of(context) method. This allows us to easily apply the theme to our app's visual elements.
That's it! With just a few lines of code, we can define a theme for our app and use it to create a consistent look and feel throughout our entire application.
Parameters
Here are some common parameters that can be set using ThemeData:
primaryColor: The primary color of the theme, used in the app bar and other visual elements.
accentColor: The accent color of the theme, used for highlighting and other visual accents.
brightness: The brightness of the theme, which can be set toBrightness.lightorBrightness.dark.
fontFamily: The default font family used throughout the app.
textTheme: An object that defines the styles for various text elements, such asheadline1,bodyText2, andcaption.
buttonTheme: An object that defines the styles for buttons in the app, such asButtonThemeData.
cardTheme: An object that defines the styles for cards in the app, such asCardTheme.
inputDecorationTheme: An object that defines the styles for input fields and labels in the app, such asInputDecorationTheme.
scaffoldBackgroundColor: The background color of the app's scaffold, which is the background color of the app's pages.