我无法在以下代码中构建子级Operations and Logistics小部件。我尝试了setState和valuelistenableBuilder,但没有任何效果。运营和物流部门存储自己的列表(以及其他一些数据),当它们第一次构建(称为init)时,它们会从API提取数据。
final GlobalKey<OperationsState> operationsKey = GlobalKey();
final GlobalKey<LogisticsState> logisticsKey = GlobalKey();
Widget build(BuildContext context) {
_tabsList = [
Tab(
child: Text(
'Operations',
style: CustomAppTheme.tabHeading,
overflow: TextOverflow.ellipsis,
),
),
Tab(
child: Text(
'Logistics',
style: CustomAppTheme.tabHeading,
overflow: TextOverflow.ellipsis,
),
),
];
// Operations and Logistics are stateful widget with their own state/data
_tabBarViewList = [
Tab(
child: Operations(
operationsKey: operationsKey,
logisticsKey: logisticsKey,
),
),
Tab(
child: Logistics(),
),
];
return DefaultTabController(
length: 2,
initialIndex: 0,
child: Scaffold(
key: scaffoldKey,
floatingActionButton: ValueListenableBuilder(
valueListenable: _showFloatingActionButton,
builder: (_, showButton, child) {
return showButton
? FloatingActionButton(
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
return CreateRequest();
})).then((val) {
// on successfull request creation I am passing 'reload' to refresh the tabs
if (val == 'reload') {
// _refreshLeaves.value++;
setState(() {
});
}
});
},
child: Icon(Icons.add),
backgroundColor: CustomAppTheme.primaryColor,
)
: Container();
},
),
appBar: AppBar(), // code omitted as not related to the question
body: ValueListenableBuilder(
valueListenable: _refreshLeaves,
builder: (_, refresh, child) {
return TabBarView(
// controller: _tabController,
children: _tabBarViewList);
},
),
),
);
}```
答案 0 :(得分:0)
我终于解决了它...而不是传递已经初始化的GlobalKey(),而是传递了UniqueKey()。每当调用setState()时,这都能正确更新子级。