繁体   English   中英

将 OnPressed 操作添加到小部件 flutter

[英]Add OnPressed action to a widget flutter

我想做一个提交按钮进行注册,但每次添加 onPressed 时都会出错,这是代码:

Widget _submitButton() {
  return Container(
    width: MediaQuery.of(context).size.width,
    padding: EdgeInsets.symmetric(vertical: 15),
    alignment: Alignment.center,
    decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(5)),
        boxShadow: <BoxShadow>[
          BoxShadow(
              color: Colors.grey.shade200,
              offset: Offset(2, 4),
              blurRadius: 5,
              spreadRadius: 2)
        ],
        gradient: LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: [Color(0xff03a9f4), Color(0xff81d4fa)])),
    child: Text(
      'Register Now',
      style: TextStyle(fontSize: 20, color: Colors.white),
    ),
  );
}

我应该改变什么才能使代码工作? 我想用这个按钮连接到 firebase

您可以将小部件包装在InkWell中并使用onTap属性

Widget _submitButton() {
  return InkWell(
    onTap: () {
      if (_formKey.currentState.validate()) {
        registerToFb();
      }
    },  // use this
    child: Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.symmetric(vertical: 15),
      alignment: Alignment.center,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(5)),
          boxShadow: <BoxShadow>[
            BoxShadow(
                color: Colors.grey.shade200,
                offset: Offset(2, 4),
                blurRadius: 5,
                spreadRadius: 2)
          ],
          gradient: LinearGradient(
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
              colors: [Color(0xff03a9f4), Color(0xff81d4fa)])),
      child: Text(
        'Register Now',
        style: TextStyle(fontSize: 20, color: Colors.white),
      ),
    ),
  );
}

或者您可以使用GestureDetector

Widget _submitButton() {
  return GestureDetector(
    onTap: () {
      if (_formKey.currentState.validate()) {
        registerToFb();
      }
    },  // use this
    child: Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.symmetric(vertical: 15),
      alignment: Alignment.center,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(5)),
          boxShadow: <BoxShadow>[
            BoxShadow(
                color: Colors.grey.shade200,
                offset: Offset(2, 4),
                blurRadius: 5,
                spreadRadius: 2)
          ],
          gradient: LinearGradient(
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
              colors: [Color(0xff03a9f4), Color(0xff81d4fa)])),
      child: Text(
        'Register Now',
        style: TextStyle(fontSize: 20, color: Colors.white),
      ),
    ),
  );
}

用 GestureDetector 包裹父容器。

return GestureDetector(
     onTap: (){  doSomething();  }
     child: Container(
           .........

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM