how do you return a selected valuestring from a dropdown menu?

You can copy paste run full code below
You can use GlobalKey and assign to RecurringTypeDropDown(key: _key1)
To get value, do (_key1.currentState as _RecurringTypeDropDownState).currentSelectedValue;
code snippet

class RecurringTypeDropDown extends StatefulWidget {
  RecurringTypeDropDown({Key key}) : super(key: key);

... 
class _MyHomePageState extends State<MyHomePage> {
  GlobalKey _key1 = GlobalKey();
  GlobalKey _key2 = GlobalKey();
  String value1 = "";
  String value2 = "";
  
  void _incrementCounter() {
    value1 = (_key1.currentState as _RecurringTypeDropDownState)
        .currentSelectedValue;
    value2 = (_key2.currentState as _RecurringTypeDropDownState)
        .currentSelectedValue;

...
RecurringTypeDropDown(key: _key1),
SizedBox(height: 10),
RecurringTypeDropDown(key: _key2), 

working demo

enter image description here

full code

import 'package:flutter/material.dart';

class RecurringTypeDropDown extends StatefulWidget {
  RecurringTypeDropDown({Key key}) : super(key: key);
  @override
  _RecurringTypeDropDownState createState() => _RecurringTypeDropDownState();
}

class _RecurringTypeDropDownState extends State<RecurringTypeDropDown> {
  var currentSelectedValue;
  final dropTypes = ["Value 1", "Value 2", "Value 3"];

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Center(
            child: Container(
      padding: EdgeInsets.symmetric(horizontal: 30),
      child: FormField<String>(
        builder: (FormFieldState<String> state) {
          return Container(
            decoration: BoxDecoration(
                border: Border.all(color: Colors.white),
                borderRadius: BorderRadius.circular(10.0),
                color: Colors.white),
            child: InputDecorator(
              decoration: InputDecoration(
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10.0))),
              child: DropdownButtonHideUnderline(
                child: DropdownButton<String>(
                  hint: Text("Choose an Option",
                      style: TextStyle(
                          color: Colors.blue, fontFamily: 'Montserrat')),
                  value: currentSelectedValue,
                  isDense: true,
                  onChanged: (newValue) {
                    setState(() {
                      return currentSelectedValue = newValue;
                    });
                    print(currentSelectedValue);
                  },
                  items: dropTypes.map((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  }).toList(),
                ),
              ),
            ),
          );
        },
      ),
    )));
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GlobalKey _key1 = GlobalKey();
  GlobalKey _key2 = GlobalKey();
  String value1 = "";
  String value2 = "";
  int _counter = 0;

  void _incrementCounter() {
    value1 = (_key1.currentState as _RecurringTypeDropDownState)
        .currentSelectedValue;
    value2 = (_key2.currentState as _RecurringTypeDropDownState)
        .currentSelectedValue;

    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RecurringTypeDropDown(key: _key1),
            SizedBox(height: 10),
            RecurringTypeDropDown(key: _key2),
            SizedBox(height: 10),
            Text("$value1"),
            Text("$value2"),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top