Home > Mobile >  How to make widget without '?' and 'required'
How to make widget without '?' and 'required'

Time:04-05

so im working on company project and realize the widget doesn't need Key? and required costructor, and when i try to make it like that it occur red underline.

my project i want to remove the required and '?'

enter image description here

example of what i want

enter image description here

CodePudding user response:

you have probably defined height as final and that's why it is asking it.

define height as

double? height

CodePudding user response:

It should view like that:

class CustomButton extends StatelessWidget {
  final double? height;
  final double width;
  final String title;
  final double margin;
  final Function() onPressed;
  const CustomButton ({
  Key? key,
  this.height,
  required this.width,
  required this.title,
  required this.margin,
  required this.onPressed,
  }) : super (key: key);
}

CodePudding user response:

You have to pass an initial value for the CustomButton height property.

class CustomButton extends StatelessWidget {
  final double height;
  final double width;
  final String title;
  final double margin;
  final Function() onPressed;
  const CustomButton ({
  Key? key,
  this.height = 100,
  required this.width,
  required this.title,
  required this.margin,
  required this.onPressed,
  }) : super (key: key);
}
  • Related