class VoteCalonUmumPage extends StatelessWidget {
const VoteCalonUmumPage({Key? key, required this.title}) : super (key: key);
最終字串標題;
Widget _buildListItem(BuildContext context, DocumentSnapshot document) { return ListTile( tileColor: Color(0xff99c2ec), title: Row( children: [ Expanded( child: Text( document['name'], style: TextStyle( color: Colors.black87,字體大小: 20, ) ),
),
Container(
decoration: const BoxDecoration(
color: Color(0xffecc399),
),
padding: const EdgeInsets.all(10.0),
child: Text(
document['votes'].toString(),
style: Theme
.of(context)
.textTheme
.headline4,
),
),
],
),
onTap: () {
FirebaseFirestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap =
await transaction.get(document.reference);
await transaction.update(freshSnap.reference, {
'votes': freshSnap['votes'] 1,
});
});
},
);
}
uj5u.com熱心網友回復:
簽出下面的代碼一個簡單的邏輯它可能會幫助你,
bool isLoading = false; //global variable
onTap: () {
if(!isLoading)
{
isLoading = true;
try{
FirebaseFirestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap = await transaction.get(document.reference);
await transaction.update(freshSnap.reference, {'votes': freshSnap['votes'] 1,});
isLoading = false;
});
}catch((e){
isLoading = false
});
}
},
uj5u.com熱心網友回復:
為了實際禁用onTap處理程式,您必須傳遞null給onTap. 我會在這個類中創建一個變數來跟蹤是否onTap已經被按下,如果已經按下,傳遞null給onTap而不是你的回呼函式。
onTap: onTapPressed ? null : () {
setState(() {
// call set state here so that the UI will be updated.
onTapPressed = true;
});
FirebaseFirestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap =
await transaction.get(document.reference);
await transaction.update(freshSnap.reference, {
'votes': freshSnap['votes'] 1,
});
});
},
然后在您的小部件中添加此成員。
bool onTapPressed = false;
還有ListTile一個名為 的可選引數enabled,您可以將其設定為false而不是傳遞null給onTap。這種方法將禁用 上的所有處理程式ListTile,而不僅僅是onTap(例如,您可能還有一個onLongPress處理程式)。它還將更新樣式以使用disabled當前Theme.
disabled: !onTapPressed,
onTap: () {
setState(() {
// call set state here so that the UI will be updated.
onTapPressed = true;
});
FirebaseFirestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap =
await transaction.get(document.reference);
await transaction.update(freshSnap.reference, {
'votes': freshSnap['votes'] 1,
});
});
},
uj5u.com熱心網友回復:
請參考以下代碼
IgnorePointer 是 flutter 中的一個內置小部件,類似于 AbsorbPointer 小部件,它們都可以防止孩子的小部件受到點擊、點擊、拖動、滾動和懸停的指標事件。IgnorePointer 小部件只是忽略指標事件而不終止它,這意味著如果 IgnorePointer 小部件樹下方有任何其他元素,那么它將能夠體驗該指標事件。
bool disableOnClick = false;
IgnorePointer(
ignoring: disableOnClick ?? false,
child: ListTile(
tileColor: Color(0xff99c2ec),
title: Row(
children: [
Expanded(
child: Text(document['name'],
style: TextStyle(
color: Colors.black87,
fontSize: 20,
)),
),
Container(
decoration: const BoxDecoration(
color: Color(0xffecc399),
),
padding: const EdgeInsets.all(10.0),
child: Text(
document['votes'].toString(),
style: Theme.of(context).textTheme.headline4,
),
),
],
),
onTap: () {
FirebaseFirestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap =
await transaction.get(document.reference);
await transaction.update(freshSnap.reference, {
'votes': freshSnap['votes'] 1,
});
});
disableOnClick = true;
setState(() {});
},
),
)
uj5u.com熱心網友回復:
只需在您的 onTap 中添加條件
在單擊時宣告要處理的構建函式外部的變數
bool isClicked = false;
您的 onTap 中的條件:
onTap: () {
if (!isClicked){
isClicked = true;
FirebaseFirestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap =
await transaction.get(document.reference);
await transaction.update(freshSnap.reference, {
'votes': freshSnap['votes'] 1,
});
});
}
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/390019.html
