如何在RaisedButton的已创建对象中设置功能?

时间:2019-02-04 11:46:33

标签: android dart flutter

我正在使用firebase_auth迅速创建一个应用。

对于视图,我有一个“登录”类,对于按钮和输入有一个“元素”类。我正在尝试在Elements类的一个按钮上的Login类中设置一个函数,但是我不知道如何将一个函数设置为另一个函数。有人可以帮助我吗?

谢谢!

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,

                        ]

                    ),
                ), 
            ), 
        ), 
    }

}

2 个答案:

答案 0 :(得分:0)

首先,我强烈建议您使用扩展了(Stateless | Stateful)Widget的特定类而不是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 */ },
              )
            ]
          ),
        ),
      ),
    );
  }

答案 1 :(得分:0)

例如,您有喜欢:

    new RaisedButton(
      child: const Text('Connect with Twitter'),
      color: Theme.of(context).accentColor,
      elevation: 4.0,
      splashColor: Colors.blueGrey,
      onPressed: () {
        // Perform some action
           exMethod()
      },
    ),

并具有类似的方法:

void exMethod(){
//your action
}