[英]How to fix 'Navigator operation requested with a context that does not include a Navigator. ' in flutter
I encounter this error.我遇到这个错误。
Navigator operation requested with a context that does not include a Navigator.
使用不包含 Navigator 的上下文请求的 Navigator 操作。
I follow the guidelines on the Internet but i'm still confused on how to fix this error.我遵循互联网上的指南,但我仍然对如何修复此错误感到困惑。 Here is my code
这是我的代码
class SecondScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.amberAccent, Colors.red]),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,// add Column
children: <Widget>[
Text('Welcome', style: TextStyle( // your text
fontSize: 50.0,
fontWeight: FontWeight.bold,
color: Colors.white)
),
RaisedButton(onPressed: () {
Navigator.pop(context);
},
child: Text('Button'),
shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)
),
color: Colors.white,
splashColor: Colors.blue,
textColor: Color(0xfffe67e22),
), // your button beneath text
],
),
),
),
);
}
}
```
The problem is not in your second page.问题不在您的第二页。 Actually the problem is in your
main.dart
;实际上问题出在你的
main.dart
; You should create a new widget for home property of MaterialApp
instead of using a Scaffold
widget directly;您应该为
MaterialApp
的 home 属性创建一个新的小部件,而不是直接使用Scaffold
小部件;
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: App(),
),
);
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => SecondPage()),
),
child: Text("SecondPage"),
),
],
),
),
);
}
}
Screenshot:截屏:
Full code:完整代码:
void main() => runApp(MaterialApp(home: MyApp(), debugShowCheckedModeBanner: false));
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.amberAccent, Colors.red]),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // add Column
children: <Widget>[
Text('Welcome',
style: TextStyle(
// your text
fontSize: 50.0,
fontWeight: FontWeight.bold,
color: Colors.white)),
RaisedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute<Null>(builder: (BuildContext context) {
return SecondScreen();
}));
},
child: Text('Button'),
shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
color: Colors.white,
splashColor: Colors.blue,
textColor: Color(0xfffe67e22),
), // your button beneath text
],
),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.amberAccent, Colors.red]),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // add Column
children: <Widget>[
Text('Welcome',
style: TextStyle(
// your text
fontSize: 50.0,
fontWeight: FontWeight.bold,
color: Colors.white)),
RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Button'),
shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
color: Colors.white,
splashColor: Colors.blue,
textColor: Color(0xfffe67e22),
), // your button beneath text
],
),
),
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.