简体   繁体   中英

How to Navigate to parent of widget in flutter

I want to navigate to the second bottom item on a page in flutter, from other pages.

在此处输入图片说明

The parent here is BottomNavigationBar that I want to configure to change the page (I define a key for it in Provider to access it from other pages).

 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],
    );
  }

And children are Page A, B, C, D. now from page AI Navigate to Page F. Then if an action is triggered, I want to navigate to page B, but my solution is to change the index of BottomNavigationBar in page F and then navigate to page A. It works, but does not directly navigate to the content of page B and the BottomNavigationBar. In my solution, I say ok first change index of BottomNavigationBar, then go to last page (that here is Page A), but as you see, I do not know how to directly navigate to page B. This is the code for page F.

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

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

        Navigator.pop(context);

      },

You can try using an environment variable in the app. and based on the action change the value of environment variable. Secondly bind the value of selectedIndex with that environment variable, so whenever that variable change, it will change the value of selectedIndex and call the dashboard page again with new value of the index it will navigate you to the required page from the bottom bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM