按下按钮后如何将dropdown复位到默认值?
我尝试到按按钮到复位dropdownmune到它的defualt
class drawarScreen extends StatelessWidget {
const drawarScreen({super.key});
@override
Widget build(BuildContext context) {
List<String> list = <String>['One', 'Two', 'Three', 'Four'];
String? dropdownValue;
return Scaffold(
body: Column(children:[
DropdownButton<String>(
hint: Text('moammed'),
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? value) {
// This is called when the user selects an item.
setstate({
dropdownValue = value!;
});
},
items: list.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
},
ElevatedButton(
onPress:(){
setstate({
dropdownValue='';
});
},child: Text("reste");
)
]);
}
}
我尝试到按按钮到复位dropdownmune到它的defualt
Flutter中的复位下拉菜单
1条答案
按热度按时间djmepvbi1#
您需要放置空值而不是空字符串
如果你不喜欢null,就把它放在那里,并且确保在build方法之外有
StatefulWidget
和变量。完整小部件