[英]How to add widget on tree using slide animation on 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.