【问题标题】:Add OnPressed action to a widget flutter将 OnPressed 操作添加到小部件颤动
【发布时间】:2020-07-31 10:49:26
【问题描述】:

我想做一个提交按钮进行注册,但每次添加 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

【问题讨论】:

  • 你能分享一下 onPressed() 代码
  • onPressed: () { if (_formKey.currentState.validate()) { registerToFb(); } },
  • 您是如何以及在何处添加 onPressed 的?
  • 样式后),)
  • 看看我的回答是否对你有帮助

标签: flutter


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    用 GestureDetector 包裹父容器。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-26
      • 1970-01-01
      • 2019-10-13
      相关资源
      最近更新 更多