flutter 构造函数中参数未进行赋值,为什么?

以下代码中,构造函数中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",)
阅读 1.7k
1 个回答

是我的问题,我重新flutter run就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题