flutter - 清除 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'),
解决方案
由于您选择的值正在_currentCategory
使用
setState(() {
_currentCategory = null;
}
)
应该做的伎俩。
编辑:
我看到一些反对意见。这个答案基于 Flutter 1.22.6。如果它在 Flutter 2 上不起作用,那就是原因。
推荐阅读
- python - 函数出错,输出一定是30但不回答问题出在哪里?
- javascript - 如何避免使用 socket.io 更改套接字 ID?
- java - Java泛型 - 遍历从相同父级扩展的未实例化类列表并调用父级方法
- python-3.x - 我有下面的python代码,我面临错误'AttributeError:'Difference'对象没有属性'maximumDifference''
- java - Spring boot Kafka Stream Listener 消息偏移量在重启之间丢失
- php - Laravel - 查询,进行搜索
- c# - 如何用 json lis 填充 DataGridView
- java - 如何知道哪个 jtable 具有选定的行
- python - Pyserial | serial.read 也在读取我用 serial.write 发送的内容
- android-bluetooth - 如何判断 Android startDiscovery() 是否发现 BLE 或 Classic 设备