Applying Themes and Styling
1. Understanding ThemeData Class
| Property | Values | Description |
|---|---|---|
| primaryColor | Color |
Primary color for the app's theme, used for app bars and buttons. |
| primaryColorDark | Color |
Darker variant of the primary color, used for status bars. |
| primaryColorLight | Color |
Lighter variant of the primary color, used for backgrounds. |
| accentColor | Color |
Accent color for the app's theme, used for interactive elements. |
| textTheme | TextTheme |
Defines the typography and text styles for various parts of the app. |
| buttonTheme | ButtonThemeData |
Defines the appearance of buttons in the app. |
| appBarTheme | AppBarTheme |
Defines the appearance of app bars in the app. |
| iconTheme | IconThemeData |
Defines the appearance of icons in the app. |
| dialogTheme | DialogTheme |
Defines the appearance of dialog boxes in the app. |
| bottomAppBarColor | Color |
Color of the bottom app bar. |
Example: Defining a ThemeData
final ThemeData myTheme = ThemeData(
primaryColor: Colors.blue,
accentColor: Colors.orange,
textTheme: TextTheme(
headline1: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
bodyText1: TextStyle(fontSize: 16),
),
appBarTheme: AppBarTheme(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
);
2. Setting App Theme
Themes in Flutter define the visual style of an app, including colors, fonts, and widget styles. The ThemeData class is used to configure these properties.
| Property | Values | Description |
|---|---|---|
| primaryColor | Color(0xFF6200EE) |
Primary color used for app bars and buttons |
| accentColor | Color(0xFF03DAC5) |
Accent color used for interactive elements |
| textTheme | TextTheme |
Defines text styles for various text elements |
| buttonTheme | ButtonThemeData |
Configures appearance of buttons |
Example: Applying a Custom Theme
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.deepPurple,
accentColor: Colors.amber,
textTheme: TextTheme(
headline1: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
bodyText1: TextStyle(fontSize: 16),
),
),
home: Scaffold(
appBar: AppBar(title: Text("Themed App")),
body: Center(child: Text("Hello, themed world!")),
),
);
}
}
3. Defining Primary and Accent Colors
Flutter allows you to define primary and accent colors for your app's theme using the primarySwatch or primaryColor properties in your ThemeData.
| Property | Description | Values |
|---|---|---|
| primaryColor | Defines the primary color used for app bars, buttons, and other UI elements. | Color |
| accentColor | Defines the accent color used for interactive elements like checkboxes and sliders. | Color |
| primarySwatch | Defines a swatch of colors to be used for the primary color, including shades like 50, 100, etc. |
MaterialColor |
Example: Defining Primary and Accent Colors
MaterialApp(
theme: ThemeData(
primaryColor: Colors.blue,
accentColor: Colors.red,
primarySwatch: Colors.purple,
),
home: MyHomePage(),
)
4. Using Theme.of(context) Method
The Theme.of(context) method is a core way to access and apply theme data in Flutter. It retrieves the current theme from the widget tree, allowing you to customize UI elements based on the app's theme.
| Property | Description | Usage Example |
|---|---|---|
| primaryColor | Primary color used for app bars, buttons, etc. | Theme.of(context).primaryColor |
| textTheme | Text theme to apply to text widgets. | Theme.of(context).textTheme.headlineMedium |
| backgroundColor | Background color for the app or widget. | Theme.of(context).backgroundColor |
| textTheme | Text theme to apply to text widgets. | Theme.of(context).textTheme.bodyMedium |
Example: Applying Theme Colors
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Theme.of(context).primaryColor,
child: Text(
'Hello World',
style: Theme.of(context).textTheme.headlineMedium,
),
);
}
}
Theme.of(context) is reactive and will update automatically when the theme changes.
5. Creating Custom Themes
Example: Defining a Custom Theme
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
textTheme: TextTheme(
headlineLarge: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
bodyMedium: TextStyle(
fontSize: 16,
color: Colors.black87,
),
),
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Theme Example'),
),
body: Center(
child: Text(
'This text uses the custom theme',
style: Theme.of(context).textTheme.bodyMedium,
),
),
);
}
}
| Property | Values | Description |
|---|---|---|
| primarySwatch | Colors.blue |
Primary color for the app's theme |
| brightness | Brightness.light |
Light or dark theme variant |
| textTheme | TextTheme |
Custom text styles for different text types |
TextTheme allows customization of various text styles like headlines, body text, etc.
6. Using ColorScheme Class
The ColorScheme class in Flutter is used to define a set of colors that are applied consistently across an app's UI. It provides a structured way to manage color themes, particularly useful for implementing dark and light modes.
| Property | Description | Example Value |
|---|---|---|
| primary | Primary color used for main UI elements like buttons and app bars. | Color(0xFF6200EE) |
| onPrimary | Color used for text or icons on top of the primary color. | Color(0xFFFFFFFF) |
| secondary | Secondary color used for supporting UI elements. | Color(0xFF03DAC6) |
| onSecondary | Color used for text or icons on top of the secondary color. | Color(0xFF000000) |
| surface | Color for surfaces like cards and dialogs. | Color(0xFFFFFFFF) |
| onSurface | Color used for text or icons on top of the surface color. | Color(0xFF000000) |
| background | Background color of the app. | Color(0xFFF5F5F5) |
| onBackground | Color used for text or icons on top of the background color. | Color(0xFF000000) |
| error | Color used for error states or messages. | Color(0xFFB00020) |
| onError | Color used for text or icons on top of the error color. | Color(0xFFFFFFFF) |
Example: Defining a ColorScheme
final colorScheme = ColorScheme(
primary: Colors.deepPurple,
onPrimary: Colors.white,
secondary: Colors.teal,
onSecondary: Colors.black,
surface: Colors.white,
onSurface: Colors.black,
background: Colors.grey.shade100,
onBackground: Colors.black,
error: Colors.red,
onError: Colors.white,
);
Example: Using ColorScheme in Theme
MaterialApp(
theme: ThemeData(
colorScheme: colorScheme,
),
home: MyHomePage(),
);
7. Customizing Text Themes
| Property | Values | Description |
|---|---|---|
| fontFamily | 'Roboto' |
Font family to use for text |
| fontSize | 16.0 |
Base font size in points |
| fontWeight | FontWeight.normal |
Font weight (e.g., FontWeight.bold) |
| color | Colors.black |
Text color |
Example: Applying Custom Text Theme
TextTheme customTextTheme = TextTheme(
headlineLarge: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
color: Colors.blue,
),
bodyMedium: TextStyle(
fontSize: 16,
fontWeight: FontWeight.normal,
fontFamily: 'Roboto',
color: Colors.black87,
),
);
TextTheme is part of the MaterialApp theme and can be customized to match your app's design language.
8. Customizing AppBar Theme (AppBarTheme)
| Property | Values | Description |
|---|---|---|
| backgroundColor | Color |
Background color of the AppBar |
| brightness | Brightness |
Light or dark theme for the AppBar |
| iconTheme | IconThemeData |
Theme for icons inside the AppBar |
| textTheme | TextTheme |
Text theme for AppBar title and actions |
| elevation | double |
Shadow elevation of the AppBar |
| toolbarTextStyle | TextTheme |
Text style for toolbar text |
| titleTextStyle | TextTheme |
Text style for title text |
Example: Customizing AppBar Theme
AppBarTheme appBarTheme = AppBarTheme(
backgroundColor: Colors.blue,
brightness: Brightness.dark,
iconTheme: IconThemeData(color: Colors.white),
textTheme: TextTheme(
headline6: TextStyle(color: Colors.white, fontSize: 20),
),
elevation: 4.0,
);
9. Customizing Button Themes (ElevatedButtonTheme)
Customize the appearance of ElevatedButton widgets using ElevatedButtonTheme.
| Property | Description | Values |
|---|---|---|
| data | The theme data for elevated buttons. | ElevatedButtonThemeData |
Example: Customizing ElevatedButton Theme
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
foregroundColor: MaterialStateProperty.all(Colors.white),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
),
),
home: Scaffold(
appBar: AppBar(title: Text("Custom Button Theme")),
body: Center(
child: ElevatedButton(
onPressed: () {},
child: Text("Custom Button"),
),
),
),
);
}
}
10. Customizing Input Decoration Theme (InputDecorationTheme)
The InputDecorationTheme allows you to customize the appearance of input fields like TextField and TextFormField. It controls the look of the input decoration, including labels, hints, borders, and icons.
| Property | Description | Example Value |
|---|---|---|
| hintStyle | Defines the style of the hint text. | TextStyle(fontSize: 14, color: Colors.grey) |
| labelStyle | Defines the style of the label text. | TextStyle(fontSize: 16, fontWeight: FontWeight.bold) |
| border | Defines the border of the input field. | UnderlineInputBorder(borderRadius: BorderRadius.circular(8)) |
| focusedBorder | Defines the border when the input is focused. | OutlineInputBorder(borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: Colors.blue)) |
| enabledBorder | Defines the border when the input is enabled but not focused. | OutlineInputBorder(borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: Colors.grey)) |
| errorBorder | Defines the border when the input has an error. | OutlineInputBorder(borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: Colors.red)) |
| focusedErrorBorder | Defines the border when the input is focused and has an error. | OutlineInputBorder(borderRadius: BorderRadius.circular(8), borderSide: BorderSide(color: Colors.red)) |
| contentPadding | Defines the padding inside the input field. | EdgeInsets.all(12) |
Example: Customizing InputDecorationTheme
ThemeData theme = ThemeData(
inputDecorationTheme: InputDecorationTheme(
hintStyle: TextStyle(color: Colors.grey),
labelStyle: TextStyle(fontWeight: FontWeight.bold),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(color: Colors.blue, width: 2),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(color: Colors.grey),
),
),
);