如何在 Dart 中传递 String 作为参数?

问题描述 投票:0回答:1

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"),
              )
            ],
          )),

如何修复此错误?

flutter dart
1个回答
0
投票

您必须删除列的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)
      ],
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.