[英]Navigator in MaterialApp - Flutter
I wrote an simple app, when i want use onPressed
in my build()
it throw me this type of error:我写了一个简单的应用程序,当我想在我的
build()
使用onPressed
,它会抛出这种类型的错误:
Navigator operation requested with a context that does not include a Navigator.
Can anybody tell me why I can't use navigator in my onPressed()
?谁能告诉我为什么我不能在
onPressed()
使用导航器?
How can I fix it?我该如何解决?
@override
Widget build(BuildContext context) {
// double width = MediaQuery.of(context).size.width;
return MaterialApp(
home:Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50.0),
child: AppBar(
title: Image(
alignment: Alignment.center,
image: AssetImage('assets/images/logo.png'),
height: 70.0,
),
centerTitle: true,
iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
backgroundColor: Colors.white,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.person,
color: Color.fromRGBO(9, 133, 46, 100),
size: 30.0,
),
onPressed: () {
//HERE
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()));
},
),
IconButton(
icon: Icon(
Icons.shopping_cart,
color: Color.fromRGBO(9, 133, 46, 100),
size: 30.0,
),
onPressed: (){
//AND HERE :(
},
),
],
),
),
body: Builder(
...
),
),
),
floatingActionButton: RawMaterialButton(
...
),
drawer: MyDrawer(),
),
);
}
onPressed: _navigate
void _navigate() {
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
}
I fix it by create new class我通过创建新类来修复它
class UserIcon extends StatelessWidget{
@override
Widget build(BuildContext context){
return IconButton(
icon: Icon(
Icons.person,
color: Color.fromRGBO(9, 133, 46, 100),
size: 30.0,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => UserPage()));
},
);
}
}
Try making a class called Home and using that as home in MaterialApp instead of directly creating a Scaffold.尝试创建一个名为 Home 的类并将其用作 MaterialApp 中的 home,而不是直接创建一个 Scaffold。 Like so,
像这样,
return: MaterialApp(
home: Home(),
);
and then,进而,
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50.0),
child: AppBar(
title: Image(
alignment: Alignment.center,
image: AssetImage('assets/images/logo.png'),
height: 70.0,
),
centerTitle: true,
iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
backgroundColor: Colors.white,
actions: <Widget>[
IconButton(
icon: Icon(
Icons.person,
color: Color.fromRGBO(9, 133, 46, 100),
size: 30.0,
),
onPressed: () {
//TRY MY ANSWER
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()));
},
),
IconButton(
icon: Icon(
Icons.shopping_cart,
color: Color.fromRGBO(9, 133, 46, 100),
size: 30.0,
),
onPressed: (){
//HOPEFULL IT WORKS :)
},
),
],
),
),
body: Builder(
...
),
),
),
floatingActionButton: RawMaterialButton(
...
),
drawer: MyDrawer(),
),
);
}
}
You are getting this error because, MaterialApp
is actually a child of MyApp
.您收到此错误是因为
MaterialApp
实际上是MyApp
的子项。 So when we use "context" to build another page, MyApp
's BuildContext
can't use MaterialApp
as a parent.所以当我们使用“上下文”来构建另一个页面时,
MyApp
的BuildContext
不能使用MaterialApp
作为父级。 Thus, using a new Class usually fixes this issue!因此,使用新类通常可以解决此问题!
Do tell if it doesn't work!!!请告诉它是否不起作用!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.