【问题标题】:How to Navigate to parent of widget in flutter如何在颤动中导航到小部件的父级
【发布时间】:2020-03-11 19:13:39
【问题描述】:

我想从其他页面导航到页面上的第二个底部项目。

这里的父级是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],
    );
  }

而孩子是Page A、B、C、D。现在从页面A I导航到页面F。然后如果触发了一个动作,我想导航到页面B,但我的解决方案是更改BottomNavigationBar的索引页面F,然后导航到页面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);

      },

【问题讨论】:

    标签: flutter flutter-navigation


    【解决方案1】:

    您可以尝试在应用程序中使用环境变量。并根据动作改变环境变量的值。其次,将 selectedIndex 的值与该环境变量绑定,因此每当该变量更改时,它都会更改 selectedIndex 的值并使用新的索引值再次调用仪表板页面,它将从底部栏导航到所需的页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2019-11-15
      • 2020-05-05
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      相关资源
      最近更新 更多