Reputation: 47
Hi how can I access context in Widgets, below is the code. In top layout Widget I have onclick for IconButton in which it is expecting context. So how can make context available for that onClick??
Widget toplayout = new Container(
width: 70,
height: 70,
color: Colors.white,
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new IconButton(
icon: Icon(Icons.arrow_back),
iconSize: 30,
onPressed: () {
// Here i want context
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
},
),
new Container(
margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
child: new Text("Day 1",
style: new TextStyle(
fontSize: 30,
color: Colors.black,
)))
],
),
);
Upvotes: 0
Views: 122
Reputation: 20118
You have to wrap IconButton with Builder class:
Builder(builder: (context) => IconButton(
icon: Icon(Icons.arrow_back),
iconSize: 30,
onPressed: () {
// Here i want context
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
},
),)
Upvotes: 1