Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Flutter Themes and Styles

11. Flutter Themes and Styles

Themes and styles are essential for creating consistent and visually appealing user interfaces in your Flutter app. Themes define the overall design of your app, while styles specify the appearance of individual widgets. In this tutorial, we'll explore how to use themes and styles effectively in Flutter.

1. Understanding Themes and Styles

Themes:

A theme in Flutter defines the overall design and appearance of your app, including colors, typography, and other visual properties. By applying a theme, you can ensure that your app's UI elements follow a consistent design language.

Styles:

Styles in Flutter allow you to customize the appearance of individual widgets, such as text, buttons, and containers. Styles include properties like font size, color, padding, and more.

2. Creating a Flutter Theme

Let's create a simple Flutter app with a custom theme.

Step 1: Create a Flutter Project

If you haven't already, create a new Flutter project:
flutter create themes_and_styles_example
 

Step 2: Update'pubspec.yaml'

In your 'pubspec.yaml' file, ensure you have the 'flutter/material.dart' package included:
dependencies:
  flutter:
    sdk: flutter
 

Then, run 'flutter pub get' to fetch the dependencies.

Step 3: Define a Custom Theme

In the 'lib/main.dart' file, define a custom theme using the 'ThemeData' class:
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Themes and Styles Example',
      theme: ThemeData(
        primaryColor: Colors.blue, // Define the primary color
        accentColor: Colors.redAccent, // Define the accent color
        fontFamily: 'Roboto', // Define the default font
      ),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Theme Example'),
      ),
      body: Center(
        child: Text(
          'Hello, Flutter!',
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}
 

In this code, we've defined a custom theme with primary and accent colors, as well as a default font family. We've also used this theme in the 'MaterialApp' widget.

Step 4: Run the App

Run your app using 'flutter run'. You should see a simple app with a custom theme applied to the app bar and text widget.

3. Creating and Applying Styles

Styles in Flutter allow you to customize the appearance of individual widgets. Let's create and apply a style to a text widget.

Step 5: Define a Text Style

In the 'lib/main.dart' file, define a custom text style:
final TextStyle customTextStyle = TextStyle(
  fontSize: 36,
  color: Colors.green,
  fontWeight: FontWeight.bold,
);

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Themes and Styles Example'),
      ),
      body: Center(
        child: Text(
          'Hello, Flutter!',
          style: customTextStyle, // Apply the custom text style
        ),
      ),
    );
  }
}

 

In this code, we've defined a custom text style called 'customTextStyle' with specific properties like font size, color, and font weight. We then applied this style to the 'Text' widget in the 'HomeScreen'.

Step 6: Run the App

Run your app using 'flutter run'. You should now see the text "Hello, Flutter!" with the custom style applied.

4. Overriding Theme Properties

You can override theme properties for specific widgets, allowing for customization while maintaining a consistent overall design.

Step 7: Override Theme Properties

In the 'lib/main.dart' file, override the theme properties for the 'AppBar':
class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Themes and Styles Example'),
        backgroundColor: Colors.yellow, // Override the app bar's background color
        textTheme: Theme.of(context).textTheme.copyWith(
              headline6: TextStyle(
                fontSize: 24,
                color: Colors.black, // Override the app bar's text color
              ),
            ),
      ),
      body: Center(
        child: Text(
          'Hello, Flutter!',
          style: customTextStyle,
        ),
      ),
    );
  }
}
 

In this code, we've overridden the 'backgroundColor' and 'textTheme' properties of the 'AppBar' widget to customize its appearance.

Step 8: Run the App

Run your app using 'flutter run'. The app should now display the app bar with the overridden background color and text color.

Conclusion:

Themes and styles in Flutter allow you to create visually appealing and consistent user interfaces. By defining custom themes, styles, and overriding properties, you can tailor the appearance of your app to your specific design requirements. Experiment with different theme and style properties to achieve the desired look and feel for your Flutter app.