Я создаю приложение во флаттере с помощью firebase_auth.
У меня есть класс «Вход» для представления и другой класс «Элементы» для кнопок и входов. Я пытаюсь установить функцию в классе Login на одну кнопку класса Elements, но я не знаю, как установить функцию от одной к другой. Кто-нибудь может мне помочь?
Спасибо!
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,
]
),
),
),
),
}
}
Это может быть что угодно, мне просто нужно ввести один из моих методов в кнопку
Прежде всего, я настоятельно рекомендую вам использовать специальный класс, который расширяет виджет (без сохранения состояния | с сохранением состояния) для вашей кнопки вместо вашего класса Elements.
Давайте сделаем его StatelessWidget для вашей кнопки и разрешим передавать ему параметры
class PrimaryButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return // Your button here;
}
}
После этого вам нужно разрешить вашему классу PrimaryButton получать обратный вызов для события.
class PrimaryButton extends StatelessWidget {
PrimaryButton({this.onClick, this.child});
final String child;
final Function() onClick;
// ....
}
в вашем методе сборки вы можете использовать эти свойства, полученные
@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,
);
}
Чтобы закончить это, именно там, где вам нужно, вы позвоните в свой 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 */ },
)
]
),
),
),
);
}
Спасибо! И если я хочу сделать то же самое с TextFormField, как я могу изменить состояние виджета?
Например, вам понравилось:
new RaisedButton(
child: const Text('Connect with Twitter'),
color: Theme.of(context).accentColor,
elevation: 4.0,
splashColor: Colors.blueGrey,
onPressed: () {
// Perform some action
exMethod()
},
),
и есть метод Like It:
void exMethod(){
//your action
}
какой у вас метод?