I'm guessing I'm missing something simple but I've been at this for days trying every possible solution. How do we handle dropdown selection in a listview.builder? Is it possible to store dropdown button values in a List-String-?
I am creating a dynamic form based upon an XML templates and have dropdowns, checkboxes, input etc. EDIT: I dont know then what widgets are required until the XML has been parsed as the page loads. Hence the attempt to pass a dynamically created list of widgets to the Listview.builder.
Code below is an basic example failing to update.
Appreciate any advice here.
import 'package:flutter/material.dart';
class FormEG extends StatefulWidget {
@override
_FormEGState createState() => new _FormEGState();
}
class _FormEGState extends State<FormEG> {
List<String> _listValues;
List<DropdownMenuItem<String>> _items;
List<Widget> _widgets;
@override
void initState() {
_listValues = new List<String>();
_listValues.add("b");
_items = new List<DropdownMenuItem<String>>();
_items.add(new DropdownMenuItem(child: Text("a"), value: "a"));
_items.add(new DropdownMenuItem(child: Text("b"), value: "b"));
_items.add(new DropdownMenuItem(child: Text("c"), value: "c"));
_items.add(new DropdownMenuItem(child: Text("d"), value: "d"));
_widgets = new List<Widget>();
_widgets.add(new DropdownButton<String>(
value: _listValues[0],
items: _items,
onChanged: (String newValue) {
setState(() {
_listValues[0] = newValue;
});
},
));
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new ListView.builder(
itemCount: _widgets.length,
itemBuilder: (context, index) {
return _widgets[index];
},
));
}
}