Specify optional keyword argument in constructor flutter - flutter

final Widget child;
final Colors color;
const FlexibleContainer({required this.child, Colors? this.color, Key? key}) : super(key: key);
I get error in this.color - parameter type Colors is incompatible with field type Colors

final Widget child;
final Color? color;
const NotificationHandler({required this.child, this.color, Key? key});

Related

Giving color from Colors class as a parameter in Flutter

class colorSymbol extends StatelessWidget {
final Colors color;
colorSymbol(Colors this.color, {Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Icon(
Icons.accessibility,
color: ,
size: 64.0,
);
}
}
The code simply return a accessibility icon from Icons and I want this widget to change its color which is given by user. I created a variable from Colors class called color but I cannot move on. Thanks in advance
You like to have Color instead of Colors.
class colorSymbol extends StatelessWidget {
final Color color;
const colorSymbol(this.color, {super.key}) ;
You should change your parameter type from Colors to Color :
import 'package:flutter/material.dart';
class colorSymbol extends StatelessWidget {
final Color color;
colorSymbol(Color this.color, {Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Icon(
Icons.accessibility,
color: color,
size: 64.0,
);
}
}

when I try to use another class the problem show me that error: The argument type 'Null' can't be assigned to the parameter type 'Key'

class ColoredCircle extends StatelessWidget {
ColoredCircle({required Key key, required this.color}) : super(key: key);
final Color color;
final double width = 50;
#override
Widget build(BuildContext context) {
return Container(
width: width ,
height: width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.red,
)
);
}
Use case
ColoredCircle(color: Colors.red, key: null),
You can remove required from key on ColoredCircle and make it nullable
class ColoredCircle extends StatelessWidget {
const ColoredCircle({
Key? key,
required this.color,
}) : super(key: key);
Also you can do, by default StatelessWidget accept null data.
class ColoredCircle extends StatelessWidget {
const ColoredCircle({
super.key,
required this.color,
});
More about StatelessWidget

I want to set a value including BorderRadius.circular as the default value of the argument of the constructor

I have defined a custom widget for ElevatedButton, and I want to make the shape field optional as shown below and use the initial value when no value is passed, but when I write it as above, the following error occurs. ..
class GradientElevatedButton extends StatelessWidget {
GradientElevatedButton({
Key? key,
required this.onPressed,
required this.child,
required this.colors,
this.shape = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),//←here
),
}) : super(key: key);
final Widget child;
final void Function() onPressed;
final List<Color> colors;
final OutlinedBorder shape;
//error
The default value of an optional parameter must be constant.
Maybe it means that the BorderRadius.circular constructor is not const,
How should I write if I want to use the following OutlinedBorder as the default value?
RoundedRectangleBorder (
borderRadius: BorderRadius.circular (30),
)
BorderRadius.circular is not a const constructor, so you cannot use it as a default value of a const constructor.
However, if you look at the implementation:
/// Creates a border radius where all radii are [Radius.circular(radius)].
BorderRadius.circular(double radius) : this.all(
Radius.circular(radius),
);
It uses BorderRadius.all and Radius.circular and both of them are const constructor.
So you can replace
BorderRadius.circular(shape)
with
BorderRadius.all(Radius.circular(30))
Here is your updated code sample:
```class GradientElevatedButton extends StatelessWidget {
GradientElevatedButton({
Key? key,
required this.onPressed,
required this.child,
required this.colors,
this.shape = const RoundedRectangleBorder( // <- Don't forget the const keyword
borderRadius: BorderRadius.all(Radius.circular(30)),//←here
),
}) : super(key: key);
final Widget child;
final void Function() onPressed;
final List<Color> colors;
final OutlinedBorder shape;
I think this is not what you want, but sometime it helps others.
import 'package:flutter/material.dart';
class GradientElevatedbutton extends StatelessWidget {
const GradientElevatedbutton({
Key? key,
required this.onPressed,
required this.child,
required this.colors,
this.shape = 30,
}) : super(key: key);
final Widget child;
final void Function() onPressed;
final List<Color> colors;
final double shape;
#override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(shape),
),
),
child: Text('click'),
);
}
}
This part of your code:
this.shape = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
must be change to:
this.shape = const RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),

how to dismiss request from contractor in flutter when using final

class Reusable extends StatelessWidget {
const Reusable({
Key? key,
required this.color,
this.cardchild,
}) : super(key: key);
final Color color;
final Widget cardchild;
use:
final Color? color;
final Widget? cardchild;

null safety on widget parameter

Before null-safety, I can pass widget, if check it is null, will show other widget. On null-safety environment, is there a way if I didn't pass widget image or note?
class Message extends StatelessWidget {
const Message({
Key key,
this.image,
this.title = "",
this.subtitle = "",
this.note,
this.onTap
}) : super(key: key);
final Widget image;
final String title;
final String subtitle;
final Widget note;
final Function onTap
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Column(
children:[
image ?? Container(),
if(note != null) note
]
)
);
}
};
Use ? if the child can be null.
class FooWidget extends StatelessWidget {
final Widget? child; // <-- Nullable, use '?'
FooWidget({
this.child,
});
// ...
}
Use required if the child can't be null.
class FooWidget extends StatelessWidget {
final Widget child; // <-- Non-nullable
FooWidget({
required this.child, // <-- Use 'required'
});
// ...
}
You can declare your image and note as nullable types. If you need to make any of the field mandatory, you can mark it as required.
class Message extends StatelessWidget {
const Message({
Key? key,
this.image,
this.title = "",
this.subtitle = "",
this.note,
required this.onTap,
}) : super(key: key);
final Widget? image;
final String title;
final String subtitle;
final Widget? note;
final VoidCallback onTap;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Column(
children:[
image ?? Container(),
note ?? Container(),
]
)
);
}
}