Reputation: 91
I'm trying to write an easier version of a shopping application using flutter but I'm having an issue when writing the onPressed method of a button I have which is supposed to contain the value of the radio button clicked (i have three radiobuttons) so that depending on its value, it will navigate to a different page. Below is the class describing the first page of the application
class MyAppState extends State
{ int _selected = 0;
void onPressed()
{
//what to write??
}
void onChanged(int value)
{
setState(() {
_selected = value;
});
}
List<Widget> makeRadios()
{
List<Widget> list = new List<Widget>();
List names = new List(3);
names[0]="Women";
names[1]="Men";
names[2]="Children";
list.add(new Text(
"Category:",
style: TextStyle(
decoration: TextDecoration.underline,
fontSize: 25,
color: Colors.deepPurple,
),
));
for(int i=0;i<3;i++)
{
list.add(
new RadioListTile(
value: i,
title: Text(names[i]),
groupValue: _selected,
onChanged: (int value){onChanged(value);},
activeColor: Colors.deepPurple,
secondary: new Icon(Icons.shopping_basket),
));
}
list.add(new RaisedButton(
child: new Text(
"done!",
style: TextStyle(
fontSize :17.0,
color: Colors.white,
),
),
color:Colors.purpleAccent,
onPressed: onPressed,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
)
);
return list;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
backgroundColor: Colors.purple,
title: new Text("Home page"),
),
body:
Center(
child:
new Container(
width: 500.0,
height:300,
color: Colors.white70,
alignment: Alignment.center,
child: new Column(
children:
makeRadios(),
),
),
),
);
}
Upvotes: 0
Views: 3202
Reputation: 7686
You already have a variable _selected
that holds current value of the selected radio button.
void onPressed() {
print('pressed $_selected');
}
And it will print the current selected radio button value.
You can replace the print statement with your navigation logic.
Upvotes: 1