[英]why icon widget wont accept null?
i created a button widget and i want the icon of my button to be optional.我创建了一个按钮小部件,我希望我的按钮图标是可选的。 so when i wanna write condition for it,it wont accept it.
所以当我想为它写条件时,它不会接受它。 here is my code:
这是我的代码:
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), ), ), ); ); }
and this is the error im getting这是我得到的错误
The argument type 'Icon?'参数类型“图标?” can't be assigned to the parameter type 'Widget'.
无法分配给参数类型“Widget”。
I'd suggest splitting it up to two different widgets then.我建议将它分成两个不同的小部件。 Use a normal
ElevatedButton
when there's showIcon
false, like:当
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),
),
),
),
);
Note that you will get an exception when you have showIcon
as true
but buttonIcons
as null
.请注意,当
showIcon
为true
但buttonIcons
为null
时,您将得到一个异常。 Maybe it's better to leave out the showIcon
and just check on buttonIcons
being null
or not to decide between the two.也许最好省略
showIcon
并只检查buttonIcons
是null
还是不在两者之间做出决定。 So like:所以像:
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),
),
),
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.