I’m encountering an issue with the Flutter app where the color property of MyContainer doesn’t seem to work as expected in the MainScreen. Specifically, I have set the color parameter of MyContainer to different colors, but it doesn’t affect the background color of the container.What am i doing wrong?Thanks in advance
Expanded(
child: Row(
children: [
Expanded(
child: MyContainer(
color: Colors.white,
margin: EdgeInsets.all(10),
child: GenderWidget(
onPressed: () {
gender = "Women";
},
gender: "Women"),
),
),
Expanded(
child: MyContainer(
color: Colors.green,
child: GenderWidget(
onPressed: () {
gender="Man";
},
gender: "Man",
))),
],
),
),
Widget class
class MyContainer extends StatelessWidget {
final Widget child;
final EdgeInsetsGeometry margin;
final Color color;
MyContainer({required this.child, this.margin = const EdgeInsets.all(12),required this.color}); // Default margin değeri verildi.
@override
Widget build(BuildContext context) {
return Container(
margin: margin, // Margin değeri instance variable olarak kullanılıyor.
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(15),
),
),
child: child,
);
}
}
class GenderWidget extends StatelessWidget {
final String gender;
final Function onPressed;
GenderWidget( {required this.gender, required this.onPressed});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPressed(),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.person),
const SizedBox(height: 10),
Text(gender,style: textStyle,),
],
),
);
}
}
2
Answers
It looks like the issue might be with the hardcoded color in your
MyContainer
widget’sBoxDecoration
. You’ve setColors.white
as the color, which means it will always be white regardless of the color you provide when creating theMyContainer
.You should use the
color
parameter that you pass toMyContainer
in theBoxDecoration
. Here’s the correctedMyContainer
widget:With this change, the
color
you pass toMyContainer
will be used as the background color. Now, when you set the color inMyContainer
in yourRow
, it should work as expected. For example:Can you try to remove color from Container as you are using decoration property in Container?Meaning remove color from Container and set your desired color in decoration .And let me know if it works or not.