I am trying to build a "theme" for my flutter app, so it is consistant throughout the project. so I thought of using the fonts Roboto-Regular, Roboto-Bold and Roboto-Medium. Below is my code
pubspec.yaml
name: customer
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/logo.png
- assets/images/lock_24px.png
- assets/images/email_24px.png
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- asset: assets/fonts/Roboto-Medium.ttf
- asset: assets/fonts/Roboto-Bold.ttf
- family: Ma Shan Zheng
fonts:
- asset: assets/fonts/MaShanZheng-Regular.ttf
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
Main.dart
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import './pages/login.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Customer App',
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
fontFamily: 'Roboto',
textTheme: TextTheme(
headline: TextStyle(fontFamily: 'Roboto-Medium', fontSize: 20.0),
button: TextStyle(fontFamily: 'Roboto-Bold', fontSize: 14.0, letterSpacing: 1.25),
)),
home: LoginPage(),
);
}
}
login.dart
Container(
margin: EdgeInsets.only(top: 60, left: 25, right: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: SizedBox(
width: double.infinity,
height: 45,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
child: Text("LOGIN",
style: Theme.of(context).textTheme.button,),
),
))
],
),
)
You can see that I have used the font styles in the Raised Button. However the font family never get applied, eventhough the other styles got applied (ex: font size, letter spacing). Why is this?
Your declaration for the custom fonts was not clear.
Change this:
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- asset: assets/fonts/Roboto-Medium.ttf
- asset: assets/fonts/Roboto-Bold.ttf
become this:
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- asset: assets/fonts/Roboto-Medium.ttf
weight: 600
- asset: assets/fonts/Roboto-Bold.ttf
weight: 700
And call them like this:
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
fontFamily: 'Roboto',
textTheme: TextTheme(
headline: TextStyle(fontWeight: FontWeight.normal, fontSize: 20.0),
button: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, letterSpacing: 1.25),
)),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With