Below is an example of how you can set up a consistent font across your entire Flutter app using the ThemeData's fontFamily property. Make sure you have already added your custom font to your project (and configured it in your pubspec.yaml). In this example, I'll assume you're using a font named "Roboto", but you can replace it with your custom font's name.
Step 1: Add the Font in pubspec.yaml
Make sure your pubspec.yaml includes something like the following:
Now, you can set up your Flutter app theme so that the same font is used everywhere:
dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ECMRC Shop',
theme: ThemeData(
// Set the default font family for the entire app
fontFamily: 'Roboto',
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
// You can also customize text themes if needed:
textTheme: TextTheme(
headline1: TextStyle(fontSize: 32.0, fontWeight: FontWeight.bold),
bodyText1: TextStyle(fontSize: 16.0),
// ...other text styles as needed
),
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ECMRC Shop'),
),
body: Center(
child: Text(
'Welcome to ECMRC Shop!',
style: Theme.of(context).textTheme.bodyText1,
),
),
);
}
}
Explanation
fontFamily: Setting this property in the ThemeData ensures that the specified font is used by default for all text widgets.
Custom textTheme: Although not strictly necessary for just setting the font, customizing the textTheme allows you to fine-tune styles (like font size and weight) for different text elements if needed.
Asset Setup: Ensure your assets (font files) are correctly referenced in the pubspec.yaml file.
Using this setup, your entire app will use the specified font family, ensuring consistency across all text elements. Enjoy building your e-commerce shop in Flutter!