【发布时间】:2025-12-22 18:45:06
【问题描述】:
我是 Flutter 的初学者,前几天刚开始。 我想从一个小部件转到另一个文件中的另一个小部件。 但是当我使用导航器时,它会显示错误。
我尝试使用堆栈溢出类似问题的一些答案来解决它,但我无法解决它。 另外,我无法正确理解这些内容。
这些是其中的一些:
Undefined name 'context' in flutter navigation
“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)
【问题讨论】:
标签: flutter