【问题标题】:Flutter onTab Click Event Not ResponsiveFlutter onTab 点击事件无响应
【发布时间】:2021-08-14 06:41:06
【问题描述】:

我正在处理颤振页脚导航,我有一个文件,我可以在其中启动所有需要的参数,如下所示:

  const Db5BottomNavigationBarItem({
    required this.icon,
    this.title,
    Widget? activeIcon,
    this.backgroundColor,
    required this.onTap,
  }) : activeIcon = activeIcon as String? ?? icon;

  
  final String icon;
  final String activeIcon;
  final Widget? title;
  final Color? backgroundColor;
  final Function onTap;
}

这就是我在页脚组件中使用它的方式:

 child: Db5BottomNavigationBar(
          items: <Db5BottomNavigationBarItem>[
            Db5BottomNavigationBarItem(
              icon: db5_ic_home,
              onTap: () {},
            ),
            Db5BottomNavigationBarItem(
              icon: db5_ic_heart,
              onTap: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => new Health()));
              },
            ),
            Db5BottomNavigationBarItem(
              icon: db5_ic_msg,
              onTap: () {},
            ),
            Db5BottomNavigationBarItem(
              icon: db5_ic_setting,
              onTap: () {},
            ),
          ],
          // currentIndex: _selectedIndex,
          unselectedIconTheme: IconThemeData(color: db5_icon_color, size: 24),
          selectedIconTheme: IconThemeData(color: db5_colorPrimary, size: 24),
          onTap: (int index) {
            setState(() {
              // _selectedIndex = index;
            });
          },
          type: Db5BottomNavigationBarType.fixed,
        ),

问题是,一旦按钮被点击,就说第一个:

                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => new Health()));
              },

完全没有错误,也没有响应。 我做错了什么?

【问题讨论】:

  • db5_ic_home 的值是多少?为什么要使用从 Widget 到 String 的类型转换?
  • 值为0,这是我的代码:var _selectedIndex=0; Db5BottomNavigationBarItem( 图标: db5_ic_heart, onTap: () { Navigator.push(context, MaterialPageRoute(builder: (context) => new Health())); }, ), currentIndex: _selectedIndex, onTap: (int index) { setState( () { _selectedIndex = 索引; }); },

标签: flutter flutter-navigation


【解决方案1】:

BottomNavigationBarItem 没有onTap 方法,这就是为什么在那里添加代码时没有任何反应的原因。 BottomNavigationBar 本身有 onTap,就像你的代码:onTap: (int index)。每次用户点击任何项目时都会调用它,index 将根据位置为 0、1、2 等。

考虑到 cmets,我建议使用 BottomAppBar 作为 bottomNavigationBar,因为用例是导航到不同的路线。比如:

bottomNavigationBar: BottomAppBar(
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
      TextButton(
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Icon(Icons.home),
          Text('Home'),
        ]),
        onPressed: () {
          Navigator.push(context,
            MaterialPageRoute(builder: (context) => Route1()));
        },
      ),
      TextButton(
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Icon(Icons.message),
          Text('Message'),
        ]),
        onPressed: () {
          Navigator.push(context,
            MaterialPageRoute(builder: (context) => Route2()));
        },
      ),
      TextButton(
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Icon(Icons.person),
          Text('User'),
        ]),
        onPressed: () {
          Navigator.push(context,
            MaterialPageRoute(builder: (context) => Route3()));
        },
      )
    ],
  ),
))

To be complete, here is the same with BottomNavigationBar, but here is no new route, but existing widgets in _pages, and the body is replaced with one of them when something is selected.这样导航栏就会持续显示当前的选择。但这也意味着_pages中的所有小部件都被创建并消耗内存。

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;

  static const List<Widget> _pages = <Widget>[
    Text('home'),
    Text('message'),
    Text('person'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("My App"),
      ),
      body: Center(
        child: _pages.elementAt(_currentIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.message),
            label: 'Message',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Person',
          ),
        ],
        currentIndex: _currentIndex,
        selectedItemColor: Colors.amber[800],
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

【讨论】:

  • 我添加了,但我遇到了错误。你能提取它的意思吗,我的意思是代码?谢谢
  • 检查example here
  • 就像另一个答案所说,重要的是要知道您是否要导航到另一条路线并将这条路线留在导航栏所在的位置,或者您希望保持它的持久性并在两者之间切换现有的小部件。
  • 颜色完全被选中,但是,它不能导航
  • 我想导航到不同的路线。
【解决方案2】:

BottomNavigationBarItem 没有 onTap 属性。但是您可以给它一个具有 onPressed 参数的 IconButton,而不仅仅是分配一个图标。

BottomNavigationBarItem(
            label: 'Post',
            icon: IconButton(
                icon: Icon(Icons.add),
                onPressed: () => Navigator.push(context,
                    MaterialPageRoute(builder: (context) => CreatePage()))),
          )

.

bottomNavigationBar: BottomNavigationBar(
  ....
   onTap: (int index) {
   //Make sure you exclude the index of the Particular BottomNavigationBarItem you want to use the onTap on
          if (index != 2) {
            setState(() {
              _currentIndex = index;
            });
          }

如果您只想将一个 BottomNavigationBarItem 路由到某处,则应使用此选项。如果您计划在所有 BottomNavigationBarItems 上使用它,就像我在您的代码中看到的那样,它会给您带来路由问题。您应该将BottomNavigationBar OnTap 属性与currentindex 一起使用。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
相关资源
最近更新 更多