当我想用flutter进行页面导航时,“Navigator operation requested with a context that does not include a Navigator.”我得到一个错误。我的代码是这样的。..................................................................................................................................................................................
void main() => runApp(const Reservation());
class Reservation extends StatefulWidget {
const Reservation({super.key, });
@override
State<Reservation> createState() => _ReservationState();
}
class _ReservationState extends State<Reservation> {
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();
@override
void initState() {
username.text = "";
password.text = "";
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Paynasoft",
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: Image.asset('image/6.png', fit: BoxFit.cover, height: 32,),
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Container(
padding: const EdgeInsets.all(40),
child: Image.asset('image/paynasoft.png', fit: BoxFit.cover, height: 180,)),
TextField(
controller: username,
decoration: InputDecoration(
labelText: "Username",
prefixIcon: const Icon(Icons.people),
border: myinputborder(),
enabledBorder: myinputborder(),
focusedBorder: myfocusborder(),
)
),
Container(height: 20,),
TextField(
controller: password,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.lock),
labelText: "Password",
enabledBorder: myinputborder(),
focusedBorder: myfocusborder(),
),
),
Container(
child: Padding(padding: EdgeInsets.all(25),),
),
Container(
height: 50,
width: 350,
child:
FloatingActionButton.extended(
label: Text("Login", style: TextStyle(fontSize: 25,),),
backgroundColor: Colors.green,
onPressed: () {Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MobileScreenLayout(),
),
);},
),
),
Container(
child: Padding (padding: EdgeInsets.all(7),),
),
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 18),
),
onPressed: () {},
child: const Text('I forgot my password?'),
),
]),
),
),
),
);
}
OutlineInputBorder myinputborder(){ //return type is OutlineInputBorder
return OutlineInputBorder( //Outline border type for TextFeild
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(
color:Colors.green,
width: 3,
)
);
}
OutlineInputBorder myfocusborder(){
return OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(
color:Colors.green,
width: 3,
)
);
}
}
字符串
screen.dart
class MobileScreenLayout extends StatefulWidget {
const MobileScreenLayout({Key? key}) : super(key: key);
@override
State<MobileScreenLayout> createState() => _MobileScreenLayoutState();
}
class _MobileScreenLayoutState extends State<MobileScreenLayout> {
int _page = 0;
late PageController pageController; // for tabs animation
@override
void initState() {
super.initState();
pageController = PageController();
}
@override
void dispose() {
super.dispose();
pageController.dispose();
}
void onPageChanged(int page) {
setState(() {
_page = page;
});
}
void navigationTapped(int page) {
//Animating Page
pageController.jumpToPage(page);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
controller: pageController,
onPageChanged: onPageChanged,
children: homeScreenItems,
),
bottomNavigationBar: CupertinoTabBar(
backgroundColor: mobileBackgroundColor,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.home,
color: (_page == 0) ? primaryColor : secondaryColor,
),
label: '',
backgroundColor: primaryColor,
),
BottomNavigationBarItem(
icon: Icon(
Icons.search,
color: (_page == 1) ? primaryColor : secondaryColor,
),
label: '',
backgroundColor: primaryColor),
BottomNavigationBarItem(
icon: Icon(
Icons.add_circle,
color: (_page == 2) ? primaryColor : secondaryColor,
),
label: '',
backgroundColor: primaryColor),
BottomNavigationBarItem(
icon: Icon(
Icons.favorite,
color: (_page == 3) ? primaryColor : secondaryColor,
),
label: '',
backgroundColor: primaryColor,
),
BottomNavigationBarItem(
icon: Icon(
Icons.person,
color: (_page == 4) ? primaryColor : secondaryColor,
),
label: '',
backgroundColor: primaryColor,
),
],
onTap: navigationTapped,
currentIndex: _page,
),
);
}
}
型
1条答案
按热度按时间tgabmvqs1#
由于您是直接从
MaterialApp
小部件导航,因此会出现错误。我们需要创建一个新的有状态或无状态小部件来使其工作。字符串
这应该能解决你的问题