【发布时间】:2020-02-23 15:51:42
【问题描述】:
单击时,我正在尝试更改底部导航栏中的 FloatingActionButton() 图标。它工作正常,但只有当我离开屏幕并回来时?有谁知道出了什么问题?调用 changeIcon() 中的 Setstate 应该强制重新渲染,还是我错了?
我真的很感激一些帮助:)
编辑:
如果我将 if this.pressed bool 语句作为孩子输入,它甚至都不起作用。 this.pressed 标志的状态从 false 更改为 true(打印状态),但即使这样也不会导致按钮更改其图标。
return FloatingActionButton(
child: this.pressed ? Icon(Icons.people) : Icon(Icons.close),
onPressed: () {
changeIcon();
});
这里附上代码:
class HomeState extends State<Home> {
MapRadials buttonProvider;
Icon fabIcon = Icon(Icons.perm_media);
FloatingActionButton floatingActionButton;
int _currentIndex = 0;
List<Widget> _screens = [
FeedScreen(),
MapScreen(),
MapScreen(),
NotificationScreen(),
ChatScreen(),
];
@override
void initState() {
super.initState();
floatingActionButton = FloatingActionButton(
onPressed: () {},
child: fabIcon,
);
}
void changeIcon(Icon icon) {
setState(() {
fabIcon = icon;
});
}
@override
Widget build(BuildContext context) {
final MapRadials radialProvider = Provider.of<MapRadials>(context);
this.buttonProvider = radialProvider;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(
Icons.menu,
size: 30.0,
color: Colors.black,
),
onPressed: () {},
),
actions: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.filter_list,
size: 30.0,
color: Colors.black,
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.search,
size: 30.0,
color: Colors.black,
),
onPressed: () {},
),
],
)
],
)
],
),
bottomNavigationBar: bottomNavBar(),
floatingActionButton: this.floatingActionButton,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: _screens[_currentIndex],
);
}
void onTabTapped(int index) {
setState(() {
if (index != 2) {
_currentIndex = index;
floatingActionButton = getFloatingActionButton(index);
}
});
}
Widget getFloatingActionButton(int index) {
switch (index) {
case 0: // Home
return FloatingActionButton(child: Icon(Icons.add), onPressed: () {});
case 1: // Map
return FloatingActionButton(
child: fabIcon,
onPressed: () {
changeIcon(Icon(Icons.save));
});
case 2: // Notification
return FloatingActionButton(
child: Icon(Icons.clear_all), onPressed: () {});
case 3: // Chat
return FloatingActionButton(
child: Icon(Icons.add_comment), onPressed: () {});
default:
return FloatingActionButton(child: Icon(Icons.add), onPressed: () {});
}
}
Widget bottomNavBar() {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
onTap: onTabTapped,
currentIndex: _currentIndex,
showSelectedLabels: false,
showUnselectedLabels: false,
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
color: Colors.black,
),
title: Text('Feed'),
),
BottomNavigationBarItem(
icon: Icon(
Icons.person_pin_circle,
color: Colors.black,
),
title: Text('Map')),
BottomNavigationBarItem(
icon: Icon(
null,
),
title: Text('Placeholder')),
BottomNavigationBarItem(
icon: Icon(
Icons.notifications,
color: Colors.black,
),
title: Text('Noftications'),
),
BottomNavigationBarItem(
icon: Icon(
Icons.chat_bubble,
color: Colors.black,
),
title: Text('Chat'))
],
);
}
}
【问题讨论】: