【发布时间】:2020-03-06 12:45:32
【问题描述】:
我正在寻找一种在按下 tabBar 的特定选项卡时隐藏/更改 appBar 操作的方法。
调用 setState(() { }) 方法重绘整个小部件以及所有 TabController 子/屏幕,这真的很糟糕。
下面是我的代码:
enum SelectedTab {
items, sales, raw_items, orders
}
class _MyHomePageState extends State<MyHomePage>
{
Widget rightActionButton(BuildContext context)
{
//This is the widget which is expected to be update upon tab change
SelectedTab currentTab = Globals.instance.selectedTab;
return Visibility (
child: . . .
visible: currentTab != SelectedTab.sales,
);
}
Widget navBar(BuildContext context)
{
return AppBar(
title: Text('Example'),
actions: <Widget>[
rightActionButton(context),
],
);
}
Widget tabBar(BuildContext context)
{
return Container(
child: TabBar(
tabs: [
. . . .
],
onTap: (index) {
Globals.instance.selectedTab = SelectedTab.values[index];
//Refresh rightActionButton
},
),
);
}
Widget tabScreens(BuildContext context) {
return TabBarView(
children: [
. . . .
],
);
}
@override
Widget build(BuildContext context)
{
return DefaultTabController(
length: 4,
child: Scaffold (
appBar: navBar(context),
bottomSheet: tabBar(context),
body: tabScreens(context),
),
);
}
}
我们如何重绘actions of the appBar only而不是重绘脚手架中的所有小部件?
【问题讨论】: