将OnPressed操作添加到窗口小部件抖动

时间:2020-07-31 10:49:26

标签: 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

2 个答案:

答案 0 :(得分:0)

您可以将小部件包装在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),
      ),
    ),
  );
}

答案 1 :(得分:0)

用GestureDetector包裹父容器。

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