How I use the value selected from drop down list from one file into the another file in flutter

Viewed 889

I am working with flutter. I create a DropDown menu in the menu_list.dart file. I want to use the value selected from the user in (menu_list.dart file) in the add_screen.dart file. So, that I can upload it in the FireStore with other user information. The code is attached below. I am glad if someone helps.

    'add_Screen.dart'
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:database/widgets/menu_list.dart';
    import 'package:flutter/material.dart';
    
    class AddScreen extends StatelessWidget {
      String? personname, personphone, vall;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Add Data"),
          ),
          body: Column(
            children: [
              Container(
                child: MenuList(),
              ),
              SizedBox(
                height: 15,
              ),
              Container(
                child: TextFormField(
                  onChanged: (String name) {
                    getStudentName(name);
                  },
                  decoration: InputDecoration(
                    labelText: "Name",
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
              ),
              SizedBox(
                height: 15,
              ),
              Container(
                child: TextFormField(
                  onChanged: (String phone) {
                    getStudentPhone(phone);
                  },
                  decoration: InputDecoration(
                    labelText: "PhoneNumber",
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
              ),
              SizedBox(
                height: 15,
              ),
              Container(
                child: RaisedButton(
                  child: Text("Add"),
                  onPressed: createData,
                ),
              ),
            ],
          ),
        );
      }
    
      createData() {
        Future<void> documentReference = FirebaseFirestore.instance
            .collection("Students")
            .doc("subcollection")
            .collection("collectionPath")
            .doc()
            .set(
          {
            "PersonName": personname,
            "PersonPhone": personphone,
          },
        );
      }
    
      getStudentName(name) {
        this.personname = name;
      }
    
      getStudentPhone(phone) {
        this.personphone = phone;
      }
    }
    `
    ----------------------------------------------------------------------------------------------
    'menu_list.dart'

    `
    import 'package:flutter/material.dart';
    
    class MenuList extends StatefulWidget {
      const MenuList({Key? key}) : super(key: key);
      @override
      _MenuListState createState() => _MenuListState();
    }
    
    class _MenuListState extends State<MenuList> {
      final items = ['Maths', 'Urdu', 'English', 'Simple'];
    
      String? value;
    
      @override
      Widget build(BuildContext context) {
        return DropdownButton<String>(
          value: value,
          isExpanded: true,
          items: items.map(buildMenuItem).toList(),
          onChanged: (value) => setState(
            () => this.value = value,
          ),
        );
      }
    }
    
    DropdownMenuItem<String> buildMenuItem(String item) => DropdownMenuItem(
          value: item,
          child: Text(
            item,
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 30,
            ),
          ),
        );
    `
2 Answers

I think the best option for you, without overegineering the code, would be to pass a callback function that is received as parameter for the onChanged parameter of the DropdownButton.

It would look something like this:

In add_screen:

String _itemSelected;
MenuList(onChanged: (value) {
    _itemSelected = value;
})

In menu_list:

class MenuList extends StatefulWidget {
    final Function(String) onChanged;
    const MenuList({required this.onChanged, Key? key}) : super(key: key);
    @override
    _MenuListState createState() => _MenuListState();
}

class _MenuListState extends State<MenuList> {
    final items = ['Maths', 'Urdu', 'English', 'Simple'];

    String? value;

    @override
    Widget build(BuildContext context) {
        return DropdownButton<String>(
            value: value,
            isExpanded: true,
            items: items.map(buildMenuItem).toList(),
            onChanged: widget.onChanged
        );
    }
}

I apologize if the code doesn't compile directly since I'm not able to compile it at the time.

Feel free to reach me if you have any questions

class AddScreen extends StatefulWidget {
  @override
  _AddScreenState createState() => _AddScreenState();
}
class _AddScreenState extends State<AddScreen> {
  // make stateful
  String? personname, personphone, vall, dropdownValue;
  // define value for dropdownValue

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Add Data"),
      ),
      body: Column(
        children: [
          Container(
            child: MenuList(dropdownValue: dropdownValue),
            // call MenuList as above passing dropdown value;
          ),
          ...
          // other children
        ],
      ),
    );
  }
}


// menu_list.dart
import 'package:flutter/material.dart';

class MenuList extends StatefulWidget {
  String dropdownValue;
  // pass the value here
  MenuList({Key? key, this.dropdownValue}) : super(key: key);
  @override
  _MenuListState createState() => _MenuListState();
}

class _MenuListState extends State<MenuList> {
  final items = ['Maths', 'Urdu', 'English', 'Simple'];
  // String? value;

  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: widget.dropdownValue,
      // set the value here
      isExpanded: true,
      items: items.map(buildMenuItem).toList(),
      onChanged: (value) => setState(
        () => widget.dropdownValue = value,
        // set the value here
      ),
    );
  }
}
Related