首页 > 解决方案 > 清除 DropDownButton 颤动

问题描述

我有一个DropDownButton为我的产品添加类别。添加我选择的产品DropDownButton展示类别后。DropDownButton 添加产品后我想刷新或清除。更好地显示提示文本。

代码:

 child: StreamBuilder<List<Categories>>(
                              stream: categories,
                              builder: ((context, snapshot) {
                                if (!snapshot.hasData)
                                  return CircularProgressIndicator();
                                return DropdownButton(
                                  hint: Text('choose category'),
                                  value: _currentCategory,
                                  items: snapshot.data
                                      .map((doc) => DropdownMenuItem(
                                            child: Text(doc.categoryname),
                                            value: doc,
                                          ))
                                      .toList(),
                                  onChanged: (selectedCategory) =>
                                      setState(() {
                                    _currentCategory = selectedCategory;
                                  }),
                                );
                              })),
                        ),
                        SizedBox(height: 15),
                        RaisedButton(
                          onPressed: () {
                            if (_formKeyProduct.currentState.validate()) {
                              ProductService().addProduct(
                                  _nameproductController.text,
                                  _priceproductController.text,
                                  _currentCategory.categoryname.toString());
                              _formKeyProduct.currentState.reset();
                              _nameproductController.clear();
                              _priceproductController.clear();
                            }
                          },
                          child: Text('add product'),

标签: flutter

解决方案


由于您选择的值正在_currentCategory使用

setState(() {
    _currentCategory = null;
  }
)

应该做的伎俩。

编辑:

我看到一些反对意见。这个答案基于 Flutter 1.22.6。如果它在 Flutter 2 上不起作用,那就是原因。


推荐阅读