Flutter 为什么我的底部导航栏不';我不能在弗利特工作吗?
我创建了一个简单的代码,其中包含flatter中的Flutter 为什么我的底部导航栏不';我不能在弗利特工作吗?,flutter,Flutter,我创建了一个简单的代码,其中包含flatter中的bottomnavigationbar,但它不起作用。我的代码如下: class MainPage extends StatefulWidget{ @override DashboardScreen createState() { return DashboardScreen(); } } class DashboardScreen extends State<MainPage> { Widget callPa
bottomnavigationbar
,但它不起作用。我的代码如下:
class MainPage extends StatefulWidget{
@override
DashboardScreen createState() {
return DashboardScreen();
}
}
class DashboardScreen extends State<MainPage> {
Widget callPage(int currentIndex) {
switch (currentIndex) {
case 0 : return MainPage();
case 1 : return SecondPage();
case 2 : return ThirdPage();
break;
default: MainPage();
}
}
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dashboard',
home: Scaffold(
appBar: AppBar(
title: Text('Dashboard'),
),
body: callPage(_currentIndex),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
label: 'Profile',
backgroundColor: Colors.blue
)
],
onTap: (index) {
setState((){
_currentIndex = index;
print(_currentIndex);
print("setstate");
});
},
),
)
);
}
此错误一直附加到控制台,直到我断开与设备的连接。为什么会这样?如何使我的
bottomnavigationbar
工作?谢谢 稍微欺骗一下代码,但失败的原因是MainPage
小部件正在递归地返回自身。我对代码进行了重构,并使其在中运行。您可以在那里复制、粘贴和运行代码
这些评论将向您显示陷阱
import 'package:flutter/material.dart';
// Just the app startup
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MainPage(),
);
}
}
// === The main part starts here!
class MainPage extends StatefulWidget{
@override
DashboardScreen createState() {
return DashboardScreen();
}
}
class DashboardScreen extends State<MainPage> {
Widget callPage(int currentIndex) {
switch (currentIndex) {
case 0 : return Container(color: Colors.red); // MainPage() // why? it's
// returning the same widget again recursively...
case 1 : return Container(color: Colors.blue); // SecondPage()
case 2 : return Container(color: Colors.green); // ThirdPage()
break;
default: return Container(color: Colors.amber); // HomePage() same mistake here
}
}
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dashboard'),
),
body: callPage(_currentIndex),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
label: 'Profile',
backgroundColor: Colors.blue
)
],
onTap: (index) {
setState((){
_currentIndex = index;
print(_currentIndex);
print("setstate");
});
},
),
);
}
}
导入“包装:颤振/材料.省道”;
//只是应用程序启动
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主页:主页(),
);
}
}
//==主要部分从这里开始!
类MainPage扩展了StatefulWidget{
@凌驾
仪表板屏幕createState(){
返回仪表板屏幕();
}
}
类DashboardScreen扩展状态{
小部件调用页(int currentIndex){
开关(当前索引){
案例0:返回容器(颜色:Colors.red);//MainPage()//为什么?它是
//再次递归返回相同的小部件。。。
案例1:返回容器(颜色:Colors.blue);//第二页()
案例2:返回容器(颜色:Colors.green);//第三页()
打破
默认值:返回容器(颜色:Colors.amber);//HomePage()这里也有同样的错误
}
}
int _currentIndex=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“仪表板”),
),
正文:调用页(_currentIndex),
底部导航栏:底部导航栏(
currentIndex:_currentIndex,
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
图标:图标(Icons.home),
标签:“家”,
背景颜色:Colors.blue
),
底部导航气压计(
图标:图标(图标.设置),
标签:“设置”,
背景颜色:Colors.blue
),
底部导航气压计(
图标:图标(图标、账户和圆圈),
标签:“配置文件”,
背景颜色:Colors.blue
)
],
onTap:(索引){
设置状态(){
_currentIndex=索引;
打印(当前索引);
打印(“设置状态”);
});
},
),
);
}
}
对于我BottomNavigationBarItem
没有标签
。你的颤振版本是什么?@Marcos我得到了错误,脚手架错误请检查
import 'package:flutter/material.dart';
// Just the app startup
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MainPage(),
);
}
}
// === The main part starts here!
class MainPage extends StatefulWidget{
@override
DashboardScreen createState() {
return DashboardScreen();
}
}
class DashboardScreen extends State<MainPage> {
Widget callPage(int currentIndex) {
switch (currentIndex) {
case 0 : return Container(color: Colors.red); // MainPage() // why? it's
// returning the same widget again recursively...
case 1 : return Container(color: Colors.blue); // SecondPage()
case 2 : return Container(color: Colors.green); // ThirdPage()
break;
default: return Container(color: Colors.amber); // HomePage() same mistake here
}
}
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dashboard'),
),
body: callPage(_currentIndex),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
backgroundColor: Colors.blue
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
label: 'Profile',
backgroundColor: Colors.blue
)
],
onTap: (index) {
setState((){
_currentIndex = index;
print(_currentIndex);
print("setstate");
});
},
),
);
}
}