簡體   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