I have an error: A value of type 'Color' can't be assigned to a variable of type 'MaterialColor' in line with code: selectedValue = newValue!. Please help me solve it. I am attaching the screen code:
class _ColorPickerWidgetState extends State<ColorPickerWidget> {
List<DropdownMenuItem<Color>> get dropdownItems{
List<DropdownMenuItem<Color>> colorItems = [
const DropdownMenuItem(
value: Colors.yellow,
child: Text('Yellow'),
),
const DropdownMenuItem(
value: Colors.red,
child: Text('Red'),
),
];
return colorItems;
}
// default value of select
MaterialColor selectedValue = Colors.yellow;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Choose a color'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownButton<Color>(
value: selectedValue,
items: dropdownItems,
onChanged: (Color? newValue){
setState(() {
selectedValue = newValue!;
});
},
),
ElevatedButton(onPressed: () => _changeColor(selectedValue),
child: const Text('Next'),
),
],
),
),
);
}
}
CodePudding user response:
Change:
MaterialColor selectedValue = Colors.yellow;
To:
Color selectedValue = Colors.yellow;
While MaterialColor
extends ColorSwatch
which extends Color
, you cannot assign a Color
(which might not be a MaterialColor
) to MaterialColor
CodePudding user response:
There are 2 two solutions
- Maintain only
MaterialColor
type in all places
class ColorPickerWidget extends StatefulWidget {
const ColorPickerWidget({Key? key}) : super(key: key);
@override
State<ColorPickerWidget> createState() => _ColorPickerWidgetState();
}
class _ColorPickerWidgetState extends State<ColorPickerWidget> {
List<DropdownMenuItem<MaterialColor>> get dropdownItems {
List<DropdownMenuItem<MaterialColor>> colorItems = [
const DropdownMenuItem(
value: Colors.yellow,
child: Text('Yellow'),
),
const DropdownMenuItem(
value: Colors.red,
child: Text('Red'),
),
];
return colorItems;
}
// default value of select
MaterialColor selectedValue = Colors.yellow;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Choose a color'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownButton<MaterialColor>(
value: selectedValue,
items: dropdownItems,
onChanged: (MaterialColor? newValue) {
setState(() {
selectedValue = newValue!;
});
},
),
ElevatedButton(
onPressed: () => _changeColor(selectedValue),
child: const Text('Next'),
),
],
),
),
);
}
}
- Maintain only the
Color
type in all places
class ColorPickerWidget extends StatefulWidget {
const ColorPickerWidget({Key? key}) : super(key: key);
@override
State<ColorPickerWidget> createState() => _ColorPickerWidgetState();
}
class _ColorPickerWidgetState extends State<ColorPickerWidget> {
List<DropdownMenuItem<Color>> get dropdownItems {
List<DropdownMenuItem<Color>> colorItems = [
const DropdownMenuItem(
value: Colors.yellow,
child: Text('Yellow'),
),
const DropdownMenuItem(
value: Colors.red,
child: Text('Red'),
),
];
return colorItems;
}
// default value of select
Color selectedValue = Colors.yellow;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Choose a color'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropdownButton<Color>(
value: selectedValue,
items: dropdownItems,
onChanged: (Color? newValue) {
setState(() {
selectedValue = newValue!;
});
},
),
ElevatedButton(
onPressed: () => _changeColor(selectedValue),
child: const Text('Next'),
),
],
),
),
);
}
}