Sure, setting up a consistent theme in your Flutter app, including a uniform font, is essential for a cohesive user experience. Here's a step-by-step guide to setting up a theme with a consistent font across your entire app:
Step 1: Add Custom Fonts
First, you need to add your custom fonts to your Flutter project.
Download the Font: Download the font files (e.g., .ttf or .otf) you want to use.
Add Fonts to the Project:
Create a fonts directory in your project’s root directory.
Place your font files inside this directory.
Update pubspec.yaml:
Add the fonts to your pubspec.yaml file.
Next, you’ll define the theme in your app, including the font family.
Create a Theme Data Class:
Create a new Dart file, e.g., theme.dart, to define your theme.
dart
import 'package:flutter/material.dart';
class AppTheme {
static ThemeData lightTheme = ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
fontFamily: 'YourCustomFont', // Set your custom font family here
textTheme: TextTheme(
bodyText1: TextStyle(fontSize: 14.0),
bodyText2: TextStyle(fontSize: 16.0),
// Add other text styles as needed
),
);
static ThemeData darkTheme = ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.blue,
fontFamily: 'YourCustomFont', // Set your custom font family here
textTheme: TextTheme(
bodyText1: TextStyle(fontSize: 14.0),
bodyText2: TextStyle(fontSize: 16.0),
// Add other text styles as needed
),
);
}
Step 3: Apply the Theme
Finally, apply the theme to your app.
Wrap Your App with MaterialApp:
In your main.dart file, wrap your app with MaterialApp and apply the theme.
dart
import 'package:flutter/material.dart';
import 'theme.dart'; // Import the theme file
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Your App Title',
theme: AppTheme.lightTheme, // Use light theme
darkTheme: AppTheme.darkTheme, // Use dark theme
themeMode: ThemeMode.light, // or ThemeMode.dark for dark theme by default
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: Text(
'Welcome to Your App!',
style: Theme.of(context).textTheme.bodyText1,
),
),
);
}
}
Summary
By following these steps, you've set up a consistent theme with a uniform font across your entire Flutter app. You can further customize the TextTheme to fit your design needs by adding more text styles as required.