第
Text(textToDisplay)
行出现问题
我收到错误
Arguments of a constant creation must be constant expressions.
class IconText extends StatelessWidget {
const IconText({super.key, required this.icon, required this.textToDisplay});
final IconData icon;
final String textToDisplay;
@override
Widget build(BuildContext context) {
return const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.male,
size: 80,
color: Colors.grey,
),
SizedBox(height: 15),
Text(textToDisplay)
],
);
}
}
我像这样使用这个小部件
const Expanded(
child: Row(
children: [
NewWidget(
color: widgetColor,
cardChild: IconText(icon: Icons.male, textToDisplay: "Male"),
),
NewWidget(
color: widgetColor,
cardChild: IconText(icon: Icons.male, textToDisplay: "Female"),
)
],
)),
如何修复此错误?
您必须删除列的const
class IconText extends StatelessWidget {
const IconText({super.key, required this.icon, required this.textToDisplay});
final IconData icon;
final String textToDisplay;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.male,
size: 80,
color: Colors.grey,
),
const SizedBox(height: 15),
Text(textToDisplay)
],
);
}
}