我尝试为我的AppBar使用自定义字体,但他不会改变。我尝试使用两种不同的字体,即RobotoMono和DancingScript,但没有任何变化,该应用程序不会更改字体。我也尝试从虚拟电话上取消安装该应用程序,也创建了另一个虚拟设备,但是什么也没有。那是我的main.dart:
import 'package:flutter/material.dart';
import 'background_image_task-9.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Blumax',
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: 'Dancing',
primarySwatch: myColour
),
home: BackgroundImage(
),
);
}
}
const MaterialColor myColour = const MaterialColor(
0xFF0009FF,
const <int, Color>{
50: const Color(0xFF0009FF),
100: const Color(0xFF0009FF),
200: const Color(0xFF0009FF),
300: const Color(0xFF0009FF),
400: const Color(0xFF0009FF),
500: const Color(0xFF0009FF),
600: const Color(0xFF0009FF),
700: const Color(0xFF0009FF),
800: const Color(0xFF0009FF),
900: const Color(0xFF0009FF),
},
);
这是我使用自定义字体background_image_task-9.dart的地方:
import 'package:flutter/material.dart';
class BackgroundImage extends StatelessWidget{
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text('Blumax', style: TextStyle(
fontWeight: FontWeight.w500,
fontFamily: 'RobotoMono',
fontSize: 40
),),
centerTitle: true,
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage("assets/blumax.jpg"), fit: BoxFit.cover),
),
),
);
}
}
那是我的pubspec.yaml:
name: iphone_prj
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
english_words: ^3.1.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
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:
assets:
- assets/
# 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
fonts:
- family: RobotoMono
fonts:
- asset: assets/fonts/RobotoMono-Bold.ttf
- family: DancingScript
fonts:
- asset: assets/fonts/DancingScript-Bold.ttf
weight: 300
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - 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: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - 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
答案 0 :(得分:0)
只需按照pubspec.yaml文件在主ThemeData中正确添加字体家族名称
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Blumax',
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: 'DancingScript',
primarySwatch: myColour
),
home: BackgroundImage(
),
);
}
}
您遇到的问题是您的字体系列名称为 DancingScript ,并且在ThemeData中将其作为 Dancing 提供。因此它不会影响您的应用字体。
此外,在BackgroundImage类中添加了 RobotoMono 字体。但是,您添加的“ fontWeight:FontWeight.w500” 与pubspec.yaml不匹配,因为您添加了 RobotoMono-Bold 字体。
因此,通过匹配字体名称和字体样式将根据您的要求影响您的应用字体。
答案 1 :(得分:0)
在pubspec.yaml中声明字体时使用资产前缀