I am currently looking at Navigating withing different screens in my app and so far I can Navigate from LoginScreen to EventsScreen successfully. The problem is in EventsScreen there is a button if clicked it should take me to LoginScreen and this is the code I have.
in my events_screen.dart file
and in my app.dart file MaterialApp widget I have my routes as
The Holiday flatbutton when clicked it does not take me to the "/HolidayScreen" route.
How can I solve the [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),
),
),
],
);
}
}
Easy, just pass the BuildContext into your eventsButton
method
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(context),
Container(margin: EdgeInsets.only(bottom: 20.0)),
],
),
),
),
);
}
And add a parameter in your method
Widget eventsButton(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment
//...
UPDATED
And change this :
Navigator.pushNamed(context, "/Holiday");
To this:
Navigator.pushNamed(context, "/HolidayScreen");
Because your route name is HolidayScreen not Holiday
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With