flutter - 导航器中未定义的名称“上下文”
问题描述
我是flutter的初学者,几天前才开始。我想从一个小部件转到另一个文件中的另一个小部件。但是当我使用导航器时,它会显示错误。
我尝试使用堆栈溢出类似问题的一些答案来解决它,但我无法解决它。另外,我无法正确理解这些。
这些是其中一些:
“take_attendance.dart”
class TakeAttendance extends StatefulWidget {
static String tag = 'take-attendance';
@override
_TakeAttendanceState createState() => _TakeAttendanceState();
}
bool colorCheck = false;
class _TakeAttendanceState extends State<TakeAttendance> {
@override
Widget build(BuildContext context) {
}
}
“home_page.dart”
这是 home_page.dart 中的一个小部件。
Widget showPeriod(int a) {
return Scaffold(
appBar: new AppBar(
title: new Text(
"AppName",
style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.w900),
),
backgroundColor: Colors.blue[800],
actions: <Widget>[
new Padding(
padding: EdgeInsets.only(right: 10.0),
child: Icon(Icons.exit_to_app)),
],
),
drawer: new AppDrawer(),
body: new Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Text("Period #$a",
style: TextStyle(fontSize: 50.0,fontWeight: FontWeight.w400),
)
],),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Container(
child: Text(
"<time>",
style: TextStyle(color: Colors.white),
),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(
new Radius.circular(30.0)),
color: Colors.grey,
),
padding:
new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
),
SizedBox(width: 10.0),
new Container(
child: Text(
"<Class>",
style: TextStyle(color: Colors.white),
),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(
new Radius.circular(30.0)),
color: Colors.grey,
),
padding:
new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
),
],),
new Padding(
padding: EdgeInsets.only(left: 10.0, top: 10.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Container(
child: Text(
"An imaginary subject.",
style: TextStyle(color: Colors.white),
),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(
new Radius.circular(30.0)),
color: Colors.grey,
),
padding:
new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
),
],
)),
SizedBox(height: 40.0,),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new RaisedButton(
child: const Text('GO!'),
color: Colors.redAccent,
elevation: 4.0,
splashColor: Colors.red,
onPressed: () {
// Perform some action
Navigator.of(context).pushNamed(TakeAttendance.tag);
//Navigator.push(context,takeAttendance());
},
),
new RaisedButton(
child: const Text('Cancel'),
color: Colors.blueAccent,
elevation: 4.0,
splashColor: Colors.blue[200],
onPressed: () {
// Perform some action
//Navigator.pop(context);
},
),
],
)
]
),
);
}
按 go 我希望它转到take_attendance.dart
页面。
但是当使用导航器时,这是我得到的错误:
未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义 name.dart(undefined_identifier)
解决方案
您的“上下文”在其任何父项上都没有 Navigator 实例,因为 Navgiator 是在 Scaffold 本身中初始化的。只需将 body: 包裹在一个新的小部件中,该小部件现在将有一个父导航器。
Widget showPeriod(int a) {
return Scaffold(
appBar: new AppBar(
title: new Text(
"Hajar",
style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.w900),
),
backgroundColor: Colors.blue[800],
actions: <Widget>[
new Padding(
padding: EdgeInsets.only(right: 10.0),
child: Icon(Icons.exit_to_app)),
],
),
drawer: new AppDrawer(),
body: MyPageBody(),
);
}
class MyPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Text(
"Period #$a",
style: TextStyle(fontSize: 50.0, fontWeight: FontWeight.w400),
)
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Container(
child: Text(
"<time>",
style: TextStyle(color: Colors.white),
),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
color: Colors.grey,
),
padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
),
SizedBox(width: 10.0),
new Container(
child: Text(
"<Class>",
style: TextStyle(color: Colors.white),
),
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
color: Colors.grey,
),
padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
),
],
),
new Padding(
padding: EdgeInsets.only(left: 10.0, top: 10.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new Container(
child: Text(
"An imaginary subject.",
style: TextStyle(color: Colors.white),
),
decoration: new BoxDecoration(
borderRadius:
new BorderRadius.all(new Radius.circular(30.0)),
color: Colors.grey,
),
padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
),
],
)),
SizedBox(
height: 40.0,
),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new RaisedButton(
child: const Text('GO!'),
color: Colors.redAccent,
elevation: 4.0,
splashColor: Colors.red,
onPressed: () {
// Perform some action
Navigator.of(context).pushNamed(TakeAttendance.tag);
//Navigator.push(context,takeAttendance());
},
),
new RaisedButton(
child: const Text('Cancel'),
color: Colors.blueAccent,
elevation: 4.0,
splashColor: Colors.blue[200],
onPressed: () {
// Perform some action
//Navigator.pop(context);
},
),
],
)
]);
}
}
推荐阅读
- django - Django:在字段中维护一个计数器(竞争条件)
- r - R - 在 Shiny 中使用反应数据集绘制 Plotly 图的问题
- laravel-5.6 - SQLSTATE [23000]:违反完整性约束:尝试迁移时,laravel
- reactjs - 在 redux 存储中处理来自 API 的排序和过滤集合
- visualization - 如何在 PowerBI 聚集柱形图上显示缺失的日期
- jquery - 需要通过在 jquery 中提供像“0.0000”这样的格式来获取小数位值
- c++ - 可变存储位置
- sql - 在 SQL 中获取特定 JSON 对象键的值
- angular - 如何处理(离子选择)OK按钮?
- jquery - jquery - 浮动部分更改高度大小