我的底部导航栏中有三个屏幕。一个屏幕包含指向另一个屏幕的按钮。通常,当我点击按钮时,我希望它在维护支架的同时打开相应的屏幕。我就是这样做的:我在底部导航中添加了一个变量,这样当我可以通过路线导航器中的索引来打开特定的屏幕时,我会收到错误
这是我的代码
import 'dart:ui';
import 'package:anime_quiz/components/appbar.dart';
import 'package:anime_quiz/components/quizDrawer.dart';
import 'package:anime_quiz/screens/newsScreen/NewsScreen.dart';
import 'package:anime_quiz/screens/homeScreen/homeScreen.dart';
import 'package:anime_quiz/screens/quizScreens/quizScreen.dart';
import 'package:anime_quiz/utilities/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
final _scaffoldKey = GlobalKey<ScaffoldState>();
class LayOutScreen extends StatefulWidget {
static const String id = "layOutScreen";
final int selectedIndex;
LayOutScreen({this.selectedIndex});
@override
_LayOutScreenState createState() => _LayOutScreenState();
}
class _LayOutScreenState extends State<LayOutScreen> {
int _selectedIndex = 1;
String _title;
@override
initState(){
super.initState();
_selectedIndex = widget.selectedIndex;
_title = 'Home';
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
switch(index) {
case 0: { _title = 'Anime Quiz'; }
break;
case 1: { _title = 'Home'; }
break;
case 2: { _title = 'Anime News'; }
break;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
appBar: PreferredSize(
preferredSize: Size.fromHeight(kH50),
child: AppBarAQ(
title: _title,
onPress: () => _scaffoldKey.currentState.openDrawer(),
leadIcon: 'assets/icons/icons8-naruto-50.png',
actionIcon: null,
),
),
bottomNavigationBar: SizedBox(
height: 52,
child: Container(
height: 40,
child: BottomNavigationBar(
backgroundColor: kWhiteColor,
unselectedItemColor: kGreyColor,
selectedIconTheme: IconThemeData(),
// showUnselectedLabels: false,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: SvgPicture.asset(
'assets/icons/shuriken2.svg',
width: kH20),
label: 'Quiz',
),
BottomNavigationBarItem(
icon: SvgPicture.asset(
'assets/icons/icons8-naruto-sign (2).svg',
width: kH20),
label: 'Home',
),
BottomNavigationBarItem(
icon: SvgPicture.asset(
'assets/icons/scroll2.svg',
width: kH20),
label: 'News',
),
],
currentIndex: _selectedIndex,
selectedItemColor: kPrimaryColor,
onTap: _onItemTapped,
),
),
),
drawer: QuizDrawer(),
body: IndexedStack(
index: _selectedIndex,
children: [
QuizScreen(),
HomeScreen(),
NewsScreen(),
],
),
);
}
}
以下是我如何使用
SimpleCard(
title1: 'ANIME',
title2: 'QUIZ',
onTap: () {Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LayOutScreen(selectedIndex: 0)
)
);},
),
我得到这个错误
[我得到的][1] :https://i.stack.imgur.com/pnuzf.png
这是控制台
我的控制台
像这样尝试
- 声明最后一个int currentIndex
- 将类的构造函数声明为yourclass({this.currentIndex=0}(;0代表第一页
- 像您的类一样导航(currentIndex:0((