簡體   English   中英

如何在顫振中導航到小部件的父級

[英]How to Navigate to parent of widget in flutter

我想從其他頁面導航到顫動頁面上的第二個底部項目。

在此處輸入圖片說明

這里的父級是我想要配置以更改頁面的 BottomNavigationBar(我在 Provider 中為它定義了一個鍵以從其他頁面訪問它)。

 List<Widget> pages = new List<Widget>();
  BottomNavigationBar bottomNavigationBar;

  updateList(){
    pages.addAll({
      Dashboard(),
      AllServices(),
      Account(),
      Charge(),
    });
  }

  int _selectedIndex = 0;

  Widget _bottomNavigationBar(int selectedIndex,key)  {
    return BottomNavigationBar(
      key: key,
      onTap: (int index) => setState(() => _selectedIndex = index),
      currentIndex: selectedIndex,
      type: BottomNavigationBarType.fixed,
      items: <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: new Icon(Icons.home),
          title:  new Text("A page"),
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.location_on),
          title: new Text("B page")
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.store),
          title: new Text("C page")
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.person),
          title:new Text("D page")
        ),
      ],
    );

  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    updateList();
  }

  @override
  Widget build(BuildContext context) {
    final globalKeyForBottomNavigate =  Provider.of<Controller>(context, listen: false).bottomNavigate;

    return Scaffold(
      bottomNavigationBar: _bottomNavigationBar(_selectedIndex,globalKeyForBottomNavigate),
      body: pages[_selectedIndex],
    );
  }

和孩子是頁面A,B,C,D。現在從頁面AI導航到頁面F。然后如果觸發了一個動作,我想導航到頁面B,但我的解決方案是更改頁面F中的BottomNavigationBar的索引和然后導航到頁面A。它可以工作,但不會直接導航到頁面B和BottomNavigationBar的內容。 在我的解決方案中,我說好的,首先更改BottomNavigationBar的索引,然后轉到最后一頁(這里是A頁),但是如您所見,我不知道如何直接導航到B頁。這是F頁的代碼.

onTap: (){
        var bottomKey=Provider.of<Controller>(context, listen: false).bottomNavigate;

        final BottomNavigationBar navigationBar = bottomKey.currentWidget;
        navigationBar.onTap(1);

        Navigator.pop(context);

      },

您可以嘗試在應用程序中使用環境變量。 並根據操作更改環境變量的值。 其次,將 selectedIndex 的值與該環境變量綁定,因此每當該變量發生變化時,它都會更改 selectedIndex 的值並使用新的索引值再次調用儀表板頁面,它將從底部欄導航到所需的頁面。

暫無
暫無

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

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