Flutter Themes

 

Flutter Themes

Themes are the preset packages that contain the graphical appearances to our website or mobile app screen. It makes the user interface more attractive. We mainly use themes for sharing the colors and fonts styles throughout the app.

In mobile development, it becomes mandatory to add the Light and Dark theme for our app. Today's most people prefer the dark version of the theme over the light version theme because it makes them more comfortable to their eyes and increases battery life.

In Flutter, we can either use Theme widgets that contain the colors and font styles for a specific area of the application or define app-wide themes. The app-wide themes are also Theme widgets, which are created in the root of our app under the MaterialApp widget.

After defining a theme, we can use it in any widget wherever we need it in the app. Material widgets in Flutter can also use our Theme to set the font styles and background colors for AppBars, Buttons, Buttons, Checkboxes, and many more.

Flutter uses the default theme while creating an application. If we want to share a custom theme to the entire application, we need to use a ThemeData under the MateialApp() widget.

Sometimes we want to override the app-wide theme in the part of an application. In that case, we need to wrap the section of the app in the theme widget. Flutter allows us two approaches to do this:

  1. By creating a unique ThemeData
  2. By extending the parent theme

By creating a unique ThemeData

The first approach is used when we do not want to inherit any application colors or font styles. In that case, we will create an instance of ThemeData() and pass it to the Theme widget, as shown in the below code snippet:

  1. Theme(  
  2.   data: ThemeData(  
  3.     accentColor: Colors.blue,  
  4.   ),  
  5.   child: FloatingActionButton(  
  6.     onPressed: () {},  
  7.     child: Icon(Icons.person),  
  8.   ),  
  9. );  

By extending the parent theme

If you do not want to override anything, use the second approach that extends the parent theme. It can be handle with the use of copyWith() method. See the below code snippet:

  1. Theme(  
  2.   data: Theme.of(context).copyWith(accentColor: Colors.blue),  
  3.   child: FloatingActionButton(  
  4.     onPressed: null,  
  5.     child: Icon(Icons.person),  
  6.   ),  
  7. );  

How to use a Theme

After defining a theme, we can use it into the widget build() methods with the Theme.of(context) method. This method looks into the widget tree and returns the very first theme in the tree. If you have a not defined above your widget, the app's theme will be returned.

In the below code snippet, the FloatingActionButton uses this technique for returning the accentColor.

  1. Container(  
  2.   color: Theme.of(context).accentColor,  
  3.   child: Text(  
  4.     'Text with a background color',  
  5.     style: Theme.of(context).textTheme.headline,  
  6.   ),  
  7. );  

Let us understand how to use ThemeData in Flutter application through the below example.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() {runApp(MyApp());}  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       theme: ThemeData(  
  10.         // Define the default brightness and colors.  
  11.         brightness: Brightness.dark,  
  12.         primaryColor: Colors.lightBlue,  
  13.         accentColor: Colors.green,  
  14.   
  15.         // Define the default font family.  
  16.         fontFamily: 'Monotype Coursiva',  
  17.   
  18.         // Define the TextTheme that specifies the default  
  19.         // text styling for headlines, titles, bodies of text, and more.  
  20.         textTheme: TextTheme(  
  21.           headline: TextStyle(fontSize: 32.0, fontStyle: FontStyle.italic, fontFamily: 'Hind')  
  22.         ),  
  23.       ),  
  24.       home: MyThemePage(),  
  25.     );  
  26.   }  
  27. }  
  28.   
  29. class MyThemePage extends StatelessWidget {  
  30.   @override  
  31.   Widget build(BuildContext context) {  
  32.     return Scaffold(  
  33.       appBar: AppBar(  
  34.         title: Text('Flutter Theme Example'),  
  35.       ),  
  36.       body: Center(  
  37.         child: Container(  
  38.           color: Theme.of(context).accentColor,  
  39.           child: Text(  
  40.             'Themes contains the graphical appearances that makes the user interface more attractive.',  
  41.             style: Theme.of(context).textTheme.headline,  
  42.           ),  
  43.         ),  
  44.       ),  
  45.       floatingActionButton: Theme(  
  46.         data: Theme.of(context).copyWith(  
  47.           colorScheme:  
  48.           Theme.of(context).colorScheme.copyWith(secondary: Colors.blue),  
  49.         ),  
  50.         child: FloatingActionButton(  
  51.           onPressed: null,  
  52.           child: Icon(Icons.person),  
  53.         ),  
  54.       ),  
  55.     );  
  56.   }  
  57. }  

Output:

When we run the app in the device or emulator, we will see the UI similar to the below screenshot.

Flutter Themes


Post a Comment

Previous Post Next Post