Flutter - 自定义 ThemeData 无法正常工作
Flutter - customised ThemeData not working properly
我为我的应用创建了以下主题:
class MyPortfolio extends StatelessWidget {
@override
Widget build(BuildContext context) {
final myPortfolio = "my portfolio";
return MaterialApp(
title: myPortfolio,
theme: ThemeData(
primaryColor: Color.fromRGBO(10, 16, 19, 100),
// accentColor:,
fontFamily: 'Futura',
textTheme: TextTheme(
headline5: TextStyle(fontSize: 13),
),
home: MyAppBar(),
);}}
然后我将它应用到我的应用程序如下:
class _MyAppBarState extends State<MyAppBar> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
title: Row(
children: <Widget>[
Logo(),
// action button
FlatButton(
onPressed: () {
_select(choices[0]);
},
child: Text(
'Portfolio',
style: Theme.of(context).textTheme.headline5,
)),...
我的pubspec.yaml如下:
flutter:
fonts:
- family: Baskerville
fonts:
- asset: fonts/Baskerville.ttc
- family: Futura
fonts:
- asset: fonts/Futura.ttc
- family: Tinos
fonts:
- asset: fonts/Tinos-Italic.ttf
style: italic
- asset: fonts/Tinos-BoldItalic.ttf
weight: 700
uses-material-design: true
但是,当我尝试访问此处的字体时,它显示的是默认的 headline5 字体样式,而不是我自定义的字体样式。
请问大家有什么建议吗?
您正在使用默认参数的 _MyAppBarState 中使用 MaterialApp 小部件的新实例。
class MyPortfolio extends StatelessWidget {
@override
Widget build(BuildContext context) {
final myPortfolio = "my portfolio";
return MaterialApp(
debugShowCheckedModeBanner: false,
title: myPortfolio,
theme: ThemeData(
primaryColor: Color.fromRGBO(10, 16, 19, 100),
// accentColor:,
fontFamily: 'Futura',
textTheme: TextTheme(
headline5: TextStyle(fontSize: 13),
),
home: MyAppBar(),
);}}
class _MyAppBarState extends State<MyAppBar> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
title: Row(
children: <Widget>[
Logo(),
// action button
FlatButton(
onPressed: () {
_select(choices[0]);
},
child: Text(
'Portfolio',
style: Theme.of(context).textTheme.headline5,
)),...
我的问题已经解决,因为我一直在应用程序的其他部分愚蠢地使用 MaterialApps,因此 ThemeData 无法正常运行。
我为我的应用创建了以下主题:
class MyPortfolio extends StatelessWidget {
@override
Widget build(BuildContext context) {
final myPortfolio = "my portfolio";
return MaterialApp(
title: myPortfolio,
theme: ThemeData(
primaryColor: Color.fromRGBO(10, 16, 19, 100),
// accentColor:,
fontFamily: 'Futura',
textTheme: TextTheme(
headline5: TextStyle(fontSize: 13),
),
home: MyAppBar(),
);}}
然后我将它应用到我的应用程序如下:
class _MyAppBarState extends State<MyAppBar> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
title: Row(
children: <Widget>[
Logo(),
// action button
FlatButton(
onPressed: () {
_select(choices[0]);
},
child: Text(
'Portfolio',
style: Theme.of(context).textTheme.headline5,
)),...
我的pubspec.yaml如下:
flutter:
fonts:
- family: Baskerville
fonts:
- asset: fonts/Baskerville.ttc
- family: Futura
fonts:
- asset: fonts/Futura.ttc
- family: Tinos
fonts:
- asset: fonts/Tinos-Italic.ttf
style: italic
- asset: fonts/Tinos-BoldItalic.ttf
weight: 700
uses-material-design: true
但是,当我尝试访问此处的字体时,它显示的是默认的 headline5 字体样式,而不是我自定义的字体样式。 请问大家有什么建议吗?
您正在使用默认参数的 _MyAppBarState 中使用 MaterialApp 小部件的新实例。
class MyPortfolio extends StatelessWidget {
@override
Widget build(BuildContext context) {
final myPortfolio = "my portfolio";
return MaterialApp(
debugShowCheckedModeBanner: false,
title: myPortfolio,
theme: ThemeData(
primaryColor: Color.fromRGBO(10, 16, 19, 100),
// accentColor:,
fontFamily: 'Futura',
textTheme: TextTheme(
headline5: TextStyle(fontSize: 13),
),
home: MyAppBar(),
);}}
class _MyAppBarState extends State<MyAppBar> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0.0,
title: Row(
children: <Widget>[
Logo(),
// action button
FlatButton(
onPressed: () {
_select(choices[0]);
},
child: Text(
'Portfolio',
style: Theme.of(context).textTheme.headline5,
)),...
我的问题已经解决,因为我一直在应用程序的其他部分愚蠢地使用 MaterialApps,因此 ThemeData 无法正常运行。