In this lesson we'll cover: how to style text using TextTheme
, custom fonts and basic Material Design theming with TextStyle
. We'll also cover code organization when working with style.
Check out the tourismandco repo's step/step04
branch for the code we'll cover in this lesson.
TextTheme
, fonts assets and TextStyle
assets/fonts
.assets/fonts
in the step/step03
branch of this repo and copy it to our new directory.pubspec.yaml
file so we can use our fonts.fonts
to pubspec.yaml
# pubspec.yaml
flutter:
# ...
fonts:
- family: Montserrat
fonts:
- asset: assets/fonts/Montserrat-Regular.ttf
weight: 300
- asset: assets/fonts/Montserrat-Bold.ttf
weight: 600
# ...
Again, be careful of the indentation here.
Note: this screen isn't currently scrollable, so if the content for this screen overflows, we'll see some weird rendering issues. Later, we'll take care of this issue.
TextTheme
theme
parameter of our MaterialApp
widget, located in app.dart
.app.dart
to the following:
// app.dart
import 'package:flutter/material.dart';
import 'screens/location_detail/location_detail.dart';
import 'style.dart';
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LocationDetail(),
theme: ThemeData(textTheme: TextTheme(body1: Body1Style)),
);
}
}
// ...
textTheme
param of ThemeData
.style.dart
.style.dart
file so we can cleanly keep all our app styles in one place.style.dart
// style.dart
import 'package:flutter/material.dart';
const String FontNameDefault = 'Montserrat';
const Body1Style = TextStyle(
fontFamily: FontNameDefault,
fontWeight: FontWeight.w300,
fontSize: 26.0,
color: Colors.black,
);
const
here (a "constant"), which ensures that the value of this constant is assigned at compile-time rather than runtime. A small optimization.TextStyle
allows to style an individual Text
widget using its style
param.ThemeData
, no code changes will be needed. Pretty cool.const
variables, we'll need to restart our app, as these changes aren't supported by Flutter's Hot Reload feature.In this lesson, we covered the basics of layout, imagesand text. Our "Tourism & Co." app is taking shape.