Working with Material Theme and Custom Fonts in Dart's Flutter Framework

in #utopian-io7 years ago

flutter-logo.jpg

Repository

https://github.com/flutter/flutter

What Will I Learn?

  • You will learn how to use Material Themeing in Flutter
  • You will learn about the Theme Data Widget
  • You will learn about the Copy With Method
  • You will learn how to import Custom Font Families
  • You will learn how to use contextual calls to change specific colors and text styles

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A fair understanding aesthetics
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • Some understanding of UX design

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Flutter Video Tutorial, we take a look at how we can implement uniform themes according to the material design specification into our application. We also take a look at the various ways that we can customize and change the different looks and styles to make our application more unique. Finally, look at importing custom fonts and using them inside of our application.

Outline for this Tutorial
Item 1: Material Themeing Inside of Flutter

In order to share font styles and colors throughout our application, we need to take advantage of themes. In flutter, there are two main ways that you can define themes; through app wide declarations and on individual widgets. The Material Application Widget contains a property called theme which allows us to insert a ThemeData object which proliferates the colors and styles that we specify throughout our entire application.

root-theme.jpg

This image shows off our Root widget for this application. Inside of this root widget, we create a Theme Data object by calling a function called buildThemeData. This function can be called directly in the theme property of the material application widget.

Item 2: Copy With and Customize

While uniform themes are a great way to create a consistent look for your application; they can make applications look very similar to each other. This is why we usually want to further customize the themes of our applications through the use of contextual function calls and the copyWith method. The Contextual Function calls allow us to directly fetch the theme data of the root widget anywhere inside of the application. We can then copy this theme data object using the copyWith method and change the properties that we want to change for the widget that we are working on.

color-theme-copy-with.jpg

In this line of code, we are specifying the color of a widget in our application. Specifically, we are specifying the card color of a card widget. We are able to fetch the entire theme data object using the Theme.of(context) call and then the copyWith method allows us to change the single card color value for this instance of the object. We can then just grab that card color value and pass it into the color property.

Item 3: Importing and Using Custom Fonts in Flutter

Typography and Fonts can be very important in mobile applications. They make text more readable and allow you to draw more attention to various pieces of text. Fonts can make a format look bad or look great depending on how they are integrated into the application. Flutter comes with a few default fonts, however, we are also able to import as many custom font assets as we would like. After importing the fonts, we can directly call them in the dart code by passing a string to a property.

fonts.jpg

Above you can see the font declarations inside of the pubspec.yaml file for our application. In this file, we are importing two different font families through five different files. Notice that some of the font files are prefaced with weight and style keywords. In Dart, we are able to directly specify which font file should correspond with which font family, font weight and font style. This allows us to call these values inside of our application later in a way that feels comfortable.

The source code for this project can be found here

The FiraCode Font may be found here

The Material Design Color Tool can be found here

Video Tutorial

Related Videos:

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Sort:  

Hey @tensor
Thanks for contributing on Utopian.
We're already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thank you @tensor for your contribution.

Your contribution has been reviewed and scored accordingly.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for always moderating my contributions so quickly.

Thanks for sharing Sir. I'll be training gradually with your materials fir reference. Thanks again