[英]Flutter: OrientationBuilder - reuse child widgets without rebuilding?
[英]Flutter - child widgets not rebuilding in tabBarView
我無法在以下代碼中構建子操作和物流小部件。 我試過 setState 和 valuelistenableBuilder,但沒有任何效果。 Operations 和 Logistics 存儲自己的列表(和一些其他數據),當它們第一次構建(調用 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);
},
),
),
);
}```
我終於解決了……我沒有傳遞已經初始化的 GlobalKey(),而是傳遞了 UniqueKey()。 每當調用 setState() 時,這都會正確更新子代。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.