【发布时间】:2022-01-25 04:16:45
【问题描述】:
我在 AppBar 中有一个按钮,我想在点击时更改其图标,但没有任何效果。 如果你检查onPressed里面的图标类型,那么根据应该是哪个按钮触发条件,但它不显示。
bool toggle = true;
late Widget searchWidget = IconButton(
onPressed: (){
setState(() {
toggle = !toggle;
});
},
icon: toggle ? const Icon(Icons.search) : const Icon(Icons.cancel),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
searchWidget,
],
title: searchBar
),
body: displayBody,
bottomNavigationBar: _bottomMenu,
);
}
【问题讨论】: