Flutter:我想在运行时将字体应用于整个应用程序文本 [英] Flutter: I want to apply font on runtime to whole app Text

查看:123
本文介绍了Flutter:我想在运行时将字体应用于整个应用程序文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有语言选择设置.根据语言选择英语或阿拉伯语,我想使用不同的字体系列.我是在MaterialApp()中完成的,但是并没有达到我的目标.

I have language selection settings in my app. On the basis of language selection English or Arabic , I want to use different font family. I did it inside MaterialApp() but it will not achieve my target.

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'OnlyTick',  
      theme: ThemeData(
        primarySwatch: Colors.indigo,
        buttonColor: Colors.indigo,
        iconTheme: IconThemeData(color: Colors.indigo),
        accentIconTheme: IconThemeData(color: Colors.indigoAccent),
        fontFamily: appLang == 'English'?'Proxima':'DroidKufi',
      ),
      debugShowCheckedModeBanner: false,
      home: SplashScreenPage(),
      localizationsDelegates: [
        // const TranslationsDelegate(), //TODO: will create it later, refer https://www.didierboelens.com/2018/04/internationalization---make-an-flutter-application-multi-lingual/
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', ''),
        const Locale('ar', ''),
      ],
      // setHome(),
      routes: routes,
    );
  }

推荐答案

假设您正在使用以下结构运行项目:

Assuming you're running your project using the following structure:

    void main() => runApp(new App());

将功能setAppFontFamily添加到您的App类中,该类将负责更改应用程序fontFamily:

Add the function setAppFontFamily to your App class which will be responsible for changing you application fontFamily:

    class App extends StatefulWidget {
      App({Key key,}) :

     super(key: key);

     @override
     _AppState createState() => new _AppState();

      static void setAppFontFamily(BuildContext context, String _selectedFontFamily) {
      _AppState state = context.ancestorStateOfType(TypeMatcher<_AppState>());
         state.setState(() {
            state._fontFamily = _selectedFontFamily;
         });
      }


     }

之后,将一个局部变量添加到您的_AppState类中或您当前为State类使用的任何名称;

After that add a local variable to your _AppState class or whatever the name you're currently using for your State class;

     String _fontFamily = 'Proxima' ; //Initial value before any selection is made

然后将_fontFamily分配给Material App ThemefontFamily属性:

Then assign _fontFamily to the fontFamily property of your Material App Theme:

     theme: ThemeData(
     primarySwatch: Colors.indigo,
     buttonColor: Colors.indigo,
     iconTheme: IconThemeData(color: Colors.indigo),
     accentIconTheme: IconThemeData(color: Colors.indigoAccent),
     fontFamily: _fontFamily,
     ),

完成所有这些步骤后,您可以使用以下行从应用程序中的任何位置更改应用程序的fontFamily:

After doing all these steps, you'll be able to change the fontFamily of your app from anywhere in your application using the below line:

     App.setAppFontFamily(context, 'DroidKufi');

这篇关于Flutter:我想在运行时将字体应用于整个应用程序文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆