Android 颤振:不应用自定义字体族
我正在尝试为我的Flitter应用程序构建一个“主题”,因此它在整个项目中都是一致的。所以我想到了使用字体Android 颤振:不应用自定义字体族,android,ios,flutter,Android,Ios,Flutter,我正在尝试为我的Flitter应用程序构建一个“主题”,因此它在整个项目中都是一致的。所以我想到了使用字体Roboto Regular,Roboto Bold和Roboto Medium。下面是我的代码 公共规范yaml name: customer description: A new Flutter project. # The following defines the version and build number for your application. # A version
Roboto Regular
,Roboto Bold
和Roboto Medium
。下面是我的代码
公共规范yaml
name: customer
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/logo.png
- assets/images/lock_24px.png
- assets/images/email_24px.png
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- asset: assets/fonts/Roboto-Medium.ttf
- asset: assets/fonts/Roboto-Bold.ttf
- family: Ma Shan Zheng
fonts:
- asset: assets/fonts/MaShanZheng-Regular.ttf
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
login.dart
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import './pages/login.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Customer App',
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
fontFamily: 'Roboto',
textTheme: TextTheme(
headline: TextStyle(fontFamily: 'Roboto-Medium', fontSize: 20.0),
button: TextStyle(fontFamily: 'Roboto-Bold', fontSize: 14.0, letterSpacing: 1.25),
)),
home: LoginPage(),
);
}
}
Container(
margin: EdgeInsets.only(top: 60, left: 25, right: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: SizedBox(
width: double.infinity,
height: 45,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)),
child: Text("LOGIN",
style: Theme.of(context).textTheme.button,),
),
))
],
),
)
容器(
页边距:仅限边集(顶部:60,左侧:25,右侧:10),
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
灵活的(
孩子:大小盒子(
宽度:double.infinity,
身高:45,
孩子:升起按钮(
形状:圆形矩形边框(
边界半径:新边界半径。圆形(18.0),
边:边框边(颜色:Colors.red)),
子:文本(“登录”,
样式:Theme.of(context.textTheme.button,),
),
))
],
),
)
您可以看到我使用了
凸起按钮中的字体样式。但是,即使应用了其他样式(例如:字体大小、字母间距),也永远不会应用字体系列。为什么会这样?我建议您使用新提供的软件包。它使您更容易使用所需字体,并动态加载,使您的应用程序更轻,并且它们具有您想要使用的Roboto字体:
MaterialApp(
theme: ThemeData(
textTheme: GoogleFonts.latoTextTheme(
Theme.of(context).textTheme,
),
),
);
用下面的代码替换Main.dart中的代码
headline: TextStyle(fontFamily: 'Roboto',fontWeight: FontWeight.normal,fontSize: 20.0),
button: TextStyle(fontFamily: 'Roboto',fontWeight: FontWeight.bold,fontSize: 14.0, letterSpacing: 1.25),
新年快乐:)您为三种不同的字体类型定义了相同的名称,因此当您给Roboto打电话时,他不知道您想要哪一种。
例如,当您调用Roboto Medium时,没有定义该名称,只有它的源文件调用Roboto-Medium.ttf,不能直接调用它。
尝试为每种字体类型定义一个名称,如下所示:
- family: Roboto-Regular
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- family: Roboto-Meduim
fonts:
- asset: assets/fonts/Roboto-Medium.ttf
- family: Roboto-Bold
fonts:
- asset: assets/fonts/Roboto-Bold.ttf
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
fontFamily: 'Roboto',
textTheme: TextTheme(
headline: TextStyle(fontWeight: FontWeight.normal, fontSize: 20.0),
button: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, letterSpacing: 1.25),
)),
之后,您可以通过“Roboto Medium”在代码中调用它们。这里是flutter dev网站()所说的
文件夹路径:
awesome_app/
fonts/
Raleway-Regular.ttf
Raleway-Italic.ttf
RobotoMono-Regular.ttf
RobotoMono-Bold.ttf
宣布:
flutter:
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
- asset: fonts/Raleway-Italic.ttf
style: italic
- family: RobotoMono
fonts:
- asset: fonts/RobotoMono-Regular.ttf
- asset: fonts/RobotoMono-Bold.ttf
weight: 700
在整个应用程序中使用:
MaterialApp(
title: 'Custom Fonts',
// Set Raleway as the default app font.
theme: ThemeData(fontFamily: 'Raleway'),
home: MyHomePage(),
);
在特定小部件中使用:
Text(
'Roboto Mono sample',
style: TextStyle(fontFamily: 'RobotoMono'),
);
试着替换
- asset: assets/fonts/Roboto-Regular.ttf
与
您对自定义字体的声明不清楚
更改此项:
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- asset: assets/fonts/Roboto-Medium.ttf
- asset: assets/fonts/Roboto-Bold.ttf
变成这样:
fonts:
- family: Roboto
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- asset: assets/fonts/Roboto-Medium.ttf
weight: 600
- asset: assets/fonts/Roboto-Bold.ttf
weight: 700
这样称呼他们:
- family: Roboto-Regular
fonts:
- asset: assets/fonts/Roboto-Regular.ttf
- family: Roboto-Meduim
fonts:
- asset: assets/fonts/Roboto-Medium.ttf
- family: Roboto-Bold
fonts:
- asset: assets/fonts/Roboto-Bold.ttf
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.blue,
fontFamily: 'Roboto',
textTheme: TextTheme(
headline: TextStyle(fontWeight: FontWeight.normal, fontSize: 20.0),
button: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, letterSpacing: 1.25),
)),
当无法在项目中添加字体时,存在两个主要问题:
检查yaml文件中的缩进。这是至关重要的,因为在yaml文件中空间是有意义的
从一开始就重新加载模拟器。当我第一次学习颤振时,我坚持了两个小时。重新启动它,库将在yaml文件中添加字体。包装和包装也一样
将您的ROBOTO字体移到字体文件夹中,然后尝试是否工作?@AR:它们已经在。我还尝试在资产
文件夹外创建字体
文件夹,但luckI仍然没有尝试过,但如何使用“Roboto常规”、“Roboto中等”和“Roboto粗体”?它只是有一个机器人,你可以用它的字体风格和重量。