I am trying to integrate a child class (implementing DropdownButton with four values: 2, 4, 6, and 8) to a parent class which should show some content depending on the value chosen.
If user clicks on one of the drop down values ie: 2, a widget on the main class should display a container with blue colour. If user click on value 4 it should display a container with red colour and so on.
My thoughts went along these lines, implementing a simple method which is reading chosen value, calls the appropriate class (ClassTwo, ClassThree...) passing its content to the parent class but I am not sure how to do so. My renderWidget() function remains unused and fuller suggesting removing it.
Can anyone please help?
child class (DropdownMenuButton)
class VorschlageDropdownMenu extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<VorschlageDropdownMenu> {
List<ListItem> _dropdownItems = [
ListItem(1, "2"),
ListItem(2, "4"),
ListItem(3, "6"),
ListItem(4, "8"),
];
List<DropdownMenuItem<ListItem>> _dropdownMenuItems;
ListItem _selectedItem;
void initState() {
super.initState();
_dropdownMenuItems = buildDropDownMenuItems(_dropdownItems);
_selectedItem = _dropdownMenuItems[0].value;
}
List<DropdownMenuItem<ListItem>> buildDropDownMenuItems(List listItems) {
List<DropdownMenuItem<ListItem>> items = List();
for (ListItem listItem in listItems) {
items.add(
DropdownMenuItem(
child: Text(listItem.name),
value: listItem,
),
);
}
return items;
}
@override
Widget build(BuildContext context) {
return SizedBox(
child: Container(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: kWidgetBacgroundColor,
border: Border.all()),
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: _selectedItem,
items: _dropdownMenuItems,
onChanged: (value) {
setState(() {
_selectedItem = value;
renderWidget() {
if (value == "2")
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => MenuForZwei(),
),
);
else if (value == "4")
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => MenuForVier(),
),
);
else if (value == "6")
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => MenuForSechs(),
),
);
else if (value == "8")
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => MenuForAcht(),
),
);
}
});
}),
),
),
);
}
}
class ListItem {
int value;
String name;
ListItem(this.value, this.name);
}
parental class which should display value chosen
class VorschlageZutaten extends StatelessWidget {
const VorschlageZutaten({
Key key,
this.renderWidget,
}) : super(key: key);
final Function renderWidget;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 50, top: 20),
),
Text(
"Für ",
style: TextStyle(
color: kPrimaryHeaderColor.withOpacity(0.6),
fontSize: (20.0),
fontWeight: FontWeight.w600,
),
),
Container(height: 40, child: VorschlageDropdownMenu()),
Text(
" Personen:",
style: TextStyle(
color: kPrimaryHeaderColor.withOpacity(0.6),
fontSize: (20.0),
fontWeight: FontWeight.w600,
),
),
],
),
Container(
child: renderWidget(),
),
],
);
}
}