Reputation: 83
I'm creating an app in flutter with firebase_auth.
I have a 'Login' class for the view and other 'Elements' class for the buttons and inputs. I'm trying to set a function in the Login class at one button of the Elements class but I don't know how to set a function from one to the other. Someone can help me?
Thanks!
class Elements {
RaisedButton buttonPrimaryFilled(String _text) {
return new RaisedButton(
onPressed: (){
},
elevation: 4,
color: Colors.red,
child: new Container(
child: new Center(
child: new Text(_text,
textAlign: TextAlign.center,
style: new TextStyle(fontSize: 23,
color: Colors.white,
letterSpacing: 2,
fontWeight: FontWeight.bold
)
),
),
width: 350,
height: 50,
),
textColor: Colors.white,
);
}
}
class _LoginPageSate extends State<LoginPage> {
@override
Widget build(BuildContext context) {
//Button login
RaisedButton btnLogin = this.myElements.buttonPrimaryFilled("Iniciar sesión");
return new Scaffold(
body: new SingleChildScrollView(
child: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//Here is the problem
btnLogin.onPressed,
]
),
),
),
),
}
}
Upvotes: 1
Views: 419
Reputation: 447
First of all, I strongly recommend you use a specific class that extends (Stateless|Stateful)Widget for your button instead of your Elements
class.
Let's make it a StatelessWidget for your button, and allows passing parameters to it
class PrimaryButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return // Your button here;
}
}
After it, u need allow your PrimaryButton
class receive a callback for the event
class PrimaryButton extends StatelessWidget {
PrimaryButton({this.onClick, this.child});
final String child;
final Function() onClick;
// ....
}
on your build method, you can use these properties received
@override
Widget build(BuildContext context) {
return new RaisedButton(
onPressed: onClick,
elevation: 4,
color: Colors.red,
child: new Container(
width: 350,
height: 50,
child: new Center(
child: new Text(
text,
textAlign: TextAlign.center,
style: new TextStyle(
fontSize: 23,
color: Colors.white,
letterSpacing: 2,
fontWeight: FontWeight.bold
)
),
),
),
textColor: Colors.white,
);
}
To finish it, just where u need you call to your PrimaryButton
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
PrimaryButton(
child: Text('Iniciar sesión'),
onClick: () { /* your function */ },
)
]
),
),
),
);
}
Upvotes: 1
Reputation: 7220
For Example, U have Like It :
new RaisedButton(
child: const Text('Connect with Twitter'),
color: Theme.of(context).accentColor,
elevation: 4.0,
splashColor: Colors.blueGrey,
onPressed: () {
// Perform some action
exMethod()
},
),
and Have Method Like It :
void exMethod(){
//your action
}
Upvotes: 0