skip to Main Content

I am new to flutter I am trying to create a large form, where I am using form key to validate form and global object to store value using on Saved method on TextFormFeild of flutter how can i do same thing with DropdownButtonFormField.

Specifically, i don’t want to use DropdownButtonFormFeild onChanged method instead i want to do it using onSaved method.

2

Answers


  1. If you can use validator on TextFormField, then you can use it on dropdown too.

    Wrap with Form, add formKey then just trigger the validation with button.

    Example:

      DropdownButtonFormField<String>(
                    items: items,
                    onChanged: onChanged,
                    validator: (e){
                      if (e == null){
                        return "ERROR";
                      }
                      return null ; 
                    },
                  ),
    

    You can use validator like commong TextFormField

    Login or Signup to reply.
  2. Try this:

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key});
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      GlobalKey<FormState> formKey = GlobalKey<FormState>();
      String? selectedItem;
    
      @override
      void dispose() {
        formKey.currentState?.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
            child: Form(
              key: formKey,
              child: Column(
                children: <Widget>[
                  DropdownButtonFormField<String>(
                    value: selectedItem,
                    hint: const Text("Select"),
                    items: const <String>[
                      "A",
                      "B",
                    ].map<DropdownMenuItem<String>>(
                      (String value) {
                        return DropdownMenuItem<String>(
                          value: value,
                          child: Text(value),
                        );
                      },
                    ).toList(),
                    onSaved: (String? value) {
                      selectedItem = value;
                      setState(() {});
                    },
                    validator: (String? value) {
                      return value == null ? "Required" : null;
                    },
                  ),
                  ElevatedButton(
                    onPressed: () {
                      if (formKey.currentState?.validate() ?? false) {
                        // Valid form
                      } else {
                        // Invalid form
                      }
                    },
                    child: const Text("Submit"),
                  )
                ],
              ),
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search