flutter 为什么图标小部件不接受空?

j9per5c4  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(140)

我创建了一个按钮小部件,我想我的按钮图标是可选的.所以当我想写条件为它,它不会接受它.这里是我的代码:

import 'package:flutter/material.dart';

Widget CustomButtom({
  String? title,
  EdgeInsetsGeometry? paddin,
  EdgeInsetsGeometry? margin,
  double? width,
  double? height,
  Color? backgroundColor,
  dynamic? onPress,
  Color? fontColor,
  double? fontsize,
  double borderRaidius = 10,
  bool showIcon = true,
  Icon? buttonIcons,
}) {
  return Container(
    width: width,
    height: height,
    child: Directionality(
      textDirection: TextDirection.rtl,
      child: ElevatedButton.icon(
        style: ElevatedButton.styleFrom(
            backgroundColor: backgroundColor,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(borderRaidius),
            )),
        onPressed: onPress,
        icon: showIcon? buttonIcons!:null,
        label: Text(
          '$title',
          style: TextStyle(fontSize: 20),
        ),
      ),
    ),
  );
}

这就是我得到的错误

参数类型“Icon?”不能赋给参数类型“Widget”。

cigdeys3

cigdeys31#

我建议将其拆分为两个不同的小部件,当showIcon为false时使用普通的ElevatedButton,例如:

return Container(
  width: width,
  height: height,
  child: Directionality(
    textDirection: TextDirection.rtl,
    child: showIcon
        ? ElevatedButton.icon(
            style: ElevatedButton.styleFrom(
                backgroundColor: backgroundColor,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(borderRaidius),
                )),
            onPressed: onPress,
            icon: buttonIcons!,
            label: Text(
              '$title',
              style: TextStyle(fontSize: 20),
            ),
          )
        : ElevatedButton(
            style: ElevatedButton.styleFrom(
                backgroundColor: backgroundColor,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(borderRaidius),
                )),
            onPressed: onPress,
            child: Text(
              '$title',
              style: TextStyle(fontSize: 20),
            ),
          ),
  ),
);

请注意,当您将showIcon作为true,而将buttonIcons作为null时,您将得到一个异常。也许最好忽略showIcon,只检查buttonIcons是否为null,或者不在两者之间做出决定。

return Container(
  width: width,
  height: height,
  child: Directionality(
    textDirection: TextDirection.rtl,
    child: buttonIcons != null
        ? ElevatedButton.icon(
            style: ElevatedButton.styleFrom(
                backgroundColor: backgroundColor,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(borderRaidius),
                )),
            onPressed: onPress,
            icon: buttonIcons,
            label: Text(
              '$title',
              style: TextStyle(fontSize: 20),
            ),
          )
        : ElevatedButton(
            style: ElevatedButton.styleFrom(
                backgroundColor: backgroundColor,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(borderRaidius),
                )),
            onPressed: onPress,
            child: Text(
              '$title',
              style: TextStyle(fontSize: 20),
            ),
          ),
  ),
);

相关问题