I'm trying to style my app globally and I have some questions about hot the properly retrieve the value like this!
This code it's not working because there is no context
at this point, but is there some way to get this value?
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
ThemeData lightTheme = ThemeData(
useMaterial3: true,
// Text Style
textTheme: TextTheme(
titleMedium: GoogleFonts.nunito(fontWeight: FontWeight.bold, letterSpacing: -1, fontSize: 16),
),
// AppBar
appBarTheme: AppBarTheme(
titleTextStyle: Theme.of(context).textTheme.titleMedium,
),
);
Instead of globally declaring theme data, use a class with static method returning themeData with context as parameter:
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeClass.getThemeData(context),
home: const HomePage(),
);
}
class ThemeClass {
static ThemeData getThemeData(BuildContext context) {
final lightTheme = ThemeData(
useMaterial3: true,
// Text Style
textTheme: TextTheme(
titleMedium: GoogleFonts.nunito(
fontWeight: FontWeight.bold, letterSpacing: -1, fontSize: 16),
),
// AppBar
appBarTheme: AppBarTheme(
titleTextStyle: Theme.of(context).textTheme.titleMedium,
),
);
return lightTheme;
}