英文:
Navigator operation requested with a context that does not include a Navigator error
问题
当我想要在Flutter中进行页面导航时,会出现错误:“使用不包含导航器的上下文请求导航器操作。” 我的代码如下。
...
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 OutlineInputBorder(
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,
)
);
}
}
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;
@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) {
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,
),
);
}
}
英文:
when I want to do page navigation with flutter, "Navigator operation requested with a context that does not include a Navigator." I get an error. My codes are like this.
..................................................................................................................................................................................
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
得分: 2
由于您直接从MaterialApp
小部件导航,所以出现了错误。我们需要创建一个新的有状态或无状态小部件以使其正常工作。
import 'package:flutter/material.dart';
void main() => runApp(const Reservation());
class Reservation extends StatefulWidget {
const Reservation({
super.key,
});
@override
State<Reservation> createState() => _ReservationState();
}
class _ReservationState extends State<Reservation> {
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Paynasoft',
debugShowCheckedModeBanner: false,
home: Homepage(),
);
}
}
class Homepage extends StatefulWidget {
const Homepage({super.key});
@override
State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();
@override
void initState() {
username.text = '';
password.text = '';
super.initState();
}
OutlineInputBorder myinputborder() {
return const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(
color: Colors.green,
width: 3,
),
);
}
OutlineInputBorder myfocusborder() {
return const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(
color: Colors.green,
width: 3,
),
);
}
@override
Widget build(BuildContext context) {
return 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: const Padding(
padding: EdgeInsets.all(25),
),
),
SizedBox(
height: 50,
width: 350,
child: FloatingActionButton.extended(
label: const Text(
'Login',
style: TextStyle(
fontSize: 25,
),
),
backgroundColor: Colors.green,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MobileScreenLayout(),
),
);
},
),
),
Container(
child: const Padding(
padding: EdgeInsets.all(7),
),
),
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 18),
),
onPressed: () {},
child: const Text('I forgot my password?'),
),
],
),
),
),
);
}
}
这应该解决您的问题。
英文:
Since you are directly navigating from the MaterialApp
widget you are getting the error. we need to create a new stateful or stateless widget to get this working.
import 'package:flutter/material.dart';
void main() => runApp(const Reservation());
class Reservation extends StatefulWidget {
const Reservation({
super.key,
});
@override
State<Reservation> createState() => _ReservationState();
}
class _ReservationState extends State<Reservation> {
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Paynasoft',
debugShowCheckedModeBanner: false,
home: Homepage(),
);
}
}
class Homepage extends StatefulWidget {
const Homepage({super.key});
@override
State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();
@override
void initState() {
username.text = '';
password.text = '';
super.initState();
}
OutlineInputBorder myinputborder() {
//return type is OutlineInputBorder
return const OutlineInputBorder(
//Outline border type for TextFeild
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(
color: Colors.green,
width: 3,
),
);
}
OutlineInputBorder myfocusborder() {
return const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(
color: Colors.green,
width: 3,
),
);
}
@override
Widget build(BuildContext context) {
return 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: const Padding(
padding: EdgeInsets.all(25),
),
),
SizedBox(
height: 50,
width: 350,
child: FloatingActionButton.extended(
label: const Text(
'Login',
style: TextStyle(
fontSize: 25,
),
),
backgroundColor: Colors.green,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MobileScreenLayout(),
),
);
},
),
),
Container(
child: const Padding(
padding: EdgeInsets.all(7),
),
),
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 18),
),
onPressed: () {},
child: const Text('I forgot my password?'),
),
],
),
),
),
);
}
}
This should solve your problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论