6

I want to reset value at DropdownButtonFormField, value is changed to null, but DropdownButtonFormField doesn't change. Where is the problem? If I have used DropdownButton it was changing value properly, value was cleared. I need to use DropdownButtonFormField.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {

  @override
  MyAppState createState() => MyAppState();
}


class MyAppState extends State<MyApp> {

  String abc;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center (
          child: Column(
            children: <Widget>[DropdownButtonFormField(
              hint: Text('select value'),
              value: abc,
              items: <String>['A', 'B', 'C', 'D'].map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
              onChanged: (String newValue) {
                setState(() {
                  abc = newValue;
                });
              },
           ),
            Text("value is $abc"),
          ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: (){
            setState((){
              abc = null;
            });
         },
         tooltip: 'Reset',
         child: Icon(Icons.clear),
       )        
       ),
    );
  }
}
Maroshb
  • 63
  • 1
  • 3

3 Answers3

19

You can use a GlobalKey<FormFieldState>.


class SomeWidget extends StatelessWidget {
  final GlobalKey<FormFieldState> _key;

  @override
  Widget build() {
    return DropdownButtonFormField(
      key: _key,
      //...
    )
  }

  reset() {
    _key.currentState.reset();
  }
}

https://dartpad.dev/04247868fa2e86c5f417532486b48870

idy
  • 368
  • 3
  • 9
6
class SomeWidget extends StatelessWidget {
  final GlobalKey<FormFieldState> _key = GlobalKey<FormFieldState>();

  @override
  Widget build() {
    return DropdownButtonFormField(
      key: _key,
      //...
    )
  }

  reset() {
    _key.currentState.reset();
  }
}
ammar188
  • 83
  • 1
  • 5
0

Have the key a defined in you controller as below:

GlobalKey<FormFieldState> productlistddlKey =GlobalKey<FormFieldState>();

Here how to reset inside the controller

productlistddlKey.currentState?.reset();

In the page, define your DropdownButtonFormField

   Obx(() =>  DropdownButtonFormField(
                      key: controller.productlistddlKey,
                    isDense: true,
                    autovalidateMode: AutovalidateMode.onUserInteraction,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.all(
                          Radius.circular(5),
                        ),
                      ),
                    ),
                    items: [],
                    value: value,
                    onChanged: (value) {
                    ///  
                    },
                    isExpanded: true,
                  ))
doneforaiur
  • 1,308
  • 7
  • 14
  • 21