如何以编程方式选择Flutter中的BottomNavigationBar选项卡而不是内置onTap回调?

Har*_*rma 5 dart flutter bottomnavigationview material-components

我一直在使用BottomNavigationBar,但是我无法在onTap回调BottomNavigationBar之外以编程方式选择Tab.

以下代码使用onTap回调[哪个有效]

    return new BottomNavigationBar(
  items: <BottomNavigationBarItem>[
    _bottomNavigationItem(Icons.account_circle, DrawerTitles.CONTACTS),
    _bottomNavigationItem(Icons.delete, DrawerTitles.DELETED_CONTACTS),
    _bottomNavigationItem(Icons.list, DrawerTitles.LOGS),
  ],
  onTap: (int index) {
    setState(() {
      navigationIndex = index;
      switch (navigationIndex) {
        case 0:
          handleBottomNavigationBarClicks(DrawerTitles.CONTACTS);
          break;
        case 1:
          handleBottomNavigationBarClicks(DrawerTitles.DELETED_CONTACTS);
          break;
        case 2:
          handleBottomNavigationBarClicks(DrawerTitles.LOGS);
          break;
      }
    });
  },
  currentIndex: navigationIndex,
  fixedColor: Colors.blue[400],
  type: BottomNavigationBarType.fixed,
);
Run Code Online (Sandbox Code Playgroud)

但我想更改onTap回调之外的选项卡?

我尝试在onTap callBack之外更改BottomNavigatioBar使用的索引,但它没有用.

这是我试过的 -

void changeTabs(int tabIndex) {
setState(() {
     navigationIndex = tabIndex;
});}
Run Code Online (Sandbox Code Playgroud)

有没有办法改变标签?

这是代码的要点

https://gist.github.com/harsh159357/62f338a196b0e5edad7aed5017dae3e5

Huy*_*àng 13

您可以使用GlobalKey获取此BottomAppBar小部件.通过此GlobalKey,您可以处理此小部件.这是代码的要点

在这里指定一个GlobalKey

GlobalKey globalKey = new GlobalKey(debugLabel: 'btm_app_bar');
Run Code Online (Sandbox Code Playgroud)

并将该键放在BottomNavigationBar中

new BottomNavigationBar(
    key: globalKey,
    items: [...],
   onTap: (int index) {...},
  ),
Run Code Online (Sandbox Code Playgroud)

现在你可以调用widget的方法了

 final BottomNavigationBar navigationBar = globalKey.currentWidget;
 navigationBar.onTap(2);
Run Code Online (Sandbox Code Playgroud)


Jav*_*ash 12

这是一个关于如何实现您想要的完整示例。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

const String page1 = "Page 1";
const String page2 = "Page 2";
const String page3 = "Page 3";
const String title = "BNB Demo";

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: title,
      home: new MyHomePage(title: title),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Widget> _pages;
  Widget _page1;
  Widget _page2;
  Widget _page3;

  int _currentIndex;
  Widget _currentPage;

  @override
  void initState() {
    super.initState();

    _page1 = Page1();
    _page2 = Page2();
    _page3 = Page3();

    _pages = [_page1, _page2, _page3];

    _currentIndex = 0;
    _currentPage = _page1;
  }

  void changeTab(int index) {
    setState(() {
      _currentIndex = index;
      _currentPage = _pages[index];
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: _currentPage,
      bottomNavigationBar: BottomNavigationBar(
          onTap: (index) => changeTab(index),
          currentIndex: _currentIndex,
          items: [
            BottomNavigationBarItem(
                title: Text(page1), icon: Icon(Icons.account_circle)),
            BottomNavigationBarItem(
                title: Text(page2), icon: Icon(Icons.account_circle)),
            BottomNavigationBarItem(
                title: Text(page3), icon: Icon(Icons.account_circle))
          ]),
      drawer: new Drawer(
        child: new Container(
          margin: EdgeInsets.only(top: 20.0),
          child: new Column(
            children: <Widget>[
              navigationItemListTitle(page1, 0),
              navigationItemListTitle(page2, 1),
              navigationItemListTitle(page3, 2),
            ],
          ),
        ),
      ),
    );
  }

  Widget navigationItemListTitle(String title, int index) {
    return new ListTile(
      title: new Text(
        title,
        style: new TextStyle(color: Colors.blue[400], fontSize: 22.0),
      ),
      onTap: () {
        Navigator.pop(context);
        changeTab(index);
      },
    );
  }
}

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(page1),
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(page2),
    );
  }
}

class Page3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(page3),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

每当您想更改为选项卡时,请调用 changeTab(YOUR_TAB_INDEX)

  • 有什么办法可以改变孩子的标签吗?如果我在第 1 页上有一个按钮,单击该按钮我想导航到第二个选项卡(第 2 页) (5认同)

Sho*_*din 5

感谢 @HuyHo\xc3\xa0ng 的 flutter 版本 2+ 我使用这个:

\n
var bottomWidgetKey=new GlobalKey<State<BottomNavigationBar>>();\n
Run Code Online (Sandbox Code Playgroud)\n

然后将此键分配给 BottomNavigationBar 然后可以访问底部,如下所示:

\n
BottomNavigationBar navigationBar =  bottomWidgetKey.currentWidget as BottomNavigationBar;\nnavigationBar.onTap!(1);\n
Run Code Online (Sandbox Code Playgroud)\n