How do you pass/bind a value through a RaisedButton in Flutter? How do you pass/bind a value through a RaisedButton in Flutter? flutter flutter

How do you pass/bind a value through a RaisedButton in Flutter?


You can pass the name as an input to the onPressed function. I.e.

  new RaisedButton(      onPressed: () => _navigateToRoute(name),      child: new Text(name),  ),

The function signature would then be:

  void _navigateToRoute(String name)


You can just pass any variable to a method:

new RaisedButton(  onPressed: () => _navigateToRoute(name),  child: new Text(name),),

As long as you define your method to be able to receive variable. Than you can do what ever you want with that variable.

void _navigateToRoute(String name) {  Navigator.of(context).push(new MaterialPageRoute<Null>(    builder: (BuildContext context) {      return new Scaffold(        body: new Text(name),      );    },  ));}

You could even pass it further down in your class hierarchy:

void _navigateToRoute(String name) {  Navigator.of(context).push(new MaterialPageRoute<Null>(    builder: (BuildContext context) {      return new MyNewPage(name);    },  ));}class MyNewPage extends StatelessWidget {  String name;  MyNewPage(this.name);  ...rest of the widget code}