以下代码中,构造函数中type的赋值失效,在initColor中print出来为null。为什么?
///图标按钮
///可配置文本、图标、背景色、文本色
class MIconButton extends StatelessWidget{
final Function? onTap;
final String text;
final Icon icon;
Color bgColor;
Color textColor;
final String? type;
MIconButton({Key? key, this.onTap, required this.text,required this.icon,this.bgColor = Colors.white,this.textColor = Colors.black, this.type}) : super(key: key){
this.initColor();
}
void initColor(){
print("init color");
print(this.type);
// if(this.type != null){
// switch(this.type){
// case "primary":
// this.bgColor = ColorConst.primaryColor;
// this.textColor = Colors.white;
// break;
// default:
// this.bgColor = Colors.white;
// this.textColor = Colors.black;
// }
// }
}
...
使用地方
MIconButton(text: "编辑资料",icon: Icon(Icons.edit_outlined), type: "primary",)
是我的问题,我重新flutter run就可以了