我创建了一个名为
getRegularStyle
的函数,它返回 TextStyle
。当我想用它来 change appBar
titleTextStyle
时,它会给出以下错误:
The argument type 'TextStyle' can't be assigned to the parameter type 'TextStyle?'.
这是我的功能:
TextStyle _getTextStyle(double fontSize, String fontFamily, FontWeight fontWeight, Color color){
return TextStyle(fontSize: fontSize, fontFamily: fontFamily, fontWeight: fontWeight, color: color);
}
//Regular Style
TextStyle getRegularStyle({double fontSize = 12, required Color color}){
return _getTextStyle(fontSize, FontConstants.fontFamily, FontWeightManager.regular, color);
}
这里是我调用函数来更改 appBar 的 titleTextStyle 的地方:
appBarTheme: AppBarTheme(
centerTitle: true,
color: ColorManager.primary,
elevation: AppSize.s4,
shadowColor: ColorManager.primaryWithOpacity70,
**titleTextStyle: getRegularStyle(color: ColorManager.white, fontSize: FontSize.s16),**
),
粗体代码行提供了错误。
我什至使函数可以为空来解决问题,但它提供了另一个错误,在我的情况下,我认为这不能解决问题。
最有可能的是,您的
getRegularStyle
和 AppBarTheme
位于不同的文件中,并且它们导入了不同的包。
例如
import 'dart:ui';
//Regular Style
TextStyle getRegularStyle({double fontSize = 12, required Color color}){
return _getTextStyle(fontSize, FontConstants.fontFamily, FontWeightManager.regular, color);
}
和
import 'package:flutter/material.dart';
appBarTheme: AppBarTheme(
centerTitle: true,
color: ColorManager.primary,
elevation: AppSize.s4,
shadowColor: ColorManager.primaryWithOpacity70,
**titleTextStyle: getRegularStyle(color: ColorManager.white, fontSize: FontSize.s16),**
),
一个导入
dart.ui
,而另一个导入 flutter/material.dart
(或其他方式)。这将导致参数类型无法分配错误。检查您的导入或粘贴完整的代码文件。