简体   繁体   English

我正在尝试在 flutter 应用程序中实现底部导航栏,但我的源代码中不断出现错误

[英]I am trying to implement a bottom navigation bar in a flutter app but I keep getting errors in my source code


class BottomNavigationBar extends StatefulWidget {
  const BottomNavigationBar({super.key});

  @override
  State<BottomNavigationBar> createState() => _BottomNavigationBarState();
}

class _BottomNavigationBarState extends State<BottomNavigationBar> {
  List views = [
    const HomeView(),
    const CurrentLocationView(),
    const ProfileView(),
    const SettingsView()
  ];
  int currentIndex = 0;
  void onTap(int index) {
    currentIndex = index;

  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: views[0],
       bottomNavigationBar:const BottomNavigationBar(
        onTap: onTap,
        currentIndex:currentIndex,
        selectedItemColor: Colors.black54,
        unselectedItemColor: Colors.grey.withOpacity(0.5),
        showUnselectedLabels: false,
        showSelectedLabels: false,
        elevation:0,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            label: 'Home',
            icon: Icon(Icons.home_outlined),
          ),
          BottomNavigationBarItem(
            label: 'Map',
            icon: Icon(Icons.map),
          ),
          BottomNavigationBarItem(
            label: 'Settings',
            icon: Icon(Icons.settings),
          ),
          BottomNavigationBarItem(
            label: 'Profile',
            icon: Icon(Icons.person),
          ),
        ],
      ),
    );
  }
}

In Visual studio code, it highlights the following piece of code, which I will post below in red, and gives the following error messages:在 Visual Studio Code 中,它突出显示了以下代码,我将在下面以红色发布,并给出以下错误消息:

The name "onTap" isn't defined.名称“onTap”未定义。 Try correcting the name to an existing named parameters name or defining the named parameter with the name onTap尝试将名称更正为现有命名参数名称或使用名称 onTap 定义命名参数

The name "currentIndex" isn't defined.名称“currentIndex”未定义。 Try correcting the name to an existing named parameters name or defining the named parameter with the name "currentIndex."尝试将名称更正为现有命名参数名称或使用名称“currentIndex”定义命名参数。

The name "items" isn't defined.名称“items”未定义。 Try correcting the name to an existing named parameters name or defining the named parameter with the name "items."尝试将名称更正为现有命名参数名称或使用名称“items”定义命名参数。

It gives the same error for the other six lines of code below.对于下面的其他六行代码,它给出了相同的错误。

        onTap: onTap,
        currentIndex:currentIndex,
        selectedItemColor: Colors.black54,
        unselectedItemColor: Colors.grey.withOpacity(0.5),
        showUnselectedLabels: false,
        showSelectedLabels: false,
        elevation:0,
        items: <BottomNavigationBarItem>[

This is because your defined class ( BottomNavigationBar ) is the same as prebuilt class that comes with Flutter. you need to change that这是因为您定义的 class ( BottomNavigationBar ) 与 Flutter 附带的预建 class 相同。您需要更改它

   import 'package:flutter/material.dart';

class BottomNavigationScreen extends StatefulWidget { ////here, class name should be different from flutter built in classes
  const BottomNavigationScreen({super.key});

  @override
  State<BottomNavigationScreen> createState() => _BottomNavigationScreenState();
}

class _BottomNavigationScreenState extends State<BottomNavigationScreen> {
  List views = [
    const HomeView(),
    const CurrentLocationView(),
    const ProfileView(),
    const SettingsView()
  ];
  int currentIndex = 0;
  void onTap(int index) {
    currentIndex = index;

  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: views[0],
       bottomNavigationBar:BottomNavigationBar(
        onTap: onTap,
        currentIndex:currentIndex,
        selectedItemColor: Colors.black54,
        unselectedItemColor: Colors.grey.withOpacity(0.5),
        showUnselectedLabels: false,
        showSelectedLabels: false,
        elevation:0,
        items: const<BottomNavigationBarItem>[
          BottomNavigationBarItem(
            label: 'Home',
            icon: Icon(Icons.home_outlined),
          ),
          BottomNavigationBarItem(
            label: 'Map',
            icon: Icon(Icons.map),
          ),
          BottomNavigationBarItem(
            label: 'Settings',
            icon: Icon(Icons.settings),
          ),
          BottomNavigationBarItem(
            label: 'Profile',
            icon: Icon(Icons.person),
          ),
        ],
      ),
    );
  }
}

onTap is a Function which returns selected tab index whenever user changed tab.It gives red error because you've not defined onTap function. onTap 是一个 Function,它会在用户更改选项卡时返回选定的选项卡索引。它会给出红色错误,因为您尚未定义 onTap function。

Use Code as below,使用代码如下,

onTap: (int index) {
      print("selected Tab Index : $index");
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在 Flutter 中实现底部导航栏时,为什么我的应用程序崩溃了? - Why is my app crashing when I try to implement my bottom navigation bar in Flutter? 我正在尝试运行我的 flutter 应用程序,但出现这些错误? - I am trying to run my flutter app and I am getting these errors? 为什么我无法更改 Flutter 底部导航栏的背景颜色? - Why am I not able to change the background color of my bottom navigation bar in Flutter? 我正在尝试将我创建的底部导航栏插入我的主页,这是搜索视图,但是当我运行代码时它不起作用 - I am trying to insert the bottom navigation bar which i have created into my main page which is the hunt view but its not working when i run the code 如何在我的 Flutter 应用程序的某些屏幕中保持底部导航栏的持久性 - How to keep the Bottom Navigation Bar persistent in certain screen of my Flutter app 我想要 flutter 中的停靠底部导航栏 - I want a docked bottom navigation bar in flutter 尝试在 android studio 中构建我的颤振项目时遇到错误 - I am getting errors while trying to build my flutter project in android studio 尝试运行 flutter 应用程序时出现错误。 参数的值不能为“null” - I am getting errors when trying to run flutter app. The parameter can't have a value of 'null' 我在尝试将数据从云 Firestore 带到我的 flutter 应用程序时遇到此错误 - I am getting this error while am trying to bring data from cloud firestore to my flutter app 我想在相机屏幕选项卡上隐藏应用栏和底部导航栏 - I want to hide app bar and bottom navigation bar while i am on camera screen tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM