[英]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.