How to use GoogleFonts as default fonts in flutter app? - flutter

I'm using GoogleFonts api in my flutter app but right now I am setting the fontStyle for every text manually to GoogleFonts.roberto but I want to set that as default in ThemeData in main.dart. But the fontFamily: GoogleFonts.roberto throws an error saying expected an string value so how can I achive that?

You can use it like this to make or modify an entire text theme to use the "Roboto" font as mentioned in their official document:
MaterialApp(
theme:ThemeData(
textTheme: GoogleFonts.robotoTextTheme(
Theme.of(context).textTheme,
),
);

Related

I see different font familiy when i upload to google play store

I use google_fonts in my flutter project. while i run the code, i can see my font settings in the app. but, when i upload my app to google play console, i cant see my font familiy settings, i see different font family. what is the problem?
static TextStyle styleGraphicBoldText = GoogleFonts.mali(
color: MainColor.mainColor,
fontSize: (AppConfig.screenHeight!) / 70,
fontWeight: FontWeight.bold,
);
home: HomePage(
prefs: prefs,
),
theme: ThemeData(
textTheme: GoogleFonts.maliTextTheme(Theme.of(context).textTheme)),
I tried to run flutter clean, changing version name. They didn't help
Google fonts uses internet, so you may have to update the dependencies in the manifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Else you can try to make the google font available in the offline by adding it in the assets folder.
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
// textTheme: GoogleFonts.maliTextTheme(
// Theme.of(context).textTheme,
// ),
fontFamily:'maliTextTheme' <-- 👈 Use this code to load offline
),
home: GoogleFontDemo(),
);
Refer this : https://stackoverflow.com/a/74949875/13431819

how can I use both sub font family in my app?

I am using font-family called "FFShamelFamily". I added two sub-font families as shown in the picture below. When I run my app, it uses the first sub-fontFamily: "SansOneBook" only. I try to use fontWeight to make use of the second sub-fontFamily:"SansOneBold" but it doesn't show. I mean that I want to use both of the sub-FontFamily, the bold one for titles and the other one for any text in the app. How can I do it?
When using a custom font you need to specify which FontFamily to use on a TextStyle. Your “sub” font FFShamelFamily-SansOneBold is correctly set up in your pubspec.yaml as a weight variation of the same Font Family.
To use it in a textStyle you simply have to use the weight you set it to with the correct family name:
Text(
“Hello, world”,
style: TextStyle(
fontFamily: “FFShamelFamily”,
fontWeight: FontWeight.w900,
),
)
Please check the official flutter documentation for using custom fonts to learn more.

flutter dark mode splash screen setting in pubspec

I use package splash_screen_view for my splash screen.
and it got the dark and light image for the default theme of the app.
but if the user theme mobile is light and wants to use the app dark, how can I set a condition for my splash?
the setting of this package sets In pubspec.yaml
so can anyone help me, please?
here is my pubspec:
Check that your MaterialApp() has theme and darkTheme set AND set themeMode: ThemeMode.system, so your app follows the settings of the user's phone.
Looks something like this:
MaterialApp(
title: 'Great App',
theme: lightTheme, //create this
darkTheme: darkTheme, //create this
themeMode: ThemeMode.system, //follows user's device setting
home: GoHome(),
);
Your splashscreen will follow the correct setting.
Bonus: Search for flutter_native_splash to ALSO set the native splashcolor to phonesetting.

Flutter how lo listen device dark theme change instantly

My flutter app doesnt listen user's preferences changes at dark theme feature. I need to hard restart my app to reflect the changes.
How to make my flutter theme change instantly once I turn on / off the dark theme in device setting?
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
brightness: Brightness.light
),
darkTheme: ThemeData(
brightness: Brightness.dark
),
themeMode: ThemeMode.system,
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
Thanks
I would recommend you give this plugin Adaptive Chameleon Theme a try. It is designed to help you switch theme modes with ease be it from light to dark or even utilising the default user defined device theme.
Like so:
// sets theme mode to dark
AdaptiveChameleonTheme.of(context).changeThemeMode(dark: true);
// sets theme mode to light
AdaptiveChameleonTheme.of(context).changeThemeMode(dark: false);
// sets theme mode to system default
AdaptiveChameleonTheme.of(context).changeThemeMode(dynamic: true);
It also comes with the added benefit of allowing you to change the theme colours if you need to.
AdaptiveChameleonTheme.of(context).setTheme(AppThemes.LightRed);
AdaptiveChameleonTheme.of(context).setTheme(AppThemes.LightBlue);
The selections are stored in shared preferences and persisted in the device memory.
Full disclosure, I am the developer of the plugin so you can consider this a shameless plug. However, I too faced similar challenges in the past leading me to develop this quick and easy to use solution.
To see the change you need two things, use StatefulWidget and call setState everytime user change theme.
IconButton(
onPressed: () {
setState(() {
functionToChangeTheme();
});
},
icon: Icon(Icons.add),
)

Flutter setting default font family for non-material apps

I want to use a default font family for my flutter app but according to the Flutter docs for setting a default font family we should declare it as a theme data in our MaterialApp instance
Text : To use a font as the default, set the fontFamily property as part of the app’s theme. The value provided to fontFamily must match the family name declared in the pubspec.yaml.
MaterialApp(
title: 'Custom Fonts',
// Set Raleway as the default app font.
theme: ThemeData(fontFamily: 'Raleway'),
home: MyHomePage(),
);
But I don't want to use MaterialApp for my app and I want to set a font as the default font of my application , any solution ?
No.
you can use your font anywhere that you want
Text("Helloo!",style:TextStyle(fontFamily:"font name"))