簡體   English   中英

當我滑動時,我的應用程序正在調用所有 TabBarView 內容

[英]My app is calling all TabBarView contents when I swipe

我的應用程序有問題,問題是我有一個帶有 3 個選項卡的應用程序:

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        body: DefaultTabController(
          length: 3,
          child: _isLoading ? Center(
            child: CircularProgressIndicator()
          ) 
          : Scaffold(
            bottomNavigationBar: Material (
              child: TabBar(
                tabs: <Tab>[
                  Tab(icon: Icon(Icons.add_shopping_cart)),
                  Tab(icon: Icon(Icons.shopping_basket)),
                  Tab(icon: Icon(Icons.person)),
                ],
              ),
              color: Colors.green,
            ),
            body: TabBarView(
              children: [
                ProductListPage(),
                OrderPage(),
                InfoPage(),
              ],
            ),
          ),
        )
      ), 
      onWillPop: onBackPressed
    );
  }

在每個子頁面中,我實現了onInit從后端加載數據:

/* The exact same thing in ProductListPage, OrderPage and InfoPage */
@override
  void initState() {
    _isLoading = true;
    onInit().then((value) {
      print('initStateProductList');
    });
    super.initState();
  }

但問題是當我在TabBarView上滑動頁面時,我不明白為什么,但有時當我從第一個選項卡滑動到選項卡 3 時,應用程序正在從選項卡 2 調用InitState

在這張圖片中,我從 Tab1 滑動到 Tab3

當我從 Tab3 滑動到 Tab1 時,我發現了一個解決方法AutomaticKeepAliveClientMixin但我不喜歡它,因為我每次滑動時都想重新加載。

你正在父母身上實施這個

_isLoading ? Center(
            child: CircularProgressIndicator()
          ) 

把它留給你的孩子,這是標簽

 children: [
        ProductListPage(),
        OrderPage(),
        InfoPage(),
      ],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM