How to make widget without '?' and 'required' - flutter

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 '?'
example of what i want

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);
}

If you want to preserve the non-nullable state of your class, 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);
}

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

Related

Flutter: I am missing the image parameter

I have a class where my object are, I tried calling it out in another file where I want to make use of the properties, It is telling me required named parameter 'image' must be provided.
class _homeState extends State<home> {
var _currentIndex = 0;
final screens = [
HomePage(),
Categories(),
const NearYou(image: '', title: '', subtitle: '',),
Cart(),
Profile()
];
I really don't know what to put in those parameters above
Also, It is saying: Found this candidate, but the arguments don't match.
Will I also call my class in that file?
class NearYou extends StatefulWidget {
final String image;
final String title;
final String subtitle;
const NearYou(
{Key? key,
required this.image,
required this.title,
required this.subtitle})
: super(key: key);

Flutter invalid constant value (on excercise from flutter apprentice book)

When creating this stateless widget I try to assign a TextDecoration value to a widget attribute based on the value of a boolean attribute from the object being passed to it on creation.
textDecoration = item.isComplete ? TextDecoration.lineThrough : TextDecoration.none,
On this row it marks item.isComplete as an error, saying invalid constant value.
Is it because isComplete in the class is defined with the late keyword and therefore it could be null? I checked all the possible causes for which the invalid constant value error migh arise and I still haven't made any sort of dent into it.
The widget:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import '../models/grocery_item.dart';
class GroceryTile extends StatelessWidget {
final GroceryItem item;
final Function(bool?)? onComplete;
final TextDecoration textDecoration;
const GroceryTile({
Key? key,
required this.item,
this.onComplete,
}) : textDecoration = item.isComplete // Error here
? TextDecoration.lineThrough
: TextDecoration.none,
super(key: key);
#override
Widget build(BuildContext context) {
return Container();
}
}
The class:
import 'package:flutter/painting.dart';
enum Importance { low, medium, high }
class GroceryItem {
final String id;
final String name;
final Importance importance;
final Color color;
final int quantity;
final DateTime date;
late bool isComplete;
GroceryItem({
required this.id,
required this.name,
required this.importance,
required this.color,
required this.quantity,
required this.date,
this.isComplete = false,
});
GroceryItem copyWith({
String? id,
String? name,
Importance? importance,
Color? color,
int? quantity,
DateTime? date,
bool? isComplete,
}) {
return GroceryItem(
id: id ?? this.id,
name: name ?? this.name,
importance: importance ?? this.importance,
color: color ?? this.color,
quantity: quantity ?? this.quantity,
date: date ?? this.date,
isComplete: isComplete ?? this.isComplete);
}
}
The const constructor cannot have a body and if you want to initialize some value in the constructor body you need to remove the const keyword
//...
GroceryTile({ // <- Remove `const`here
Key? key,
required this.item,
this.onComplete,
}) : textDecoration = item.isComplete
? TextDecoration.lineThrough
: TextDecoration.none,
super(key: key);
//...
Try this:
class GroceryTile extends StatelessWidget {
final GroceryItem item;
final Function(bool?)? onComplete;
final TextDecoration textDecoration;
GroceryTile({
Key? key,
required this.item,
this.onComplete,
}) : textDecoration = item.isComplete
? TextDecoration.lineThrough
: TextDecoration.none,
super(key: key);
#override
Widget build(BuildContext context) {
return Container();
}
}

How to make a widget require one of two properties?

So I have a widget that can accept a color or a gradient property :
class OuterWheel extends StatelessWidget {
final double outerRadius;
final double innerRadius;
final TextStyle textStyle;
final double percentage;
final int decimals;
final Color? color;
final List<Color>? gradient;
const OuterWheel({
Key? key,
required this.outerRadius,
required this.innerRadius,
required this.textStyle,
required this.percentage,
required this.decimals,
this.color,
this.gradient,
}) : super(key: key);
I would like to make it like in TypeScript where you could have a union type like Color|List<Color> , so I could only use one property, but apparently we don't have that in Dart.
So how can I make so that you have to choose one of these two fields?
What about mutual exclusive operator?
const OuterWheel({
Key? key,
required this.outerRadius,
required this.innerRadius,
required this.textStyle,
required this.percentage,
required this.decimals,
this.color,
this.gradient,
}) : assert((color == null) ^ (gradient == null), 'color and gradient are mutually exclusive'), super(key: key);
This way you ensure that only one of them, but not both, is not null.

how to make a non required parameter in dart

I have different required parameters in flutter and I want to make one of them non required (colour). If I leave it without the required key, it give an error. How can I make it? Here is my code:
class DiscoverCardTemplate extends StatelessWidget {
DiscoverCardTemplate({
required this.textTop,
required this.textMiddle,
required this.textBottom,
required this.coverImage,
required this.onTap,
this.colour,
});
final String textTop, textMiddle, textBottom, coverImage;
final Function onTap;
final Color colour;
make it nullable means colour will be either null or colur_value.
class DiscoverCardTemplate extends StatelessWidget {
DiscoverCardTemplate({
required this.textTop,
required this.textMiddle,
required this.textBottom,
required this.coverImage,
required this.onTap,
this.colour,
});
final String textTop, textMiddle, textBottom, coverImage;
final Function onTap;
final Color? colour;
or
add a default value for colour (if you didn't pass colour then it uses default value).
class DiscoverCardTemplate extends StatelessWidget {
DiscoverCardTemplate({
required this.textTop,
required this.textMiddle,
required this.textBottom,
required this.coverImage,
required this.onTap,
this.colour = colour_value,
});
final String textTop, textMiddle, textBottom, coverImage;
final Function onTap;
final Color colour;
Either make the
final Color colour;
as nullable:
final Color? colour;
Or, assign a default color in the constructor
this.colour = Colors.black,

how to create a constructor in flutter with null safety

This got a bit tricky for me. since we can no longer pass null values in contructor I had this
class LeadingButton extends StatelessWidget {
final IconData icon;
final Color color;
final Color iconColor;
final double iconSize;
final double size;
final Color backGroundColor;
final VoidCallback onPressed;
final double padding;
final EdgeInsets margin;
final double rotate;
LeadingButton({
#required this.icon,
this.color = Colors.transparent,
#required this.onPressed,
this.size = 20,
this.backGroundColor = Colors.transparent,
this.iconSize = 20,
this.iconColor = darkColor,
this.rotate = 0,
this.padding = 0,
this.margin = const EdgeInsets.all(10)});
...
I am getting an error on icon and onpressed function. although the
documentation is saying
The parameter 'onPressed' & 'icon' can't have a value
of 'null' because of its type, but the implicit default value is
'null'. Try adding either an explicit non-'null' default value or the
'required' modifier.
I guess I am missing some thing, kindly mind to share
Not sure, but it could be that this is due to the recent change of the keyword.
Did you try the required keyword instead?
https://dart.dev/null-safety/faq#how-does-required-compare-to-the-new-required-keyword
I think that LeadingButton's onPressed type needs to be changed to VoidCallback? so it can accept null values.