I'm creating a mobile application in which I want to use a dropdown to select a "note type" (to create a new note) and based on the selection from the dropdown I want to show a specific form for the selected note type (each note type should have their own mix of formfields, both dropdowns and text) in the section below the "note type dropdown menu".
Have yet not been able to find any example on how to achieve this and therefor giving this a try!:)
Update: The buildNoteTypeForm() functions are linked to each forms individual .dart file where each individual form will be built. This is giving me an error on onChanged: Printscreen of error
class _NoteState extends State<NoteWidget>
Map<String, Widget> noteTypeOptions = {
'First note type': buildFirstNoteTypeForm(),
'Second note type': buildSecondNoteTypeForm()
};
late String noteType;
@override
void InitState() {
noteType = noteTypeOptions.keys.first;
}
@override
Widget build(BuildContext context) {
Widget noteTypeMenu = buildNoteTypeMenu();
Widget noteTypeForm = buildNoteTypeForm();
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: ListView(children: [noteTypeMenu, noteTypeForm])),
);
}
buildNoteTypeForm() {
//Get noteType from selected dropdown option and build selected form
}
buildNoteTypeMenu() {
DropdownButton(
items: noteTypeOptions
.map((key, value) {
return MapEntry(
key,
DropdownMenuItem(
value: value,
child: Text(key),
));
})
.values
.toList(),
value: noteType,
onChanged: (noteType? selected) {
if (selected != null) {
setState(() {
noteType = selected;
});
}
});
}