【发布时间】:2019-01-20 16:01:00
【问题描述】:
我目前正在查看在我的应用程序中使用不同屏幕进行导航,到目前为止,我可以从 LoginScreen 导航到 EventsScreen 成功。问题在于 EventsScreen 中有一个按钮,如果单击它应该带我到 LoginScreen,这是我拥有的代码。
在我的 app.dart 文件 MaterialApp 小部件中,我的路线为
Holiday 扁平按钮在单击时不会将我带到 "/HolidayScreen" 路线。
我该如何解决[dart] Undefined name 'context'?
events_screen.dart
import 'package:flutter/material.dart';
import 'holiday.dart';
class EventsScreen extends StatelessWidget {
Widget build(context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.white,
body: Container(
alignment: Alignment.center,
margin: EdgeInsets.all(20.0),
child: ListView(
children: <Widget>[
Container(margin: EdgeInsets.only(bottom: 20.0)),
eventsButton(),
Container(margin: EdgeInsets.only(bottom: 20.0)),
],
),
),
),
);
}
Widget eventsButton() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
color: Colors.red[800],
onPressed: () {},
child: Text(
'Events',
style: new TextStyle(color: Colors.white, fontWeight: FontWeight.normal),
),
),
Container(margin: EdgeInsets.only(right: 20.0)),
FlatButton(
color: Colors.red[800],
child: Text(
'Holidays',
style: new TextStyle(color: Colors.white, fontWeight: FontWeight.normal),
),
onPressed: () {
Navigator.pushNamed(context, "/Holiday");
},
),
Container(margin: EdgeInsets.only(right: 20.0)),
FlatButton(
color: Colors.red[800],
onPressed: () {},
child: Text(
'Flights',
style: new TextStyle(color: Colors.white, fontWeight: FontWeight.normal),
),
),
],
);
}
}
【问题讨论】:
-
试试 Navigator.of(context).pushReplacementNamed("/Holiday");
-
将你的 events_screen.dart 更改为 Stateful ,或者只是将 buildcontext 作为参数传递
-
如果您需要帮助,请粘贴您的 events_screen.dart 的代码
-
@diegoveloper 好的
标签: dart flutter flutter-layout flutter-test