Home > Blockchain >  How to get selected item value with dropdown menu from another class in Flutter
How to get selected item value with dropdown menu from another class in Flutter

Time:10-14

I'm new to flutter and I was trying to get selected item with dropdown menu from another class. I'am gonna set the data as status to firestore. How can i reach selected item from another class? I'm new to flutter and I was trying to get selected item with dropdown menu from another class. I'am gonna set the data as status to firestore. How can i reach selected item from another class?

This is my code.

dropdown_menu.dart

const List<String> list = <String>[
 'item1',
 'item2'

];


class DropdownButtonExample extends StatefulWidget {
  const DropdownButtonExample({Key? key}) : super(key: key);

  @override
  State<DropdownButtonExample> createState() => _DropdownButtonExampleState();
}

class _DropdownButtonExampleState extends State<DropdownButtonExample> {
  String dropdownValue = list.first;
  String holder = '';

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.blueGrey),
      underline: Container(
        height: 2,
        color: Colors.orangeAccent,
      ),
      onChanged: (String? value) {
        // This is called when the user selects an item.
        setState(() {
          holder = value!;
        });
      },
      items: list.map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
  }
}

form.dart

class Candidateaddform extends StatefulWidget {
  const Candidateaddform({Key? key}) : super(key: key);

  @override
  State<Candidateaddform> createState() => _CandidateaddformState();
}

class _CandidateaddformState extends State<Candidateaddform> {
  var candidate = FirebaseFirestore.instance.collection('testdata').doc();
 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text('Aday Formu'),
          leading: IconButton(
              icon: const Icon(Icons.arrow_back, color: Colors.black),
              onPressed: () => Navigator.of(context)
                  .pushNamedAndRemoveUntil(homeRoute, (contex) => false)

              )),
      body: Column(
          DropdownButtonExample(),
          SizedBox(height: 24),
          ElevatedButton(
            onPressed: (addData),
            child: Text('Save'),
          ),
        ],
      ),
    );
  }
Future addData() async {
    await testdata.set({
      'status' : '',
      'updatedate': FieldValue.serverTimestamp(),
    }).then((value) => print('added.'));

CodePudding user response:

Form.dart

class Candidateaddform extends StatefulWidget {
  const Candidateaddform({Key? key}) : super(key: key);

  @override
  State<Candidateaddform> createState() => _CandidateaddformState();
}

class _CandidateaddformState extends State<Candidateaddform> {
  /////
  final ValueNotifier<String?> dropDownNotifier = ValueNotifier(null);
  ////
  var candidate = FirebaseFirestore.instance.collection('testdata').doc();
 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text('Aday Formu'),
          leading: IconButton(
              icon: const Icon(Icons.arrow_back, color: Colors.black),
              onPressed: () => Navigator.of(context)
                  .pushNamedAndRemoveUntil(homeRoute, (contex) => false)

              )),
      body: Column(
          DropdownButtonExample(valueNotifier:dropDownNotifier),
          SizedBox(height: 24),
          ElevatedButton(
            onPressed: (addData),
            child: Text('Save'),
          ),
        ],
      ),
    );
  }
Future addData() async {
    await testdata.set({
      'status' : '',
      'updatedate': FieldValue.serverTimestamp(),
    }).then((value) => print('added.'));

dropdownmenu.dart

const List<String> list = <String>[
 'item1',
 'item2'

];


class DropdownButtonExample extends StatefulWidget {
  final ValueNotifier<String?> valueNotifier;
  const DropdownButtonExample({Key? key,required this.valueNotifier}) : super(key: key);

  @override
  State<DropdownButtonExample> createState() => _DropdownButtonExampleState();
}

class _DropdownButtonExampleState extends State<DropdownButtonExample> {
  String dropdownValue = list.first;
  String holder = '';

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.blueGrey),
      underline: Container(
        height: 2,
        color: Colors.orangeAccent,
      ),
      onChanged: (String? value) {
        // This is called when the user selects an item.
        setState(() {
          holder = value!;
        });
        valueNotifier.value = value!;
      },
      items: list.map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
           onTap: (){
              valueNotifier.value = value;
              valueNotifier.notifyListener();
             }

and use dropDownNotifier.value to get selected item in form.dart

  • Related