【问题标题】:Flutter: How to change font of Appbar text?Flutter:如何更改 Appbar 文本的字体?
【发布时间】:2020-03-11 12:44:47
【问题描述】:

这是我当前的 main.dart 代码。我尝试将'主题:ThemeData(fontFamily:'Bebas Neue Regular')'放在标题下:Text('SMARTID'),因为我想更改为SMARTID的Appbar文本,但它似乎不起作用。

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
  home: MyApp(),
  debugShowCheckedModeBanner: false,
));

class MyApp extends StatefulWidget {
  @override 
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override 
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Smart ID',
      home: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/background.png"), fit: BoxFit.cover)),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            elevation: 0,
            backgroundColor: Color(0xff05B068),
            title: Text('SMARTID'),
            theme: ThemeData(fontFamily: 'Bebas Neue Regular'),
            centerTitle: true,
          ),
        ),
      )
    );
  }
}

【问题讨论】:

  • @RavinderKumar 我对 StackOverFlow 还是很陌生。我不知道为什么它不被接受。
  • @RavinderKumar 但是您的代码有效,无论如何我再次接受了您的回答。顺便谢谢!
  • 不客气:)

标签: flutter dart


【解决方案1】:

照着做,

appBar: AppBar(
            elevation: 0,
            backgroundColor: Color(0xff05B068),
            title: Text('SMARTID',style: TextStyle(fontFamily: 'YOUR_FONT_FAMILY')),
            centerTitle: true,
          ),

编辑:不要忘记像这张图片一样创建字体文件夹,

然后打开你的 pubspec.yaml 并像这样声明字体,

别忘了领取包裹

【讨论】:

  • 有必要放重吗?不增重会怎样?
  • 完全没有必要。我只是给你一个我自己的代码的示例。更多信息,您可以前往this
【解决方案2】:

此外,如果您想在我们的整个应用程序中应用,您也可以直接在您的 ThemeData 中声明 textStyle。像这样:

final ThemeData themeData = ThemeData(
  ...
  appBarTheme: AppBarTheme(
    titleTextStyle: TextStyle(
      fontWeight: FontWeight.bold,
    ),
  ),
  ...
);

【讨论】:

    【解决方案3】:

    appBar 没有任何主题属性。因此,您可以通过将样式应用于您的标题来在应用栏中添加字体,例如,

    appBar: AppBar(
            elevation: 0,
            backgroundColor: Color(0xff05B068),
            title: Text('SMARTID', style: TextStyle(fontFamily: "Tomorrow"),),
            centerTitle: true,
          ),
    

    您可以从Google Fonts下载任何字体

    将您的自定义字体放入您的根项目>字体包

    同时,在您的 pubspec.yaml 文件中添加字体详细信息,例如,

    fonts:
        - family: Tomorrow
          fonts:
    

    带有样式的不同自定义字体的路径

            - asset: fonts/Tomorrow-Regular.ttf
              style: normal
            - asset: fonts/Tomorrow-Medium.ttf
            - asset: fonts/Tomorrow-Black.ttf
            - asset: fonts/Tomorrow-Italic.ttf
              style: italic
            - asset: fonts/Tomorrow-Bold.ttf
              weight: 700
            - asset: fonts/Tomorrow-Light.ttf
    

    你准备好了。

    【讨论】:

      【解决方案4】:

      您可以通过传递一个 TextStyle 来更改 Text 的文本样式,您可以使用字体系列、大小等创建它。

      类似的东西

      文本('SMARTID',style: TextStyle(fontFamily: 'open_sans'))

      【讨论】:

        猜你喜欢
        • 2021-04-06
        • 1970-01-01
        • 2021-10-16
        • 2021-11-02
        • 1970-01-01
        • 2013-06-05
        • 2021-09-06
        • 2021-04-18
        • 2019-11-23
        相关资源
        最近更新 更多