DropdownMenuItem how to get string data of the Text widget

Viewed 1756
List<DropdownMenuItem<String>> department = [];

@override
void initState() {
  super.initState();
  department.add(DropdownMenuItem(
    value: 'DEP0000001',
    child: Text('IT - SAP'),
  ));
  department.add(DropdownMenuItem(
    value: 'DEP0000002',
    child: Text('HR - Admin'),
  ));
  department.add(DropdownMenuItem(
    value: 'DEP0000003',
    child: Text('Engineering'),
  ));
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: ListView(
      children: <Widget>[
        DropdownButton(
          items: department,
          onChanged: (value) {
            setState(() {
              _deptCode = value;
            });
          },
          hint: Text('Select Department'),
          value: _deptCode,
        ),
      ],
    ),
  );
}

In here I can get the value of the department code. However, I want also to get the department name. I was trying to get the String data Text widget but I think it's not possible. Is there any approach?

3 Answers

You can maintain a Map and later use the same to fetch the data. for example:

var data = {
    'DEP0000001':'IT - SAP',
    'DEP0000002' : 'HR - Admin',
    'DEP0000003' : 'Engineering'
  };

...

void initState() {
  super.initState();
  department.add(DropdownMenuItem(
    value: 'DEP0000001',
    child: Text(data['DEP0000001']),
  ));
  department.add(DropdownMenuItem(
    value: 'DEP0000002',
    child: Text(data['DEP0000002]),
  ));
  department.add(DropdownMenuItem(
    value: 'DEP0000003',
    child: Text(data['DEP0000003']),
  ));
}

Later you can get your data using this

print("The department ${data[_deptCode]} has code of $_deptCode");

Hope it helped:)

class MyHomePageState extends State<MyHomePage> {
  var list = [
    {"name": "IT - SAP", "id": "DEP0000001"},
    {"name": "HR - Admin", "id": "DEP0000002"},
    {"name": "Engineering", "id": "DEP0000003"},
  ];

  var _deptCode;

  @override
  void initState() {
    _deptCode = list.first;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: <Widget>[
          DropdownButton(
            items: list
                .map((f) => DropdownMenuItem(
                      value: f,
                      child: Text(f["name"]),
                    ))
                .toList(),
            onChanged: (value) {
              setState(() {
                print(value["name"]);
                _deptCode = value;
              });
            },
            hint: Text('Select Department'),
            value: _deptCode,
          ),
        ],
      ),
    );
  }
}

You can also use "item.child.data"

List<int> searchDropDownByValue(String keyword, items) {
  List<int> ret = [];
  if (keyword != null &&
      items != null &&
      keyword.isNotEmpty) {
    keyword.split(" ").forEach((k) {
      int i = 0;
      items.forEach((item) {
        if (k.isNotEmpty &&
            (item.child.data
                .toString()
                .toLowerCase()
                .contains(k.toLowerCase()))) {
          ret.add(i);
        }
        i++;
      });
    });
  }
  if (keyword.isEmpty) {
    ret =
        Iterable<int>.generate(items.length).toList();
  }
  return (ret);
}

See

Related