Search code examples
flutterdropdownflutter-getx

flutter getx show dropdown using Provider, Repository, MVC


I am new to Flutter, Any one share your idea to show dropdown using getx, I tried listing with List Builder. but don't have idea about dropdown using GetX ( MVC, provider, repository).


Solution

  • First declare a variable in your controller

    var selectedRole = 'CONTENT_CREATOR'.obs;

    then declare this method

    void onSelected(String value) {
    selectedRole.value = value;
    registrationParam.value.roleType = selectedRole.value;
    

    }

    finally call from your UI code like this

    Padding(
                            padding: const EdgeInsets.only(right: 8, left: 16),
                            child: Obx(
                              () => DropdownButton(
                                underline: SizedBox(),
                                isExpanded: true,
                                hint: Text('Select a role'),
                                value: _regController.selectedRole.value,
                                items: [
                                  DropdownMenuItem(
                                      value: "CONTENT_CREATOR",
                                      child: Text("Content Creator")),
                                  DropdownMenuItem(
                                      value: "PR", child: Text("PR Agency")),
                                  DropdownMenuItem(
                                      value: "JOURNALIST",
                                      child: Text("Journalist"))
                                ],
                                onChanged: (val) {
                                  _regController.onSelected(val);
                                },
                              ),
                            )),
    

    **Your initial value must be from a value of DropdownMenuItem