Getting Selected DropDownButton Value from Child Widget

Viewed 29

I'm currently building a screen in Flutter where I have a main parent widget that uses a child widget. The child widget contains 2 Textfields and a DropDownButton. I have a weird situation where my 'selectedUserValue' value for the DropDownButton isn't being updated correctly. Whenever I do a print("$selectedUserValue entered") it always return 0, but it seems to be working in the child widget? Does anyone why it works in the child but not in the Parent?

Code is below:

Parent Widget

class VisitorScreen extends StatelessWidget {
  List<User> activeUsers = [];

  TextEditingController firstNameController = TextEditingController();
  TextEditingController lastNameController = TextEditingController();
  int selectedUserValue = 0;

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
            title:
                Image.asset("images/test.png", fit: BoxFit.cover),
            backgroundColor: Color.fromRGBO(246, 248, 250, 1.0)),
        body: Column(
          children: [
            Flexible(
                child: VisitorInputControl(firstNameController,
                    lastNameController, selectedUserValue)),
            Flexible(
                child: Center(child: ElevatedButton(
              child: Text("Next"),
              onPressed: validateUser,
            )))
          ],
        ));
  }

  void validateUser() {

    print("$selectedUserValue entered");

  }
}

Child Widget

class VisitorInputControl extends StatefulWidget {

  TextEditingController firstNameController;
  TextEditingController lastNameController;
  int selectedUserID;

  VisitorInputControl(this.firstNameController, this.lastNameController, this.selectedUserID);

  State<StatefulWidget> createState() {
    return VisitorInputControlState();
  }

}

class VisitorInputControlState extends State<VisitorInputControl> {
  
  List<User> activeUsers = [];

  @override
  initState() {

    super.initState();

    _getActiveUserList();

  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double textFieldWidth = 400;

    List<DropdownMenuItem> dropdownMenu = [];
    dropdownMenu.add(DropdownMenuItem(child: Text(""), value: 0,));

    for (int i = 0; i < activeUsers.length; i++) {
      DropdownMenuItem item = DropdownMenuItem(
          child: Text("${activeUsers[i].firstName} ${activeUsers[i].lastName}"),
          value: activeUsers[i].id);
      dropdownMenu.add(item);
    }

    DropdownButton activeUsersDropdown = DropdownButton(
        hint: new Text("Visiting Staff"),
        isExpanded: true,
        items: dropdownMenu,
        value: widget.selectedUserID,
        onChanged: (value) { 
          dropdownCallback(value);
        });

    return Center(
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
      Flexible(
          child: Container(
              margin: const EdgeInsets.only(
                  left: 10.0, right: 10.0, bottom: 10.0),
              width: textFieldWidth,
              child: TextField(
                controller: widget.firstNameController,
                  decoration: InputDecoration(
                //border: OutlineInputBorder(),
                labelText: 'First Name',
              )))),
      Flexible(
          child: Container(
              width: textFieldWidth,
              child: TextField(
                controller: widget.lastNameController,
                  decoration: InputDecoration(
                labelText: 'Last Name',
              )))),
      Flexible(
          child: Container(
              margin: const EdgeInsets.only(
                  left: 10.0, right: 10.0, top: 10.0, bottom: 10.0),
              width: textFieldWidth,
              child: activeUsersDropdown))
    ]));
  }

  void _getActiveUserList() async {
  
    UserApi api = UserApi();

    List<dynamic> usersFuture = await api.getActiveUser();

    _addUsersToList(usersFuture);

  }

  void _addUsersToList(List<dynamic> userList)
  {

    setState(() {
      for (int i = 0; i < userList.length; i++){

      Map<String, dynamic> currentUserMap = userList[i];
      User currentUser = User.fromJson(currentUserMap);

      activeUsers.add(currentUser);

      }
    });

  }

  void dropdownCallback(int selectedValue) {

      setState(() {
        widget.selectedUserID = selectedValue;
      });

  }

  void _displayError() {
    print("failed to retrieve users");
  }

}
1 Answers

I guess because the parent widget is a stateless widget. Have you tried it as an stateful widget?

Related