【发布时间】:2019-10-24 08:57:43
【问题描述】:
import 'package:flutter/material.dart';
main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
appBar: AppBar(
title: Text("Page 1"),
),
body: Center(
child: Column(
children: <Widget>[
MaterialButton(
child: Text("Next Page"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => nextPage()),
);
},
color: Colors.red,
)
],
),
),
),
);
}
}
class nextPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text("Page 2"),
),
body: Center(
child: Column(
children: <Widget>[
MaterialButton(
child: Text("Go Back!"),
onPressed: () {
Navigator.pop(context);
},
color: Colors.red,
)
],
),
),
),
);
}
}
这是我用来导航到新页面的代码,但即使编译器没有抛出任何错误,我也遇到了问题
这是我收到的错误消息
引发了另一个异常:使用不包含导航器的上下文请求导航器操作。
单击按钮时出现此错误
【问题讨论】: