我在使用抽屉和底部导航时遇到页面导航问题

  • 本文关键字:导航 遇到 问题 底部 flutter dart
  • 更新时间 :
  • 英文 :


我是flutter新手,我曾尝试创建底部导航和抽屉,但在抽屉列表磁贴中点击功能时出错,应用程序按预期工作,但当我尝试打开抽屉时,flutter会出现错误

import 'package:flutter/material.dart';
import 'package:navigation_examples_meals/screen/categories_screen.dart';
import 'package:navigation_examples_meals/screen/filters_screen.dart';
class CustomDrawer extends StatelessWidget {

Widget buildListTile(String title, IconData icon,Function onpressed){
return ListTile(
leading: Icon(
icon,
size: 26,
),
title: Text(
title,
style:const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 28,
)
),

onTap: onpressed(),
);
}  

const CustomDrawer({ Key? key }) : super(key: key);
@override
Widget build(BuildContext context) {
return Drawer(
child: Column(children: <Widget>[
Container(
height: 120,
width: double.infinity,
padding:const EdgeInsets.all(20),
alignment: Alignment.centerLeft,
color: Colors.blue, 
child:const Text(
"We have what you need",
style: TextStyle(
fontWeight: FontWeight.w900,
color:Colors.black,
fontSize:25,
)
),
),
buildListTile(
"Meals",Icons.restaurant,
() {

Navigator.of(context).pushReplacementNamed(FiltersScreen.routename);
}
),
buildListTile(
"Filters", Icons.filter,
() {
Navigator.of(context).pushReplacementNamed(FiltersScreen.routename);
}
),
],),

);
}
}

如果我评论掉导航,该应用程序工作得很好

class BottomNavigation extends StatefulWidget {

const BottomNavigation({ Key? key }) : super(key: key);
@override
_BottomNavigationState createState() => _BottomNavigationState();
}
class _BottomNavigationState extends State<BottomNavigation> {
final List<Map<String,dynamic>> pages = [
{
'page': CategoriesScreen(),
'title': "Meals"
},
{
'page': FavouritesScreen(),
'title': "Favourites"
},
];
int selectedPage_Index = 0;
void selectPage(int index){
WidgetsBinding.instance!
.addPostFrameCallback((_) => setState(() {
selectedPage_Index = index;
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
pages[selectedPage_Index]['title'],
style:const TextStyle(
color: Colors.white,
),
),
backgroundColor: Colors.blue,
),      

drawer: CustomDrawer(),
body: pages[selectedPage_Index]['page'],
bottomNavigationBar: BottomNavigationBar(
onTap: selectPage,
unselectedItemColor: Colors.white,
selectedItemColor: Colors.red,
currentIndex: selectedPage_Index,
// type: BottomNavigationBarType.shifting,
backgroundColor: Colors.blue,
items:const [
BottomNavigationBarItem(
icon:Icon(Icons.category),
label: "Categories"  ,
),
BottomNavigationBarItem(
icon:Icon(Icons.star_border),
label: "Favourites"  ,
),
],
),
);
}
}

这是我最底层的导航类

FlutterError (setState() or markNeedsBuild() called during build.
This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
Overlay-[LabeledGlobalKey<OverlayState>#0ca13]
The widget which was currently being built when the offending call was made was:
CustomDrawer)

这是我的错误

如果您像这样放置回调:onTap: onpressed(),它会在小部件渲染过程中立即调用onpressed函数,从而导致错误
您可以像这样重写onTap: onpressed,因此只有当您按下磁贴时才会调用该函数

相关内容

最新更新