While using GetMaterialApp fontFamily for the Theme is not Changing

119

Solution 1

Once this happened to me while working with some google fonts. I tried different methods, there is nothing wrong with GetMaterialApp()
Option 1: flutter clean to clear all data, then update all the packages, and then flutter pub get
Option 2: Initialy this is what I did. In the textTheme, add TextStyle for different types of text (headline1,bodyText1,etc)

fontFamily: "Sen",
textTheme: const TextTheme(
   headline1: TextStyle(
     fontSize: 96.0,
     fontWeight: FontWeight.w300,
     fontFamily: 'Sen',
     color: kPrimaryTextColor,
   )

Option 3: If nothing works, try flutter upgrade

Solution 2

I have tried using the minimal code but everything works for me. The font changes as you can see in the VIDEO DEMO. I suggest you to clean the cache or update the packages version to latest

Example

Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      initialRoute: RoutesName.homePage,
      getPages: Routes.getPages(),
      initialBinding: InitialBindings(),
      theme: ThemeData(fontFamily: "Blaka"),
    );
  }

pubspec file

flutter:
  uses-material-design: true

  fonts: 
    - family: Blaka
      fonts: 
        - asset: assets/fonts/Blaka/Blaka-Regular.ttf
Share:
119
Sanjay TM
Author by

Sanjay TM

I'm a Student, Teacher, Creator, Tech geek, 3D designer, and Artest. I like to learn new things each day, I like to solve problems. I mainly work with hardware and software. In my free time, I work on modeling and designing. I work with MicroControllers, Processors, C++, Python, Flutter, and 3D printers.

Updated on December 01, 2022

Comments

  • Sanjay TM
    Sanjay TM over 1 year

    I am using Getx for my state management. So When I Change MaterialApp to GetMaterialApp, the fontFamily switched back to the default.

    pubspec.yaml file

    flutter:
      uses-material-design: true
      fonts:
        - family: Sen
          fonts:
            - asset: assets/fonts/Sen-Regular.ttf
              weight: 300
            - asset: assets/fonts/Sen-Bold.ttf
              weight: 700
    

    and I have saved Sen.ttf files in the assets/fonts folder
    enter image description here

    main.dart

    Widget build(BuildContext context) {
        return GetMaterialApp(
          theme: ThemeData(
            colorScheme: ColorScheme.fromSwatch().copyWith(
            primary: kPrimaryColor,
            secondary: kSecondaryColor,
           ),
          fontFamily: "Sen"
          )
          debugShowCheckedModeBanner: false,
          home: const MainScreen(),
        );
      }
    

    If I switch back to MaterialApp font will work fine.
    Any help will be appreciated, Thank You.

    • Dabbel
      Dabbel almost 2 years
      Removing the leading assets folder might help: - asset: fonts/Sen-Bold.ttf
    • Sanjay TM
      Sanjay TM almost 2 years
      Tried but not helping
    • Dabbel
      Dabbel almost 2 years
      I tried to reproduce the problem, but everything for fine for me with GetMaterialApp and fontFamily. The only difference is that I use ` getPages:` instead of home:.
  • Sanjay TM
    Sanjay TM almost 2 years
    Really appreciate the effort you put into this, thank you. I will try and update
  • Sanjay TM
    Sanjay TM almost 2 years
    The second method worked for me thank you.