所以基本上我有一個小部件,我導航到:
Navigator.of(context).pushNamed(Purchases.route, arguments: {"index": index});
購買小部件只是一個空的小部件,除了兩個使用TabBar, TabBarView, and TabController. 選項卡用于pending and delivered. 這個想法是,當我導航到小部件時,正確的選項卡是打開的初始選項卡。
例如:當我單擊pending上一個小部件時,導航器導航到“購買”小部件,待處理選項卡是打開的初始選項卡。當我單擊delivered上一個小部件時,導航器導航到“采購”小部件,并且已交付選項卡是打開的初始選項卡。
我傳遞了我想要打開的選項卡的索引并在initState方法中使用它并且它可以作業,但是只有部分.
void initState() {
super.initState();
_controller = TabController(length: 2, vsync: this);
Future.delayed(Duration.zero, () {
final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
int index = args["index"];
_controller.animateTo(index);
});
}
此實作有效,但是該選項卡最初并未真正打開,它僅在索引 0 處的選項卡打開后才進行影片處理。這是糟糕的性能,因為我計劃在標簽打開時進行 API 呼叫,如果用戶沒有打開/關心索引 0 處的標簽,那么它是一個無用的 API 呼叫。
我試過了:
void initState() {
super.initState();
final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
int index = args["index"];
_controller = TabController(length: 2, vsync: this, initialIndex: index);
}
這不起作用,因為背景關系不可使用并導致錯誤。
我怎么能正確地讓它作業?謝謝你!
uj5u.com熱心網友回復:
所以我做了一些挖掘,發現這是解決方案:
void didChangeDependencies() {
super.didChangeDependencies();
final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
int index = args["index"];
_controller = TabController(length: 2, vsync: this, initialIndex: index);
}
我猜 initState 不是正確的方法。didChangeDependencies每次構建后都會被呼叫,而initState不會。要考慮的事情。
uj5u.com熱心網友回復:
將 an 設定initialIndex 為TabController是正確的方法。
為了訪問context,您需要在單獨的函式中呼叫 API,然后在initState.
void apiCall(){
//Call here your API (context accessible).
}
@override
initState((){
...
apiCall();
...
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364078.html
上一篇:如何存盤包含“.”的資料在撲動的地圖欄位內的firestore內?
下一篇:Flutter右側浮動操作欄
